Forum > Noggit

Getters and setters are bad code practice?

(1/1)

medi4:
Hi there!
in the Noggit3 contributing guidelines (https://bitbucket.org/berndloerwald/noggit3) it states that

--- Quote ---//! \note If you really need getters and setters, your design
      //! might be broken.
--- End quote ---
What is meant by that? Isn't OOP all about the Holy and Sacred Encapsulation of data and therefore getters and setters are a necessity?
Looking forward to your arguments!

schlumpf:
Getters and setters are the exact opposite of encapsulation: You don't hide any logic but expose everything directly. Yes, you can check things in a setter, but that's as far as encapsulation goes there. You want your classes to encapsulate _logic_. Instead of setting a state, you want to tell the class how to modify _its_ state. E.g. instead of set_x(), you want y_happened(), which implicitly updates x, hidden from the user of the class.

medi4:
Hmm, I guess that is why it says "might be broken" since there are still cases in which it is ok and makes sense to use getters and setters. Anyway that clears it up for me, many thanks!

Navigation

[0] Message Index

Go to full version