"Don't Ask, Tell" Principle

 Asking the object about its state (usually using if...else.. or switch statements), and then taking the decision on what to do next using that conditions leads to leaky abstraction. The behaviour of the object is exposed to outside world.

The Don't Ask, Tell principle simply states to avoid querying or writing conditions on state of object outside its context. 

The Logic is outside object context :














The Logic is inside object context :










Example : 
1. Consider you want your game player to move, you don't make the legs move separately like 
if(object.state === 'something')
{
      object.move();
}


Instead you will :
object.move()
and write the condition, inside the method of object class. 


The state and data is tightly coupled in this case. The code for the state and data in tightly coupled system should be present in same component. 

Comments

Popular posts from this blog

Dependency Inversion Principle (DIP) Explained

GREP Cheatsheet