Access members directly or always use getters

Viewed 6264

I personally find it weird/ugly when a class uses a getter to access its own member data. I know the performance impact is none but I just don't like to see all those method calls. Are there any strong arguments either way, or is it just one of those things that's personal preference and should be left to each coder, or arbitrarily controlled in a coding standard?

Update: I'm meaning simple getters, specifically for a class' non-public members.

11 Answers

My thoughts are as follows.

Everything should be static, constant, and private if possible.

  • As you need a variable to be instanced meaning more than one unique copy you remove static.

  • As you need a variable to be modifiable you remove the const.

  • As you need a class/variable to be accessed by other classes you remove the private.

The Usage of Setters/Getters - General Purpose.

  • Getter's are okay if the value is to ONLY be changed by the class and we want to protect it. This way we can retrieve the current state of this value without the chance of it's value getting changed.
  • Getter's should not be used if you are planning to provide a Setter with it. At this point you should simply convert the value to public and just modify it directly. Since this is the intent with a Get/Set.

  • A Setter is plain useless if you are planning to do more then simply "this.value = value". Then you shouldn't be calling it "SetValue" rather describe what it is actually doing.

  • If let's say you want to make modifications to a value before you "GET" it's value. Then DO NOT call it "GetValue". This is ambiguous to your intent and although YOU might know what's happening. Someone else wouldn't unless they viewed the source code of that function.

  • If let's say you are indeed only Getting/Setting a value, but you are doing some form of security. I.e. Size check, Null Check, etc.. this is an alternative scenario. However you should still clarify that in the name E.g. "SafeSetValue" , "SafeGetValue" or like in the "printf" there is "printf_s".

Alternatives to the Get/Set situations

  • An example that I personally have. Which you can see how I handle a Get/Set scenario. Is I have a GameTime class which stores all kinds of values and every game tick these values get changed.

    https://github.com/JeremyDX/DX_B/blob/master/DX_B/GameTime.cpp

  • As you will see in the above my "GETS" are not actually "GETS" of
    values except in small cases where modification wasn't needed. Rather they are descriptions of values I am trying to retrieve out of this
    GameTime class. Every value is "Static Private". I cannot do Const
    given the information is obtained until runtime and I keep this
    static as there is no purpose to have multiple instances of Timing.

  • As you will also see I don't have any way of performing a "SET" on any of this data, but there are two functions "Begin()" and "Tick()" which both change the values. This is how ALL "setters" should be handled. Basically the "Begin()" function resets all the data and loads in our constants which we CANT set as constants since this is data we retrieve at runtime. Then TICK() updates specific values as time passes in this case so we have fresh up to date information.

  • If you look far into the code you'll find the values "ResetWindowFrameTime()" and "ElapsedFrameTicks()". Typically I wouldn't do something like this and would have just set the value to public. Since as you'll see I'm retrieving the value and setting the value. This is another form of Set/Get, but it still uses naming that fits the scenario and it uses data from private variables so it didn't make sense to pull another private variable and then multiply it by this rather do the work here and pull the result. There is also NO need to edit the value other then to reset it to the current frame index and then retrieve the elapsed frames. It is used when I open a new window onto my screen so I can know how long I've been viewing this window for and proceed accordingly.

Related