In chapter 2, we discussed the advantage of localizing invariants. We have already done that when introducing classes because they pull together functionality concerning the same data and thereby also pull invariants closer and localize them. In this chapter, we focus on encapsulation—limiting access to data and functionality—such that invariants can only be broken locally and therefore are much easier to prevent.
At this point, the code follows our rules and is already much more readable and extendable. However, we can do even better by introducing another rule: Do not use getters or setters.