Clean Code and Object-oriented dishwashers
30 March 2013, by Chris Harris
We thought we’d share this creative moan from a colleague (who shall remain nameless!)
Apparently we are all very used to dealing with nicely written classes with great encapsulation and no feature envy, and it seems we must be in the habit of expecting that sort of thing. I need to point out that sadly not all objects in the office are designed this way, and we need to behave defensively. The dishwasher is an example. At the moment we are not always safely interacting with its API.
As you may have experienced, if you attempt to add a dirty item to a dishwasher in a “washing” state, it will throw a HotSteamingWaterCominAtchaException. HOWEVER: if you attempt to add a dirty item to a dishwasher in a state containing entirely clean items, IT WILL NOT THROW. You will silently put the dishwasher into an invalid state, leading to wasted washing effort, wasted manual sorting effort, lack of dishwasher space and an untidy chill-out. Needless to say, the correct interaction is to empty the dishwasher first, or if response time is critical, use a different dishwasher instance from the pool.
No matter how carefully you have designed your code behaviour, the dishwasher might not be in the state you expect, since it is a shared resource. Please always defensively check the state of the dishwasher before you interact with it.