Do we really need Get/Set methods to access class members?

Hi guys,
Do we really need Get/Set methods to access class members from outside the class? When do you access the class fields directly and when do you access them through getter setter methods? Is there any recommended pattern for this? any advice would be appreciated :)
In general: use getter/setter methods when you need to enforce class invariants.
In cases where you don't care what a member gets set to (i.e. any possible data is valid), then you can make them public.
Great question!

I've been meaning to make an article or something on this.

It depends on what you're getting/setting. But basically if all your setter does is set the appropriate variable, then no, you don't need it. Don't follow the stupid "always make your variables private" rule. It's dumb and just makes you waste all sorts of time writing stupid useless get/sets, and makes your code unnecessarily huge.

1
2
3
4
5
6
7
8
9
10
// this is an example of a stupid/pointless get/set
string MyClass::GetName()
{
  return name;
}

void MyClass::SetName(const string& s)
{
  name = s;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// this is an example of a meaningful get/set
int MyClass::GetPercentage()
{
  return percent;
}

void MyClass::SetPercentage(int s)  // this actually does something other than just assign
{
  if(s < 0)
    percent = 0;
  else if(s > 100)
    percent = 100;
  else
    percent = s;
}


Having get/set only does anything for you if:
- You need to do bounds checking or some other kind of validation on the setter
- You need to do some kind of alternative routing (ie, your get might return one of many possible variables based on the state of the object)
or
- The variable you're getting/setting is sensitive to the internal state of the object. For example, if you could directly access vector's size member, instead of having to call size()/resize() ("get"/"set"), that would be very bad, since vector needs to adjust other things when the size changes. For example if you increase the size, it would need to allocate more memory (not just change a 'size' variable).



Now people may argue that always going through a get/set gives you the option to add bounds checking or other things later without destroying the interface. This is true, but in many applications it's a moot point, as bound checking or other things are never going to be needed.

If you have a huge list of properties that do nothing but record data, then it doesn't make any sense at all to put them behind getters/setters... and in doing so you're just opening yourself up to a world of hurt.

So really, just use your judgement.

- If data is sensitive, definitely use a get/set (but really, sensitive data often shouldn't even have a setter).
- If data needs validation, use a get/set.
- If you expect data might need validation in the future, strongly consider using a get/set even if it's not immediately necessary
- If the data is just for information storage and has no impact on the internal state of the object, then don't use get/set.


EDIT: Doh, I'm a little slow, but at least I'm thorough!
Last edited on
Thank you for your thorough reply buddy :) I appreciate it!
Last edited on
Disch is there anyway around using getters/setters? u saw the rpg im working on is there a simpler way of going about it?

if there is please let me know

thanks
Disch is there anyway around using getters/setters?


Just make your data members public.

1
2
3
player.name = "playername"; // do this

player.SetName("playername"); // instead of this 
that makes more sense and i dont need to all that redundant typing lol thanks man
Topic archived. No new replies allowed.