Blogging from Inside the New Media Revolution

Tell, Don’t Ask

1 Comment
By alvin - February 20th, 2009

No, I am not suggesting you turn into a dictator and boss people around.
Warning, ruby specific software developer content ahead.

Back in the old days before my time, computers were programmed with Procedural Languages. Measure 1 cup of pancake mix into a bowl, stir in 1 cup milk, mix. Heat griddle … well, you get the idea. Everything was spelled out in procedural sequence. And programs were long and hard to maintain.

Then along came Smalltalk-80 which introduced the world to Object Oriented programming. Now we can tell the Griddle object to flip the contents. How that happens is entirely up to the griddle. The goal is to split large programs into manageable pieces (objects) that are easier for people to work through.

Tell Don’t Ask refers to a guideline in Object Oriented programming: tell an object to do something, don’t ask it for private data.

Alec Sharp (1) contrasts procedural and object oriented programming in his book Smalltalk by Example by:

“Procedural code gets information then makes decisions. Object-oriented code tells objects to do things.”
Alec Sharp, as quoted in the Pragmatic Bookshelf article “Tell, Don’t Ask”


With procedural languages writing something like this was common:

while scanner.has_links_to_visit

link = scanner.next_link_to_visit
webpage_contents = get(link)
process_webpage(webpage_contents)

end

My fingers hurt just typing that in.

Imagine my shock and horror when I realized that example was recently written in my favorite object oriented language: ruby. How could this be?  Was I asleep at the keyboard when I typed in the while loop?

How do we know this code is procedural?  Simple, it asks the “scanner” is there any work to do and makes a decision based on the answer which violates the “Tell, Don’t Ask” guideline.

We can turn this procedural code into object oriented code by using an iterator. David A. Black covers this topic nicely in his book The Well-Grounded Rubyist.  The object oriented version using an iterator turns the while loop into

scanner.each_link_to_visit {|link| process_webpage(get(link)) }

The iterator “each_link_to_visit” quietly hides the details and provides values to the block, one at a time.  The big win comes during application maintenance: reading 1 line of clear code is easier to understand than reading 5 lines of code.  It’s like reading the Cole’s Notes version of War and Peace versus the reading the original version in Russian.

Iterators are a handy-dandy tool for converting procedural while loops into object oriented loops.

Happy iterating,

Alvin.

Tags:

Filed under: Communication, Innovative Business Practices, Smibs Inc., Technology  •  Tagged: Tags:
  1. Forrest February 20, 2009 at 4:00 pm

    I agree that iterators are a huge part of Object-Oriented programming. Most people don’t understand the difference or think it is all semantic. There are subtle differences between the two. Understanding this relationship is one of the differences between good programming and great programming.

Leave comment

NOTE: We’d rather not moderate, but inappropropriate comments may be removed. Repeat offenders will be banned from commenting. Now, let’s focus on adding fun and valuable content. Thank you