Tell, Don't Ask ~ Don't Call Us - We'll Call You

Monday, February 23, 2009

Tell, Don't Ask

Procedural code gets information then makes decisions. Object-oriented code tells objects to do things.
— Alec Sharp
That is, you should endeavor to tell objects what you want them to do; do not ask them questions about their state, make a decision, and then tell them what to do.

The problem is that, as the caller, you should not be making decisions based on the state of the called object that result in you then changing the state of the object. The logic you are implementing is probably the called object’s responsibility, not yours. For you to make decisions outside the object violates its encapsulation.

Source: http://www.pragprog.com/articles/tell-dont-ask

No comments:

Post a Comment