6 Defend the data

This chapter covers

  • Enforcing encapsulation with Do not use getters and setters (R6.1.1)
  • Eliminating getters with Eliminate getter or setter (P6.1.3)
  • Using Encapsulate data (P6.2.3) to Never have common affixes (R6.2.1)
  • Eliminating an invariant with Enforce sequence (P6.4.1)

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.

6.1 Encapsulating without getters

At this ...

Get Five Lines of Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.