Better Software - November 2008 - (Page 16) Code Craft the result type would have to be changed. If returning containers by copy is somewhat questionable, hardwiring the returned container type by reference to an arbitrary internal type is even more suspicious. Let’s amplify this discomfort a little further by following a path taken by many programmers, which is certainly the default path when following this style of class design in languages such as C# and Java. Listing 4 provides read-only access to the internal implementation. Good, except that we are prevented from performing convenient operations that modify the recently used list such as calling clear on the result of list to clear it of all of its entries. We can “solve” this by overloading the list member function so that when called on a plain recently_used_list (as opposed to a const-qualified one), it returns an unqualified reference to a vector. The class in listing 5 now supports the ability to clear a recently used list via the list function. It turns out, however, that this is a pyrrhic design victory. help to drive a nail into the coffin of the vampiric but everpopular setter–getter style, a coding style that is perhaps best characterized as object-oriented assembler. {end} What code have you come across that invites in the vampires? What problems have been caused and how have you been able to address them? Follow the link on the StickyMinds.com homepage to join the conversation. Beware of Vampires To appreciate why we have blown out of the water any encapsulation we may have had, let us briefly consider vampires—specifically, vampire lore as popularized by Hollywood in movies such as The Lost Boys, Blade, and Underworld and in TV series such as Buffy the Vampire Slayer and Angel. How do you protect yourself from vampires? Garlic, holy water, crucifixes, sunlight (often just UV light will do), or a stake through the heart are among the answers normally given. One simple form of protection is often overlooked. To quote from The Lost Boys, “Don’t ever invite a vampire into your house, you silly boy. It renders you powerless.” Without an invitation, a vampire cannot cross the threshold. And our class? Returning a reference to the internal state qualifies as an invitation. The code is saying “Bite me.” The caller, intentionally or otherwise, can now break the good form of the object, violating invariants such as uniqueness. Dependent code is now coupled to and feeding off internal implementation details. So much for private meaning encapsulated. The take-home lessons of this column extend further than just the specifics of the example. They should 16 BETTER SOFTWARE NOVEMBER 2008 www.StickyMinds.com http://www.StickyMinds.com http://www.aculis.com http://www.StickyMinds.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.