In many object oriented languages, there's a standard method that provides a string representation of an object. In Smalltalk, it's printOn: while in Java and C# it's toString. One thing to keep in mind about these methods is that they're intended to explain the object to the programmer, not the end user. You should make sure that the string representation clearly indicates what class of object this is. That's because this string is used in the development tools and things like logging tools and tracebacks. Nothing is more confusing than when an object prints itself out in a way that makes it look like another kind of object. I sometimes see the printOn: method be delegated directly to an instance variable. If this instance variable happens to be nil, then the object prints itself out as nil which can really lead you down the wrong path when you're debugging. If you want a string to use in the user interface, use displayString or something similar. Leave the printOns for the programmers.
Download