Guys,
There are two key areas that we focus on when choosing code for review:
Functionality that is important to the proper operation of the system (e.g. core frameworks, unique algorithms, performance-critical code, etc.).
Code that has a high complexity
As an example, most of our data types inherit from a base type that provides a lot of common functionality. Because of its placement in the hierarchy, it is important that our base type functions in a consistent, reliable, and expected manner. Example of core functionality that is required for correct system operation. For other code, we need to rely on code complexity measurements.
Having said that how do we analyse code complexity and how its going to be one of the objective of the code review process?
If this is the question on your mind, here is one of the way you can opt for.
Cyclomatic Code Complexity Analysis ( Microsoft.NET Applications )
There are several ways to measuring complexity (data complexity, module coupling, algorithmic complexity, calls-to and called-by, etc.. ). Although these other methods are effective in the right context, it seems to be generally accepted that control flow is one of the most useful measurements of complexity, and high complexity scores have been shown to be a strong indicator of low reliability and frequent errors.
Cyclomatic Code Complexity is based on the Tom McCabe's work and is defined by the codes control flow.
Start with 1 for the straight path through the routine
Add 1 for each of the following keywords or their equivalents: if, while, repeat, for, and, or
Add 1 for each case in a case statement
So, if we have this C# example:
while (nextPage != true)
{
if ((lineCount <= linesPerPage) && (status != Status.Cancelled) && (morePages == true))
{
// ...
}
}
In the code above, we start with 1 for the routine, add 1 for the while, add 1 for the if, and add 1 for each && for a total calculated complexity of 5. Anything with a greater complexity than 10 or so is an excellent candidate for simplification and refactoring. Minimizing complexity is a great goal for writing high-quality, maintainable code.
Some advantages of McCabe's Cyclomatic Complexity include:
It is very easy to compute, as illustrated in the example
Can be computed immediately in the development lifecycle (which makes it friendly)
It provides a good indicator of the ease of code maintenance
It can help focus testing efforts
It makes it easy to find complex code for formal review
It is important to note that a high complexity score does not automatically mean that code is bad. However, it does highlight areas of the code that have the potential for error. The more complex a method is, the more likely it is to contain errors, and the more difficult it is to completely test. Now check the Project/SEI risk assessment matrix for value of code , and decide which code needs refactoring.
Good luck and hope this works for you and your team.
Source: Fowler, Martin. 1999. Refactoring: Improving the Design of the Existing Code Boston: Addison-Wesley. (refer this book for more details)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment