
A few months back I was reading a blog entry by Darren Hobbs entitled, A bad citizen in Javaland. It is a really creative description of a day in the life of an object. This particular object is a bad citizen because it violates some basic rules about good object oriented code. For example, when the object climbs on top of a car like it is a motorcycle it is trying to cast an abstract object to the wrong concrete type. When the object reaches under the hood, grabs the engine and reaches into the engine to grab the starter. The object is violating encapsulation and not using the given interface, in this case the ignition switch, to start the car.
Thinking about good objects vs. bad, it strikes me that to a certain extent an object can only be as good as the classes it depends on. Just like adding one good citizen to a maximum security prison does not a utopia make, one beautiful and well tested class will not make a large codebase much better (although you have to start somewhere). As I considered relationships and dependencies I went back to Darren's excellent description of Javaland, and I couldn't help but try my hand at writing a sequel. Here goes. Sorry Darren.
It only takes a few bad citizens to spoil things in Javaland where everyone depends on someone else. Children in Javaland depend on their parents. It is a parent's responsibility to keep dangerous objects out of reach, and yet the newspapers and log files of Javaland are filled with stories of lazy parents who leave dangerous objects within reach of their children. Just yesterday a friend was telling me about a deadbeat parent that had left a gun lying around. The children got hold of it, and boom! Tragedy. *Shakes head* You can hardly blame the children for parents that aren't mindful about dangerous objects. And then there was the story of the online shopkeeper who swindled hundreds of upstanding citizens in Javaland. The shopkeeper had a link on his website, "click here for a NEW cake pan. Only $5.99" It was too good a deal to pass up, but although the shopkeeper was promising a new cake pan, he was actually selling the same cake pan over and over. Every cake pan was the same cake pan, so when one person poured cake batter into the pan, everyone's cake pan was filled with batter. Imagine walking home from the post office with your new cake pan. The smell of a baking cake rises out of the box. Out of curiosity you open it and are surprised to find a half baked cake. By the time you get home the cake has finished, frosted itself, and the pieces are going fast! Citizens all over Javaland are baking and eating out of the same cake pan! These citizens can't be blamed for the transgressions of one bad shopkeeper, but so often the one bad citizen starts the decay of the others. A higher power notices the inconsistency in the state of cakes in Javaland. Rather lamely, subtle changes to the fabric of reality give each citizen the ability to have exclusive access to the cake pan. Hundreds of citizens are now queuing up to use the one pan. Since there is no other form of sustenance in Javaland, people begin to starve. As more citizens buy the 5.99 cake pan a famine strikes Javaland. Subtle changes to the nature of the universe now dictate that cakes in Javaland can be baked twice as fast. Catastrophe is averted, for now...
No comments:
Post a Comment