Pramatr Blog

A collection of articles from pramatr.com on technology, security, software and anything we find interesting

Structured Approaches To Improving Code Quality

Posted by pramatr on August 17th, 2008

Over the past few years, I’ve worked on numerous projects that have grown very quickly but have lacked a structured approach to development. The code is grown somewhat organically, being used in ways never imagined and bent into shapes it was never supposed to make. In the very worst cases, it ended up with Spaghetti Code that is self-obfuscating and unintelligible (even to the original author).

To help prevent this situation, teams need to embrace processes to help introduce structure and discipline to software development. This can be daunting to some teams at first, but it’s a far more productive and appealing than a team that is constantly under-pressure with various fire-fighting activities.

Kent Beck has said previously that he is not a great programmer but just a pretty good programmer with great habits. I’m a firm believer in that statement, and I think that any team that doesn’t try to develop those habits is starting from a weakened position. I’ve worked on both projects that embraced change and tried to improve their development process and also those that didn’t. I would sooner work on the former type of project every time.

With that in mind, there are very simple changes you can make to a project to help foster better habits. None of these techniques is new or groundbreaking (at least anymore), but I am still amazed at the number of projects that still don’t embrace them. There is plenty of information on these techniques out there, so there is no excuse if you want to do some more reading.

  1. Use an automated build system. Whenever you find yourself performing a common task, add it to your build file.
  2. Use code conventions. If everyone has their own style problems are guaranteed. Merging code with differing styles is never going to be fun!
  3. Make automated testing part of the development process. Code developed without tests isn’t finished and can’t be signed off.
  4. Promote Test Driven Development, not everyone will want to do it and not everyone will understand it. Work with the team to understand how it can help their development, focusing more on writing automated tests and less time ad-hoc testing and reviewing log files.
  5. Accept that some people won’t initially buy into automated testing, but ensure that they are writing tests. Whenever they aren’t writing tests, understand why they aren’t doing it. Review the tests that are being written and also measure unit test coverage. Don’t use the coverage report as your only metric though. Anyone can fake up better coverage, focus on writing good tests and the coverage will follow.
  6. Help the team understand different testing techniques. When to use them and when not to!
  7. Refactor code frequently to adapt to changes. Refactor as you go and make it a core part of the development process. Use your IDE’s refactoring support to make things easier.
  8. Don’t ignore warnings in your project, instead treat them as errors. If you continue to ignore them, the project will soon have so many that they will become virtually meaningless.
  9. Enforce your architectural rules in a meaningful way. Long architectural documents might look impressive, but a team needs something more interactive when developing code.
  10. Learn your teams code smells, help your team spot them, fix them and avoid them in future. To improve quality in future, you have to learn from the mistakes of the past.
  11. Prefer simple elegant solutions over complexity. Writing complex code isn’t big or clever. If another developer tries to impress you with some complex solution they’ve just developed, give it a second opinion to see if it actually needs needs to be that complicated. If everyone owns the code, they are just as responsible for it’s maintenance as the original author. Do you really want to maintain something you don’t understand?
  12. Review other developers code frequently. Not only will this give the code a second opinion, but it will ensure that more than one developer has knowledge of the code. Reading other peoples code is a skill in itself, the more code you read the better you become at reading it!
  13. Promote Continuous Integration of code and introduce a Continuous Integration build system. You need to be running the quality reports and tests frequently, otherwise they just won’t get run. You want to know as soon as possible if something is broken. If someone breaks the build, their first priority is to fix it.
  14. Add a PMD task, a PMD/CPD task and a FindBugs task to your build file. Run the tasks frequently and fix up the issues they highlight. Start with a strict rule set and tweak it fit your code base.
  15. Encourage your team to run the tests and review the quality reports before they commit changes. Ensure you review the reports frequently (at least at first).
  16. Encourage your developers with targeted reading. There are plenty of good books out there, let them know which ones they need to be reading to improve their skills.
  17. Learn from people who have been there and done it. There’s nothing wrong with making your own mistakes, but some mistakes are more expensive than others.
  18. Encourage your developers to share! If they’ve read about something that might help your team you want to know about it! The team needs constant feedback to improve and adapt to change.
  19. Don’t try to implement everything at once! Incremental change in your process and code is the best way to succeed. It’s very tempting to fix everything at once, but in my experience it just doesn’t work. People need time to understand the changes you’ve made and the effect it has on their work.
  20. If something isn’t working for your team, don’t bury your head in the sand and ignore it. Discuss it, address it and learn from it! There is no point trying to pretend a process is working if it clearly isn’t, in many ways you might as well have no process at all.

This is by no means meant to be an exhaustive list and as I said previously none of it is new or groundbreaking. Hopefully you’re already doing some (if not all) of these already and you are seeing the benefits they deliver. This is a list that has worked for me in the past, and one that I am currently working through on my current project.

Agile development processes embody many of these best practices and provide an À la carte approach to improving software development. A good reference and starting point is Extreme Programming, there are also many excellent agile mailing lists.

As a final note, it’s vitally important that you remember these techniques are applied to help the team improve their process. I strongly believe there shouldn’t be a finger of blame when the build is broken. Whoever breaks the build fixes it, end of story. If the code quality or coverage reports indicate things have actually gotten worse overtime, discuss with the team what went wrong and why. You need to address these issues as soon as they arise, fix them and move on. If someone isn’t following the process, work with them to try and understand what the problem is. If you can’t get them back on track, Peer pressure can also be a valuable last resort :-) .

  • pramatr
    @mario: Thanks for the recommendations, I actually read Peopleware a couple of weeks ago and loved it. I only wish that I'd read it sooner. Great book!
  • Pramatr
    Thanks Mario. I think the 'human factor' is probably the most important of all. You can't force someone to do something they don't want to do. At least you can't force them to do it and expect them to do a good job. Unless they buy-into what you are trying to achieve, it's going to be pretty hard going.
  • mario.gleichmann
    Nice entry - enjoyed reading!


    I especially liked the points that hinted to the 'human factor' of software development. As you said, processes and tools can only support (but can't enforce) the improvement of code quality. It's even possible to come up with an unstructured or obuscated code base when using an automated build system or continous integration, if not having the 'right' people to do the job (i think i saw this statement in Tom de Marcos 'Peopleware', were a successful Manager was asked about his key decisions in retrospective. He answered: "pick the right people, support them and let them do the job." :o))



    Greetings



    Mario
  • Pramatr
    Thanks Jörg, I thought if there's one thing the world needs it's another blog :-).

    The beauty of these simple processes is you can adopt them in a piecemeal fashion. If you aren't doing any of them, then there's lots of room for improvement. Either way it's about incremental change and finding out what works for your team. If you try and introduce too many things too quickly it's very hard to get buy-in to the changes.

    I've worked on projects that didn't incorporate any processes and seen how big the improvements can be when you add structure and discipline. These projects spent most of the time fire fighting, bug fixing, counter bug fixing and debugging. Long term, this just doesn't work!
  • joerg20
    Welcome to the world of blogging :) I really like your list. It's really good. If you are working on a project nearly ignoring any of the recommendations (as I do at the moment) you end up with at least 80% of your time spent to debugging, 15% trying to figure out how to fix it and 5% at maximum doing actual code changes.Regards,Joerg
blog comments powered by Disqus