-1

A small debate flared up in a question about protected member variables regarding the use of getters/setters. There are lots of questions already over whether getters/setters are evil, however one particular argument against them, which was posed by two separate individuals with much higher reputation than myself, struck me.

One said that getters/setters made the code only 0.01% less brittle, and the other stated that adding 10 lines of code where one would do makes the code more brittle.

This goes against most of what I had previously read, been taught, thought, or experienced. Does anyone else agree/disagree with those comments?

Community
  • 1
  • 1
Robert Gowland
  • 7,677
  • 6
  • 40
  • 58
  • 1
    I would argue this is at least partially dependent upon the language. Some hide the notion entirely allowing a "direct access" to be transparently changed later (and others don't allow "direct access" at all!). Something that might be good in X might be not-so-good in Y. –  Oct 15 '10 at 20:27
  • @pst - agreed, and I would add that it also depends on the code / problem at hand. I would also suggest making this wiki as there is no definitive answer. – RQDQ Oct 15 '10 at 20:31
  • 1
    We've done this before. Repeatedly. See [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) and others. On top of which this might be better on [Programmers](http://programmers.stackexchange.com) than Stack Overflow. But check for duplicates there as well because this comes up over and over again. – dmckee --- ex-moderator kitten Oct 15 '10 at 20:31
  • The procession of a programmer: 1) Beginner programmer: "does not know getter/setter, use direct member access for everything". 2) Mid-level programmer: "reads about getter/setter pattern, consistently apply the pattern for everything and anything". 3) Senior-level programmer: "advocates that getter/setter is evil, and should be used sparingly". 4) Guru: "knows where getter/setter are useful and where they're evil" – Lie Ryan Oct 15 '10 at 20:47

1 Answers1

3

I know it is close to herecy, but I hate get/set methods. Loathe them. Almost never write them.

Generally, a class should either provide much more high-level operations than directly and simply reading and modifying internal state variables, or it should get the hell out of the way and act like the struct it is.

Even if I were to write one, I would almost never use it inside the class. The whole point of them is that you can change the internal representation of thing without affecting a client. Inside the class, it is the internal representation you care about! If you are tempted to do a lot of operations on the class using its own interface inside the class, you probably have a second class in there fighting to get out.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • Just for fun, this answer is a repeat of my answer in http://stackoverflow.com/questions/2374009/calling-this-get-this-set-methods-versus-directly-accesing-member-variables-in/2374139#2374139. I'm curious what the result will be. – T.E.D. Oct 15 '10 at 20:38