Despite our best intentions, many of us don’t always get to work on the latest and greatest language or technology. In some cases, we may need to make our way around a code base that has been written by another team. Given that the average software system lives for 11 years, it’s probably safe to say that there are, in fact, many such cases where we need to quickly assess a new code base to find its flaws (i.e. risks) and determine which portions of the code may be difficult to maintain and extend and which should be easier. If you’re looking at a code base that was developed by another team or even code you just wrote yesterday, once it’s written, it’s now code that you must maintain.

If you need to analyze a code base and determine which portions of the code are more risky than others, what would you do? For me, I use tools that help me quickly assess the code quality. For instance, I’d like to know something I call the “big five” measures:

  • Code complexity
  • Amount of duplication
  • The resilience of the architecture
  • Whether coding standards are being applied
  • Amount of test code coverage

As I mentioned, I like to use tools to help me assess these measures. However, there are sometimes concerns when people consider using code metrics on their projects:

1) Don’t know which tools to use
2) Can be easily abused
3) Not sure what the data means

The table below links a measure to a tool (.NET and Java), the code smell associated with this measure, the refactoring for the smell and possible patterns that may be applied to refactor the code smell.

Measure Smell Tools Refactoring Pattern
Cyclomatic Complexity Conditional Complexity IDE, CCMetrics, Source Monitor, JavaNCSS, Eclipse Metrics Extract Method, Extract Class, Replace Conditional with Polymorphism Abstract Factory, Strategy
Number of Lines in Method Long Method IDE, PMD, Eclipse Metrics, CheckStyle, Source Monitor, FxCop Extract Method Strategy
Depth of Inheritance (DIT) IDE, PMD, Source Monitor Replace Inheritance with Delegation, Pull Up/Push Down Method Delegation

This is not a detailed list, but should give you some ideas. I plan to add to this list in the future. Do you have additional measures|tools|refactorings|patterns? Please share them with us.

Next time you need to target risky parts of your code base, use these tools to obtain metrics so that you can monitor and improve your code quality (through refactoring and patterns) throughout your development lifecycle.