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
Fields accessible from outside the class baits for unnecessary coupling issues.
There is no need to expose all information to everyone.
Example
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}")Refactoring
- Choose Proper Access Control
- Encapsulate Field
- Encapsulate Collection
- Hide Behind Method
- Hide Behind Abstract Class
- Hide Behind Interface
Sources
- ORIGIN