2

Is it really necessary or a good practice in general to add getters and setters on all the private variables of a class?

I am not asking about the read-only variables. What I am really asking is the general case, where most of the time we just add getters and setters in case they'll be needed and don't really know whether they will be used or not.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 2
    Of course not. In most cases, most of a class's variables should be private fields. Furthermore, in many cases, properties should be read-only and may not need setters at all. In the case of those variables that *should* be exposed publicly, however, it is sometimes necessary, but nearly always good practice, to use getters and setters instead of fields, for many reasons. – phoog Jun 06 '16 at 03:51
  • 2
    Possible duplicate of [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – phoog Jun 06 '16 at 03:52
  • @phoog Thans for the response. But I was not really asking about 'public varibales' vs getters and setters. I edited my question in case it was difficult to unerstand. – Supun Wijerathne Jun 06 '16 at 04:12
  • any way implicitly they are set automatically, we can just customize them if needed... – Arun Prasad E S Jun 06 '16 at 04:14
  • @ARUN Can you please give an example what you mean by 'if needed'? – Supun Wijerathne Jun 06 '16 at 04:17
  • 1
    There are many blogs that consider getters/setters evil. I would try to avoid then alltogether. And I would never add them if I do not know if they are needed. – CoronA Jun 06 '16 at 04:21
  • i happens internally, you can check it during debugging, create a object of a class and set a break point to see the values, you can see there , same field repeated twice – Arun Prasad E S Jun 06 '16 at 04:24
  • @CoronA its needed 100%, if you need to control the rights , like make it read only, validation etc – Arun Prasad E S Jun 06 '16 at 04:24
  • Only if it makes sense. It makes sense when it helps the user of the class to use the class effectively. The public interface is likely the most important feature of a class in OO design. Everything on there should mean something from a user's perspective and if it does not, it should not be there. The mere existence of a private data member is not a reason to create a public property. – Martin Maat Jun 06 '16 at 04:54
  • 2
    @ARUN: I almost never need setter properties. The internal state is initialized in the constructor and changed in an oo way with full qualified semantically named methods. Read the attached links, e.g http://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen?rq=1 to get an impression how setters could be avoided. – CoronA Jun 06 '16 at 06:15
  • @CoronA its not needed on daily basis, only if you want to make it readonly, validation simplification, there are some useful things you can do with it – Arun Prasad E S Jun 06 '16 at 06:21
  • 2
    Seems like a good question for [programmers.se], as it is not about a specific programming problem, but about best practises. – SQB Jun 06 '16 at 07:15

3 Answers3

4

There are different explanations by different people regarding the use of 'getters' and 'setters'. The purest OO people says, they are evil.

But actually there are situations where we have to go with them at least in non-direct way.

But I believe that, it is not a good practice to add them seamlessly as you suggest.

Nipun Wijerathne
  • 1,839
  • 11
  • 13
1

As private properties are the internal state of the defining class there is no need to write accessors.

The benefits are:

  • faster code execution (less function calls),
  • higher readability and maintainability (lower LOC),
  • accessibility to private properties only for the defining class.
John Smith
  • 1,091
  • 9
  • 17
-2

It is not necessary to write getter or setter for all private variables. It is just a good practice. But without any public function you can not access the private data(variable) of the class.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • It is actually the opposite - it is bad practice to expose unnecessary information as you will likely create unwanted dependencies to private data. Information hiding is one of the key principles - which gets obsolete if you can access all the contained data via accessor methods. – Florian Salihovic Jun 06 '16 at 08:56