Better Software - March 2008 - (Page 15) Code Craft Connection*con = net_connect(…); if (con) { int err_code = use_net(con); net_close(db); if (err_code) // Report network error } else { // Report connection failure } Listing 3 Connection*con = net_connect(…); use_net(con); // May throw net_close(db); // May throw Listing 4 Connection*con = net_connect(…); try { use_net(con); // May throw net_close(db); } catch (…) { net_close(db); } Listing 5 // May throw Suppose you have a linked list of objects of some type. How would an early 1990s programmer traverse it? The traListing 6 ditional solution consisted of a pair of “first-next” functions, something like listing 9. Not too shabby, but why should we reveal that we’re usConnection con(…); // An object; not a pointer ing pointers to traverse the list? What if we change our mind use_net(con); later and use some other mechanism? Users shouldn’t have to change the code when we change our implementation. Listing 7 Another downside to this solution is that users can have only one active traversal at a time, since the list itself void deleter(Connection* p) { does the advancing. Such a container isn’t sure what net_close(p); kind of abstraction it is: Does it hold things or does } it traverse things? To do both violates the important design principle of cohesion: Abstractions should do shared_ptr con(new Connection(…), &deleter); one thing well. A better design moves the notion of use_net(con.get()); traversal into its own abstraction: an iterator. In current terminology, the Iterator design patListing 8 tern reifies sequence traversal. To reify is to make a www.StickyMinds.com MARCH 2008 BETTER SOFTWARE try { use_net(con); } finally { net_close(db); } // May throw There are still more abstractions out there, however. If you were programming in the late 1980s to early 1990s when object-oriented programming was starting to catch on, you were probably using C++. When writing a network application, you probably still had to use a C-like API since vendors were slow to catch up with the OO revolution. Listing 3 shows some typical code. The function use_net must check for errors on every call to the network API. In the case of an error, it returns an error code that you have to pass on to your caller—after closing the connection. Very tedious. Then along came exceptions to make the job easier by letting errors propagate up to the caller. The code simplifies considerably, as shown in listing 4. Oops! If use_net fails, the connection is left open. No problem—you can catch the exception on its way up, like in listing 5. But now you have to close the database on each possible path. Yuck. As shown in listing 6, Java makes it a little easier. But there is still code here that you shouldn’t have to write. A better solution uses deterministic destruction, wherein the connection is encapsulated in a class with a destructor that closes the database, like in listing 7. Now you’re down to two simple lines—code that is easier to understand and easier to test. The C++ term for this important practice is “resource acquisition is initialization” (RAII), which suggests its usage pattern: acquire resources in constructors, release them in destructors. Languages in common use that support this idiom include C++, D, and C#. Even if you have to create a connection on the heap, you can use C++’s new shared_ptr as an RAII wrapper, as the code in listing 8 illustrates. No matter how execution leaves this scope, the shared_ptr destructor calls deleter, which closes the connection. There’s no need for if statements, a finally clause, or explicit exception handling. Reification, Patterns, and System Architecture 15 http://www.StickyMinds.com
Table of Contents Feed for the Digital Edition of Better Software - March 2008 Better Software - March 2008 Contents Mark Your Calendar Contributors eLightenment Technically Speaking Code Craft Test Connection Management Chronicles Cover Story: Breaking Ground On SOA Software Development Worst Practices Mind the Gap Product Announcements 10 Things You Might Not Know About... The Last Word Ad Index Better Software - March 2008 Better Software - March 2008 - (Page Intro) Better Software - March 2008 - Better Software - March 2008 (Page Cover1) Better Software - March 2008 - Better Software - March 2008 (Page Cover2) Better Software - March 2008 - Better Software - March 2008 (Page 1) Better Software - March 2008 - Better Software - March 2008 (Page 2) Better Software - March 2008 - Contents (Page 3) Better Software - March 2008 - Mark Your Calendar (Page 4) Better Software - March 2008 - Mark Your Calendar (Page 5) Better Software - March 2008 - Contributors (Page 6) Better Software - March 2008 - Contributors (Page 7) Better Software - March 2008 - eLightenment (Page 8) Better Software - March 2008 - eLightenment (Page wp1) Better Software - March 2008 - eLightenment (Page wp2) Better Software - March 2008 - eLightenment (Page 9) Better Software - March 2008 - eLightenment (Page 10) Better Software - March 2008 - eLightenment (Page 11) Better Software - March 2008 - eLightenment (Page 12) Better Software - March 2008 - Technically Speaking (Page 13) Better Software - March 2008 - Code Craft (Page 14) Better Software - March 2008 - Code Craft (Page 15) Better Software - March 2008 - Code Craft (Page 16) Better Software - March 2008 - Code Craft (Page 17) Better Software - March 2008 - Test Connection (Page 18) Better Software - March 2008 - Test Connection (Page 19) Better Software - March 2008 - Management Chronicles (Page 20) Better Software - March 2008 - Management Chronicles (Page 21) Better Software - March 2008 - Management Chronicles (Page 22) Better Software - March 2008 - Management Chronicles (Page 23) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 24) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 25) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 26) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 27) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 28) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 29) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 30) Better Software - March 2008 - Cover Story: Breaking Ground On SOA (Page 31) Better Software - March 2008 - Software Development Worst Practices (Page 32) Better Software - March 2008 - Software Development Worst Practices (Page 33) Better Software - March 2008 - Software Development Worst Practices (Page 34) Better Software - March 2008 - Software Development Worst Practices (Page 35) Better Software - March 2008 - Software Development Worst Practices (Page 36) Better Software - March 2008 - Software Development Worst Practices (Page 37) Better Software - March 2008 - Mind the Gap (Page 38) Better Software - March 2008 - Mind the Gap (Page 39) Better Software - March 2008 - Mind the Gap (Page 40) Better Software - March 2008 - Mind the Gap (Page 41) Better Software - March 2008 - Mind the Gap (Page 42) Better Software - March 2008 - Mind the Gap (Page 43) Better Software - March 2008 - Mind the Gap (Page 44) Better Software - March 2008 - Product Announcements (Page 45) Better Software - March 2008 - 10 Things You Might Not Know About... (Page 46) Better Software - March 2008 - The Last Word (Page 47) Better Software - March 2008 - Ad Index (Page 48) Better Software - March 2008 - Ad Index (Page Cover3) Better Software - March 2008 - Ad Index (Page Cover4)
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.