So... You want your code to be maintainable. 41

Posted by Uncle Bob Wed, 25 Jun 2008 04:07:46 GMT

We know that maintenance is 90% of the software lifecycle, and 90% of the cost. We know that our systems need to be flexible, reusable, and maintainable. Indeed, that’s why we spend so much of our time trying to get the design and architecture just right. Because we all know that good design and architecture is the key to flexibility, reusability, and maintainability…right?

Of course. Good design and architecture is what makes software easy to change. Good design and architecture separates the things that change for one reason from the things that change for another reason (The Single Responsibility Principle). Good design allows us to add new features without changing a lot of old code (Open Closed Principle). Good design makes sure that high level policy does not depend on low level detail (Dependency Inversion Principle), etc. etc.

So how do we get good design? Well, that’s tricky. Oh it’s not too tricky to get a good design in place at first. The tricky part is to keep the design good. That’s the problem, you see. It’s not that the design starts out so bad (although sometimes…) rather it is that the design degrades over time as the system changes.

Systems change. Often they change in ways that thwart the original intent of the design. Unfortunately, changing the design to align to these changes is hard. So we wind up hacking the new features into the system and thwarting the design. And that’s how even the best designed systems rot.

So how do we keep the design from rotting? How do we make sure we can migrate the design as the system changes? Simple. Tests.

When you have a suite of tests that covers >90% of the code in the system, you are not afraid to make changes. Every time you make a little change you run those tests, and you know that you have not broken anything. This gives you the confidence to make the next change, and the next, and the next. It gives you the confidence to change the design!

Nothing makes a system more flexible than a suite of tests. Nothing. Good architecture and design are important; but the affect of a robust suite of tests is an order of magnitude greater. It’s so much greater because those tests enable you to improve the design.

This can’t be overstated. If you want your systems to be flexible, write tests. If you want your systems to be reusable, write tests. If you want your systems to be maintainable, write tests.

And write your tests using the Three Laws of TDD.

Comments

