When code goes bad: What to watch for
Emergent Design: Pathologies uncovered
Indicators of redundancy
Here are some indicators of redundancy.
- Redundant changes. When you find yourself making the same change in multiple places, clearly the thing you are changing is redundant. However, because people tend to think of redundancy only in terms of code and state, they can miss this indicator. Here is a very common example: imagine that the code new Widget() appears in several places in your code, because you instantiate that class in many places. Now imagine that the constructor of Widget changes. Or, that Widget becomes an abstract class, with multiple subclasses that represent different forms of the Widget. You will have to make the change(s) in every place where the code new Widget() appears. Does that mean you had a redundancy? Of course. You are probably thinking: "But wait! I do that all the time!" If you are thinking that, you are not alone, and it is a fairly common source of maintenance headaches.
- Inheriting from the Clipboard. That phrase comes from a student of mine named Boon. She always said that if you copy code from one place and paste it into another, you are obviously creating redundant code. She calls it "inheriting from the Clipboard," which I think it pretty clever and memorable. When you copy code, ask yourself why you are doing it - and if the same operation is needed in two places, doesn't this indicate that you need a service in your system?
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 and Register Books.