Search code examples
c#javaoopencapsulation

Getters and Setters are bad OO design?


Getters and Setters are bad

Briefly reading over the above article I find that getters and setters are bad OO design and should be avoided as they go against Encapsulation and Data Hiding. As this is the case how can it be avoided when creating objects and how can one model objects to take this into account.

In cases where a getter or setter is required what other alternatives can be used?

Thanks.


Solution

  • Getters or setters by themselves are not bad OO design.

    What is bad is coding practice which includes a getter AND a setter for EVERY single member automatically, whether that getter/setter is needed or not (coupled with making members public which should not be public) - because this basically exposes class's implementation to outside world violating the information hiding/abstraction. Sometimes this is done automatically by IDE, which means such practice is significantly more widespread than it's hoped for.