Better Software - November 2008 - (Page 14) Code Craft Encapsulation and Vampires by Kevlin Henney Encapsulation—it’s all about declaring data in classes private, right? Well, not quite. Encapsulation has a deeper meaning with more profound implications for your code than just “declare your data private,” although this is what text books and training courses have dumbed it down to mean. Consequently, in considering encapsulation, many programmers look no further than the presence or absence of the keyword “private.” Encapsulation is a matter of degree and sensibility; it is not simply a binary state enabled by sprinkling private over your code. What, then, does it mean to encapsulate? Perhaps the most useful and revealing definition is to be found not in a book on software development but in an ordinary dictionary, the New Oxford Dictionary of English: encapsulate • Enclose (something) in or as if in a capsule. • Express the essential feature of (someone or something) succinctly. • Enclose (a message or signal) in a set of codes which allow use by or transfer through different computer systems or networks. • Provide an interface for (a piece of software or hardware) to allow or simplify access for the user. So encapsulation involves a boundary, the succinct expression of something’s essence as an abstraction, and interposition of an interface to simplify access. Data privacy is simply one effect of encapsulation, but it is not the only one. And, as an effect, it should not be confused with a cause. typedef std::vector recently_used_list; Listing 1 Private Parts Let’s consider an example problem from a previous Code Craft column (“Programming with GUTs” July/August 2008), that of a recently used list. However, rather than use the example as a means of focusing on unit-test quality, let’s take a slightly different journey. For variety, let’s walk through it in C++ rather than C#. Using C++ gives us the excuse to start off in C+ (C++ used as a better C) and allows us to take in a couple of other sights en route. The conclusions drawn apply to the same coding habits when practiced in other languages, but C++ serves to amplify and emphasize the choices made. Listing 2 To start with, we can consider a recently used list to be list. Recently used lists aren’t just arbitrary sequences; they are a sequence of strings. Everyone’s favorite default sequence container is vector, which gives us the modest starting point both stack-like and set-like. They maintain an order of insertion, but they also preserve the uniqueness of their elements. in listing 1. Listing 2 shows a refinement that attempts to take this into The pro: We’ve given the concept a name by which it can be used. The con: It doesn’t actually behave like a recently used account. The pro: We have associated an operation with the struct recently_used_list { void insert(const std::string & most_recent) { std::vector ::iterator found = std::find( elements.begin(), elements.end(), most_recent); if(found != elements.end()) elements.erase(found); elements.insert( elements.begin(), most_recent); } std::vector elements; }; 14 BETTER SOFTWARE NOVEMBER 2008 www.StickyMinds.com ISTOCKPHOTO http://www.nxtbook.com/nxtbooks/sqe/bettersoftware0708/index.php?startid=16 http://www.nxtbook.com/nxtbooks/sqe/bettersoftware0708/index.php?startid=16 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.