Simberon Design Minute
 

MNU Code Generation

There's a principle in software development called the Principle of Least Astonishment. This means that the code shouldn't do something that astonishes the programmer - something that breaks the programmer's basic assumptions about the system. I ran into one of these last week when my co-worker was debugging a problem in a Smalltalk system. For some reason, new methods kept popping up that weren't there before. As it turns out, the class he was debugging intercepted the message not understood exception. In response, it constructed a new method for itself that forwarded the message to an object stored in an instance variable. In effect, this object was a proxy but it built the proxy methods on the fly and added them into the system. The comment in the DNU method said that this method was fun to write. Well, it may have been fun but it violates the Principle of Least Astonishment. Developers don't usually expect methods to be dynamically created at runtime when they aren't understood. It's usually best to stay away from sneaky design tricks like this.

Download