Indecent Exposure — Code Smells Catalog Skip to content

Indecent Exposure

Also known as: Excessive Exposure

Couplers Data Code Smell Within Class

Everything's public. Nothing's hidden. Other modules couple to implementation details they were never meant to see, and now you can't change a private algorithm without breaking six callers who...

2 min read 1 source

Overview

Unnecessarily exposing internal details is an Indecent Exposure code smell. The methods and variables of a class that works only with other same class methods should be kept private.

Otherwise, it might lead to Insider Trading or Feature Envy code smells. One should always strive to hide as many variables and methods from other classes as possible. Exposing irrelevant code contributes to the complexity of a design.

Causation

The developer could have a habit of creating all the methods public at first but then forgets to change the access levels to appropriate ones.

Problems

Error-Prone

Fields accessible from outside the class baits for unnecessary coupling issues.

📡
Information Overload

There is no need to expose all information to everyone.

Example

Variable count is accessible by its name and can be freely changed.
1@dataclass
2class Counter:
3    count: int = field(init=False)
4
5    def bump(self) -> None:
6        self.count += 1
7
8counter = Counter()
9counter.bump()
10print(f"Count: {counter.count}")
PYTHON

Refactoring

  • Choose Proper Access Control
  • Encapsulate Field
  • Encapsulate Collection
  • Hide Behind Method
  • Hide Behind Abstract Class
  • Hide Behind Interface

Sources

Browse All 56 Smells