Original URL: http://www.theregister.co.uk/2008/05/20/emergent_design_part_five/
Book extract, part five Scott Bain’s book, Emergent Design: The Evolutionary Nature of Professional Software Development by Addison Wesley, looks at the principles involved in building and maintaining robust, reliable, and cost-effective code. In this, our concluding extract, Scott identifies the pathologies in code when coupling (http://www.regdeveloper.co.uk/2008/04/18/emergent_design_part_three/), cohesion (http://www.regdeveloper.co.uk/2008/04/07/emergent_design_part_two/), and redundancy (http://www.regdeveloper.co.uk/2008/05/05/emergent_design_part_four/) have not been adhered to.
It is important to know the qualities you want your code to have, but it is also important to empower yourself with a set of indicators that tell you that you are heading in the wrong direction. Ideally, I would like to know I am doing something I should not do before I have done very much of it. Also, we often are called upon to evaluate other people's code, or to revisit our own code from the recent or distant past.
Pathologies help us here. In truth, code, design, and system pathologies could be the subject of an entire book, but there are a few really high-leverage indicators that we can use as a basic set. Not surprisingly, they tie into the qualities that I listed earlier: coupling, cohesion, and eliminating redundancy.
Here are some indicators of weak cohesion:
A student once told me a story that is pretty illustrative and also kind of funny. He was given a class to refactor. Someone had written the class in C#, but clearly did not know much about object orientation. The class worked fine (the author had been, obviously, a skilled programmer), but nobody could or would touch it. It was, essentially, one huge method.
He started by doing what I would probably do; he just scanned through the code without really reading it thoroughly, just to determine how tough this job was going to be. He was, of course, being asked how long this was going to take.
As he scanned along, hitting Page Down over and over, suddenly all the code disappeared from the screen. A few more page downs revealed blank screen after blank screen, and then suddenly the code was back again.
This happened several times. As he paged down through the code, sometimes for pages at a time, there would be nothing but a blank page, and then for pages at a time he would see code again.
He had been working all day, so he suspected that he may have corrupted the memory buffer of his IDE (Visual Studio, in this case). He restarted it. The same problem happened again, so he rebooted. Then he cold started. He checked out a fresh copy of the code from source-control. Same problem.
Now, he really hit the old "wall of voodoo". He figured it must be something strange, like a problem in the operating system or the installation of the IDE. He was, in fact, very close to taking a pretty drastic step and reinstalling either or both of these things.
Then he noticed how small the horizontal scroll button was at the bottom of his IDE.
The problem was actually very simple: the code was so complex, and had to do so much procedurally, that the nesting from the accumulating tabs in the code often pushed all the code off screen to the right, sometimes for pages at a time, until the conditional branching, looping, try/catches, and so forth all closed and the code drifted back to the right.
It was not good news (lots of work to do), but at least he knew it was not his machine. It was a problem with the code, and it was obviously very weakly cohesive.
Here are some examples of accidental or illogical coupling.
Here are some indicators of redundancy.
My boss and mentor Alan Shalloway puts it this way: There are three numbers in software: 0, 1, and infinity. 0 represents the things we do not do in a system (we do those for free). 1 represents the things we do once and only once. But at the moment we do something twice, we should treat it as infinitely many and create cohesive services that allow it to be reused.
It is helpful to be surrounded by smart people like Boon and Alan, and it is also an advantage of being part of a profession. Other professionals are your resources, and you are theirs.
I teach different things: design patterns, systems architecture, unit testing, test-driven development, and so forth. I've noticed that no matter what the topic is, so long as it is part of software development, I have to start by making sure that everyone understands the qualities in this chapter.
Of course, many do. However, it's very common to find that two people are using a word like cohesion very differently; or that one person thinks of coupling very simply (it's coupled or it isn't), whereas another has more depth (the different types of coupling).
I don't expect to have taught you anything new or earthshaking here (but if I have, so much the better!). The purpose here was to make sure you're emphasizing the same things I am, and that we're using these terms in the same way, and with the same depth.
Now, we have a foundation to build.
This chapter is excerpted from the new book, Emergent Design: The Evolutionary Nature of Professional Software Development by Scott Bain, published by Addison-Wesley Professional, March 2008 ISBN 0-321-50936-6 Copyright (c) 2008 Pearson Education, Inc. For more information, please see informIT.com (http://www.informit.com/store/product.aspx?isbn=0321509366) and Register Books (http://books.theregister.co.uk/catalog/browse.asp?id=878089&advert=878089review).
Read, test, don't repeat - how to avoid code complexity (5 May 2008)
http://www.theregister.co.uk/2008/05/05/emergent_design_part_four/
Hafta Man and the threat to agile design (30 April 2008)
http://www.theregister.co.uk/2008/04/30/hafta_mindsets/
Coupling and the power of accidental relationships (18 April 2008)
http://www.theregister.co.uk/2008/04/18/emergent_design_part_three/
Rethink code cohesion (7 April 2008)
http://www.theregister.co.uk/2008/04/07/emergent_design_part_two/
Comment judiciously, refactor if needed, avoid the 'f' word (28 March 2008)
http://www.theregister.co.uk/2008/03/28/case_for_comments_code/
The secret to evolutionary code (24 March 2008)
http://www.theregister.co.uk/2008/03/24/emergent_design_part_one/
Sweet, sweet smell of comments in code? (18 March 2008)
http://www.theregister.co.uk/2008/03/18/code_comment_rows/
Extreme Programming in risky position, says co-creator (13 March 2008)
http://www.theregister.co.uk/2008/03/13/web_development_needs_method/
10 ways to improve your code (10 March 2008)
http://www.theregister.co.uk/2008/03/10/ten_ways_to_improve_code/
Telltale signs your model is stuck (3 January 2008)
http://www.theregister.co.uk/2008/01/03/analysis_paralysis_part_two/
Sequence diagramming that's fit for purpose (22 December 2007)
http://www.theregister.co.uk/2007/12/22/iconix_process_sequence_diagramming/
Ten tips on agile software development (22 November 2007)
http://www.theregister.co.uk/2007/11/22/ten_agile_tips/
XP guru to developers: 'Shape up' (8 November 2007)
http://www.theregister.co.uk/2007/11/08/beck_xp_shape_up/
Designing software for testability (29 October 2007)
http://www.theregister.co.uk/2007/10/29/design_for_testability/
Calling the PHP cowboys in from the range (15 October 2007)
http://www.theregister.co.uk/2007/10/15/zend/
© Copyright 2008