5.5.1 Example

Consider this scenario:

You are building a customer information system. The designers with you want to use information from your system on the client's website -AND- they want to understand the display code and so they can maintian it themselves.

You write a UI class with a 'customers' method that returns a dictionary of all the customer objects. Each customer object has an 'address' method that returns the a dictionary with information about the customer's address. The designers want to be able to access that information.

Using PSP, the display code for the website would look something like the following, assuming your servlet subclasses the class you created for managing customer information:

  <%= self.customer()[ID].address()['city'] %>   (42 chars)

With Cheetah's NameMapper syntax, you can use any of the following:

   $self.customers()[$ID].address()['city']       (39 chars)
   --OR--                                         
   $customers()[$ID].address()['city']           
   --OR--                                         
   $customers()[$ID].address().city              
   --OR--                                         
   $customers()[$ID].address.city                
   --OR--                                         
   $customers[$ID].address.city                   (27 chars)

Which of these would you prefer to explain to the designers, who have no programming experience? The last form is 15 characters shorter than the PSP version and - conceptually - far more accessible. With PHP or ASP, the code would be even messier than with PSP.

This is a rather extreme example and, of course, you could also just implement $getCustomer($ID).city and obey the Law of Demeter (search Google for more on that). But good object orientated design isn't the point of this example.