Code Smell 321: Why Reusing Getters for Business Logic Breaks Encapsulation

A common programming mistake called 'getter piggybacking' occurs when developers reuse an existing getter method to bolt on new business logic outside the object that owns the data. Instead of adding a proper method to the class, multiple external functions end up calling the same getter and each writing their own version of the same comparison, scattering domain knowledge across the codebase. This violates the encapsulation principle and the Law of Demeter, making the code fragile and harder to refactor safely. The fix is to move the logic inside the object itself as a dedicated method, so callers simply ask the object a question rather than extracting raw values to interpret on their own. Because it is a design-level smell rather than a syntax error, no automated linter can catch it — developers must manually look for getters used in comparisons or filters across multiple external call sites.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in