Simberon Design Minute
 

Base Class Extensions

What do you do if you create a new method that naturally belongs as a method in a base class? Perhaps you want to add a method to String to verify that the parentheses in the String are properly matched. The question is, where do you put the code for this method? In some environments like Java, you don't have the option to change the base class. You need to create your own StringUtilities class and put the operation there. In Smalltalk, it's normally better to put the method directly into the String class but make it an extension that's managed by your own packages. This method should ideally be an non-core method. In other words, it shouldn't directly access the representation of String to get its work done. It works by just calling other public methods on String. If that method needs other helper methods and starts to pass lots of parameters around, you should consider using a helper class that does the work and put the class into the same package as the extension method. A helper class can change parameters into instance variables of a temporary object which are easier to manage.

Download