You should tell the objects what you want them to do instead of querying them multiple times to be able to make a decision on behalf.
Since each object have access to its data and internal state; it can do its job, make decisions and take actions internally.
Tell don’t ask principle bring a few benefits including code reuse
, testability
and reduce control coupling
.
Not good:
if (user.is_admin()){
print user.admin_greeting();
}else{
print user.user_greeting();
}
Better:
// greeting method will decide what is the proper message
print current_user.greeting();
Not good:
if (user.is_hungry()){
user.eat();
}
Better:
// eat method will check if user is hungry and decide on the action
print user.eat();