The Pragmatic Practices
Now that we have established what makes a code object-oriented, it's time to consider the practices and patterns that will help us achieve it.
The practices really are "what we do" and "what we don't do". They're written in the imperative form because they are actions and prohibitions.
As part of our practice of object-orientation, we do not use getters and setters. We inherit wisely and we Expose Snapshots.
Now, mind that the book is called Pragmatic Objects and not Dogmatic Objects. I believe we should do our best and thrive to achieve these practices because I think they lead to much better object-oriented code. I also believe that code following these rules meticulously will be very flexible and easy to maintain. But these are my beliefs and they are not backed by strong scientific evidence.
As such, I believe in my own judgement to sometimes bypass these rules as I see fit, and so should you. I cannot possibly anticipate all situations and know every context, so there may be some in which part of these rules may be difficult to apply. Sometimes, the No Getters & Setters practice must be violated, either temporarily in the practice of TDD for example, or definitely because the underlying framework or tooling needs them, or because we have to expose some informations as part of the object's contract.
So the best would be to think of them as heuristics. They're not backed by evidence, they're not hard rules either, they're just indications. This article by Rebecca Wirfs Brock talks about heuristics in patterns better than I can do.
As always, see if they speak to you and resonate with you. Don't apply them blindly. Be sure to understand the rationale and the thought process behind each practice. Strive to apply them as strictly as possible in your side projects, see them as a form of Object Calisthenics. That way, you will gain a lot of insight.
Last updated