Solutions Architecture Blog

Wednesday, December 26, 2007 - Posts

Code Reviews and Maintainability
by Mark Strawmyer 12.26.07

Comments    No Comments

Its continually amazing to me how discipline can go a long way to helping ensure a solution has long term maintainability.  Making sure you set parameters and agreed upon standards at the start of a project and then having the mettle to ensure that regardless of how tirelessly you're having to write code you take the time to stay within the parameters.  More often than not when I'm reviewing a solution I see a lack of standards or consistency and excuses such as "there isn't time for it" is almost always the explanation offered or implied.  Or even more shockingly you find an experienced developer that is coding with discipline, but not caring that others aren't and not sharing their best practices.  Open forum peer reviews and discussion of code is something I am a big proponent of in projects especially early on.  It helps ensure that folks are on the same page in terms of style, practices, and that the solution is getting off to the right start.  This is great for reviewing key and common elements of a solution even if it is only practiced early in the project and then abandoned as the project is more mature in the lifecycle.  The downside though is that it isn't realistic to expect that all code can be reviewed by such a process as there is often way too much code.

Enter tools.  Even if there are no explicit standards being followed, there are code analysis and metric tools that can be a big help to making sure the overall solution is more maintainable and performant.  Code analysis will examine the code and look for situations where items may be less than optimal or could be done another way for better performance, maintainability, security, etc.  Common situations are things such as over use of string concatenation where specially designed and class library provided string building objects would be better or inconsistent use of naming conventions.

Code metric tools will examine things such as the overall number of lines of code, depth of inheritence, and cyclomatic complexity which is the number of branches that can be taken within a specific block of code.  The higher the cyclomatic complexity the harder it will be to unit test the code as there are more conditions to test.

There are a number of tools out from vendors such as IBM (Rational), Compuware (DevPartner), and Microsoft (VSTS).  Microsoft recently released the Developer edition of its Visual Studio Team Suite that now includes code metrics and analysis.  I've been experimenting with it and using it to look at a bunch of different code and I've been pretty happy with it and its ease of use and understanding so far.

I highly recommend that developers and development teams adopt a tool to help automate code reviews and perform code analysis and metrics if you're not already.  Even the most disciplined developer will benefit from having an automated review of their code.  It only takes a couple of minutes based on the tool and size of the code base and the results can give you plenty to think about and decide to take action on or not.  Its a great way to learn additional best practices you may not have been aware of by examining the output of the automated review.  Each developer should be responsible for running such as tool against their code and remediating issues prior to having flagged an item as complete.  Again, it is a decision making process as you won't want to blindly make all changes suggested, but such a process prior to turning in code will go a long way to helping overall maintainability.