Tell Don’t Ask & Coupling

Iman Tung
Aug 6, 2022

--

In OOP, Encapsulation is bundling the data with logic within the class. In short, we tell the object what to do rather than ask for the data (Tell Don’t Ask Principle).

Tell Don’t Ask

Ask too much to the object will create the smell of code called Feature Envy or Inappropriate Intimacy. The objects are not loose coupling with each other (coupler).

The Smell Code

Furthermore, the object-oriented design should follow the Law of Demeter (a.k.a Principle of Least Knowledge) in order to loosely couple
- Each unit should have only limited knowledge about other units: only units “closely” related to the current unit.
- Each unit should only talk to its friends; don’t talk to strangers.
- Only talk to your immediate friends.

Law of Demeter

--

--

Iman Tung
Iman Tung

Written by Iman Tung

Technology to write, life to grateful. Overthinking is good, only if it has the output. Fundamental is the main concern.

No responses yet