Leave a response

  1. Avatar
    Peter Bonney about 10 hours later:

    I agree that tests can provide a strong sense of security when making changes.

    However, they can also have the converse effect, especially when the system is being maintained by programmers other than the original authors.

    If tests need to be edited every time a simple “business as usual” change is made, there can be a strong temptation to ignore the broken tests. And soon, the test suite rots … and design calcifies.

    Thus, comprehensive test coverage may not actually be optimal for future flexibility.

  2. Avatar
    Sebastian Kübeck about 11 hours later:

    @Uncle Bob

    Great post! Really looking forward to read your new book!

    @Peter

    please read:

    http://www.martinfowler.com/bliki/TestCancer.html

    Ignoring broken tests is destroying business value. I recommend not to accept (and pay) for code chanegs from developers that break tests.

  3. Avatar
    unclebob about 13 hours later:

    If tests need to be edited every time a simple “business as usual” change is made, there can be a strong temptation to ignore the broken tests. And soon, the test suite rots … and design calcifies.

    Ignoring broken tests is like ignoring high blood pressure, or severe chest pains, or the “maintenance needed” light on your car’s dashboard. It’s pretty damned foolish. A professional team does not tolerate broken tests.

    Fragile tests are a symptom of a poor design. If your tests break a lot because of small changes, it’s time to change the design of the tests.

  4. Avatar
    yachris about 13 hours later:

    Peter—I’ve seen what you say as an excuse not to comment code, too: “Gee, someone someday is going to change the code and not the comments, and then it all goes to hell, and pretty soon it’s the END OF CIVILIZATION AS WE KNOW IT!!!1!”

    In other words, don’t forget who’s writing - it’s Uncle Bob - he’s advocating an extreme position. The extreme he’s advocating is that people do The Right Thing™ continuously. And we all know that’s utter fantasy. At least where I work… sigh…

  5. Avatar
    Joe about 17 hours later:

    Actually, an extreme position is 100% coverage. 90% is the target in our environment, it is reasonable.

  6. Avatar
    Brett L. Schuchert about 17 hours later:

    Yachris,

    The extreme he’s advocating is that people do The Right Thing™ continuously. And we all know that’s utter fantasy.

    I hear this kind of logic a lot. It is OK if it is not used to do nothing about your situation. However, if you really believe this is the case, then get a new job.

    I don’t know your situation or where you live but I do know there are plenty of IT jobs for the having in several of the big cities. If you do not like the environment and you really don’t think it’s possible to do things better where you work, you really have four options
    • Do nothing (maybe whine and complain, but not anything significant)
    • Try to change the environment
    • Change where you’re working
    • Be subversive and just do things well anyway.

    I’ve seem many places where is it well neigh impossible to get things to change. But in those same environments projects are typically behind schedule, over budget and generally out of control. AND nobody ever seems to get in trouble for it.

    It’s that second thing that makes doing work well possible. Simply do the things you think you should. Make sure the work you do is done well. Write tests for your code. You can and will be able to move faster than your colleagues … even the ones just slapping shit together and checking it in.

    I’ve personally done this before I was a consultant and I’ve seen people do it. It is possible to take advantage of the chaos that exists in many organizations and simply do your job well.

    Note I am not advocating over design or RDD (resume driven development). I am, however, advocating CYAD (cover your add development) where you cover your ass by writing well-written code, backed with tests so that when someones says “your code is broken”, you can either:
    • Discover a missing test, fix it and then not have to worry about that problem again.
    • Show them that in fact it is not broken and the problem is elsewhere.

    Most place (not all) the bark is bigger than the bite. Deadlines, which simply cannot be missed, are regularly missed. “Getting in trouble” really means getting yelled at but not fired (and if the environment is really bad, getting fired might, in the long run, be a good thing).

  7. Avatar
    Rich Sharpe 1 day later:

    “The tricky part is to keep the design good”

    Although I agree this is difficult a large part is Attitude! Both you and Scott Bain, in his book Emergent Design, discuss evolving the Professionalism of our industry. If a developer has the attitude of ‘every time I touch the code, I will make it a bit better’ then they are on the path to keep the design good.

    Of course we do not live in a Utopia and some developers just want to hack code for the pay…and obviously ‘a bit better’ is subjective – but I think you get the point ;-)

  8. Avatar
    tieTYT 3 days later:

    Although I agree with the start (about design) and the finish (about tests), I don’t think tests enforce your design. They just check that your system works correctly. For example, I can have 90% test coverage and still make direct database calls in my view. This is bad design and good tests.

  9. Avatar
    moncler clearance about 1 year later:

    Very quietly I take my leave.To seek a dream in http://www.edhardy-buy.com/ starlight.

  10. Avatar
    chenyan about 1 year later:
  11. Avatar
    authentic nikes over 2 years later:

    I love how you guys always change the mysterious voodoo into common sense

    tests. Thanks SEOMoz for letting CRE tell your story so we could learn how to

    think like human beings instead of bots.

  12. Avatar
    oxpdffr over 2 years later:

    Excel en PDF Convertisseur s’installe dans votre ordinateur comme un imprimante virtuelle, il supporte convertir tous les formats imprimable en document PDF, tels que Excel(xls, xlsx), TXT, les images (JPEG, GIF, PNG, BMP, EMF, EWF, TIFF), etc, compatible avec Microsoft Office 2003/2007/2010. Huit formats de sortie : PS, EPS, PDF, PNG, JPEG, BMP, PCX, et TIFF, compatible avec Adobe Acrobat 9.0.Télécharger gratuitement Excel en PDF Convertisseur et expérimenter ce logiciel.

  13. Avatar
    http://www.blacktowhiteiphone4.com over 2 years later:

    According to a previous source of ours, we have been told that Apple in fact has no plans to release the white model iPhone 4. Our source hypothesizes another "delay" communicated around March leading us into an iPhone 5 release time-frame in June / July.

  14. Avatar
    iPad to mac transfer over 3 years later:

    Thanks for shareing! I agree with you. The artical improve me so much! I will come here frequently. iPad to Mac Transfer lets you transfer music, movie, photo, ePub, PDF, Audiobook, Podcast and TV Show from iPad to Mac or iPad to iTunes.

  15. Avatar
    electric winch over 3 years later:

    Your site is amazing.I am very impressed to see this,i want to come back for visiting your site.Keep doing Good as well as you can.

  16. Avatar
    shaded pold motor over 3 years later:

    Thanks for writing this blog post, it was informative, enjoyable, and most importantly – a good length!

  17. Avatar
    Criminal Check over 3 years later:

    If you want your systems to be flexible, write tests.

  18. Avatar
    Tenant Screening over 3 years later:

    Systems change. Often they change in ways that thwart the original intent of the design. Unfortunately, changing the design to align to these changes is hard. So we wind up hacking the new features into the system and thwarting the design.

  19. Avatar
    Criminal Records over 3 years later:

    I’ve personally done this before I was a consultant and I’ve seen people do it. It is possible to take advantage of the chaos that exists in many organizations and simply do your job well.

  20. Avatar
    okey oyunu oyna over 3 years later:

    good source. Thanks

    internette görüntülü olarak okey oyunu oyna, gerçek kisilerle tanis, turnuva heyecanini yasa.

  21. Avatar
    Monster Beats By Dr. Dre Studio over 3 years later:

    The Monster Beats By Dr. Dre Studio Headphones are classic shoes.You will be proud of owning them. Don’t hesitate!Take Monster Beats By Dr. Dre Pro home! Choose our Monster Beats By Dr. Dre Solo Headphones will make you have an amazing discovery.

  22. Avatar
    Best Phone Lookup over 3 years later:

    I visited this page first time and found it Very Good Job of acknowledgment and a marvelous source of info…......Thanks Admin!

  23. Avatar
    christian louboutin shoes on sale over 3 years later:

    Have the christian louboutin patent leather pumps is a happy thing. Here have the most complete kinds of christian louboutin leather platform pumps.

  24. Avatar
    Air Jordan 10 over 3 years later:

    http://www.realjordanshoessale.com

  25. Avatar
    TeraByte HardDrive over 3 years later:

    Visit here if you wanna TeraByte HardDrive

  26. Avatar
    ?????? over 3 years later:

    ???????????????????? ?????????? ?????????? ?????????????? ?????,?? ?,?? ?,?? ? ??,UGG ,UGG ???,UGG JIMMY CHOO ?

  27. Avatar
    ???? over 3 years later:

    ??888?http://www.shanghaikanpo.com/view/1451.html ???http://www.shanghaikanpo.com/view/1374.html ????:http://www.shanghaikanpo.com/view/1146.html Cialis?http://www.shanghaikanpo.com/view/1146.html ???:http://www.shanghaikanpo.com/view/1065.html ? ???http://www.shanghaikanpo.com/view/1378.html ?????http://www.shanghaikanpo.com/view/1125.html levitra?http://www.shanghaikanpo.com/view/1125.html

  28. Avatar
    ??????????????? over 3 years later:

    ?? ??:http://www.toryburchstore.jp/category-7-b0.html Tory Burch???:http://www.toryburchstore.jp/category-7-b0.html ?? ???:http://www.toryburchstore.jp/category-7-b0.html ?????:http://www.toryburchstore.jp/category-8-b0.html ?? ????:http://www.toryburchstore.jp/category-3-b0.html ?? ? ??:http://www.toryburchstore.jp/category-2-b0.html ?? ?:http://www.toryburchstore.jp/category-6-b0.html ?? ?????:http://www.toryburchstore.jp/category-6-b0.html ???????:http://www.toryburchstore.jp/category-2-b0.html ?? ????:http://www.toryburchstore.jp/category-4-b0.html ????:http://www.toryburchstore.jp/category-5-b0.html

  29. Avatar
    ysbearing over 3 years later:

    Slewing bearing called slewing ring bearings, is a comprehensive load to bear a large bearing, can bear large axial, radial load and overturning moment.

  30. Avatar
    Casque Audio Dr Dre over 4 years later:

    Afin d’exploiter efficacement des beats by dr dre alternatives nouveau marché, vous devez définir exactement ce qu’il est que vous êtes par exemple selling.For, si vous avez des enchères en ce casque monster cable moment un récepteur stéréo Carver, une paire de vieux beats Dr Dre, un ensemble des orateurs étagères Yamaha, et une collection de casque monster beats pas cher disques vinyles, il ya fort à parier que votre créneau est sur le marché de la production musicale .. Jetez un rapide coup d’œil les articles disponibles chez votre fournisseur de gros et de ecouteur monster choses dans votre backstock revente, et de décider où vous vous situez . Ce qui suit est une liste des sites de casque by dre revente les plus populaires disponibles pour vous et une courte description de chacun. Lorsque vous trouvez un (ou peut-être plus d’un) qui travaille pour vous, passez à l’étape 3. Spécialisée dans les ecouteur monster beats produits faits maison et de démarrage, bat pro est la ressource à consulter pour l’acheteur à la recherche d’quelque chose d’original. Aussi un site produit spécifiquement maison, beat by dr dre est une nouvelle alternative avec une base de consommateurs élégant et bohème. http://www.hifibeat.com/

  31. Avatar
    iPhone contacts backup over 4 years later:

    Yes. Of course, every programmer want to keep the code maintainable so they could be read more easily.

  32. Avatar
    Backup iPhone SMS over 4 years later:

    Backup iPhone SMS to computer and you can import the contacts file to email address book or other mobile phone. good luck.

  33. Avatar
    michael kors over 4 years later:

    good post indeed. the logical debugging of varied people is really inspiring. also got lot of information.thanks.

  34. Avatar
    Massagem Tantrica over 4 years later:

    into an iPhone 5 release time-frame in June

  35. Avatar
    bladeless fans over 4 years later:

    So… You want your code to be maintainable. 34 good post6

  36. Avatar
    louboutin sales over 4 years later:

    So… You want your code to be maintainable. 35 hoo,good article!!I like the post!76

  37. Avatar
    sunglasses over 4 years later:

    Wish to bring a trendy look to your everyday ordinary one? Buy sunglasses metal frame sunglasses and add it to your glamour quotient. ray ban 4054 Sunglasses are now a sure to categories you as chic among your purple sunglasses friends. The first thing a youngster chooses in fashionables is a stylish eye gear.

  38. Avatar
    birkin hermes over 4 years later:

    Let’s face it – not all of us have the money power to have a closet full of designer hand bags?

  39. Avatar
    cosplay girl over 4 years later:

    http://www.outfitscosplay.com/cosplay-catalog/avatar-the-last-airbender-cosplay Deluxe Avatar The Last Airbender Cosplay Costumes for Sale.Find your favorite characters and cosplay outfits from all the popular anime and games.

  40. Avatar
    shoes with the red bottom over 4 years later:

    If you have a woman in your life, or you are shopping shoes with the red bottom for yourself, then you probably know all about Coach mens red bottom shoes Handbags. Established in 1941, the Coach Leather Company Christian Louboutin peep toe pumps has been making quality products of all shapes and Christian Louboutin elisa sizes for men and women since its inception. The problem is, of course, the price.

  41. Avatar
    http://sellmonsterheadphones.com over 4 years later:

    Extrêmement régulièrement je vais à ce site. Il est très beaucoup, c’est agréable pour moi. Merci de l’auteur!beats by dre

Comments