Home Segments Index Top Previous Next

188: Mainline

A fifth virtue of procedure abstraction is that you can improve how a computation is done. You might decide, for example, that it is wasteful for your fatToCalories method to multiply out self and 9 twice. Accordingly, you might decide to do the computation just once, using a variable, named result, to hold onto the value:

Integer method definition • instance 
fatToCalories 
  | result |  
  result := self * 9. 
  Transcript show: 'The number of fat calories computed is '; 
             show: result printString; 
             cr. 
  ^ result 

Again, you do not need to find all the places where calories are computed from fat grams; you need to change only the definition of the fatToCalories method.