Why Getters & Setters Aren't OOP, Use This Technic Instead
How Yegor Bugayenko's concept of printers finally achieve proper OOP encapsulation
Getters and Setters are common practice but fundamentally against OOP.
Allen Holub describes how getters and setters make your code terrible for maintainability. He also states that this overall pattern of using getters and setters is questionable. OOP is all about encapsulation and not revealing inner logic to outer spaces. Guess what? Use getters, and you do exactly spread internals out.
With revealing instead of encapsulating, you create highly dependent parts.
The OOP concept indeed says not to reveal internals, but how is it even possible to work internal data?
1. Getters & Setters Talk Too Much…
An easy start with a class of a
CD with two fields of
_band. These internals are sealed and can't be accessed from the outside.
Imagine converting this class into a different format (XML, HTML, Binary, etc.). Then you need to make some preparations:
You need to reveal the internals by setting up getters for
Band. Only then are you allowed to retrieve their values and pass them to the new element. Alternatively, you make public fields, which leads to the same exposure.
Whatever format you want to implement, the adjustments to get there would be like in
1 question is rising: Who is responsible for the values of
You can be sure that you will build or already have built algorithms depending on these internal values. Even worse: You make changes to…