Thursday, January 31, 2008

Tell, Don't Ask

Procedural code gets information then makes decisions. Object-oriented code tells objects to do things.
— Alec Sharp

http://pragmaticprogrammer.com/articles/tell-dont-ask

Programmers should avoid writing code that looks like:

dog.getBody().getTail().wag();

colloquially known as a “Train Wreck”. This is bad because this
one line depends on the interfaces and implied structure of three
different objects

The solution is described by the heuristic "Tell, Don't Ask" , so we rewrite our
example as:

dog.expressHappiness();

and let the implementation of the dog decide what this means.

1 comment:

Unknown said...

I like your tell, don't ask funda .. nice one, programmer should learn to wirte leading program rather than asking programs. sounds good.