Friday, July 03, 2009

Name Things Right

In Object Oriented languages, Class represents a definition of what will become an Object at runtime. In some languages (Smalltalk, Ruby) Class can be a runtime Object as well, in others it is more static formula (Java, C#). It always contains the prescription, the recipe. A Class may say: Add two arms, body, head and two legs to create a Person. It can also say that Person can speak.


When software runs, two instances of Person Object are created and they can speak with each other.We try to model the problem domain which is the target of our software as collection of Objects that can interact with each other (i.e. two Persons can speak with each other). This added level of abstraction helps us understand the domain better – we do not primarily focus on the details of sound wave exchange that is human speech, we primarily focus on the fact that two Persons can speak with each other. This way we do not get lost in details from the start, we find a place to put appropriate logic and move on.


The trick to software modeling is the ability to “Name Things Right”. If I can give names to all the actors in my domain I have created all the Classes. Then I decide what the actors (now Classes) can do in form of methods and I’m done creating my domain model. If I can not name my actors, I do not understand the problem domain enough to try to solve it with software. I have to go back and get more information, remodel and refactor. It takes several iterations before the problem domain is solved.


When “Utility Classes” rear their ugly heads: Utility Classes are convenient holder of unrelated methods, often accessed statically. I’m convinced that proliferation of Utility Classes is evil in Object Oriented software. It is clear indication that we have failed to model our domain correctly and started to put methods into one size fits all bag. Strobe lights should go off and sirens should sound every time this happens – just to attract attention. “Name Things Right” and your design will be conducive for beautiful code to happen.



Hat tip to Derek Zoolander for “Name Things Right” inspiration.

No comments: