|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionCyclomatic Code Complexity was first introduced by Thomas McCabe in 1976. In 1976, Thomas McCabe published a paper arguing that code complexity is defined by its control flow. Since that time, others have identified different ways of measuring complexity (e.g. data complexity, module complexity, algorithmic complexity, call-to, call-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. OverviewThis measure provides a single ordinal number that can be compared to the complexity of other programs. It is one of the most widely accepted static software metrics and is intended to be independent of language and language format. Code Complexity is a measure of the number of linearly-independent paths through a program module and is calculated by counting the number of decision points found in the code (if, else, do, while, throw, catch, return, break etc.). Technical SpecificationCyclomatic Complexity for a software module calculated based on graph theory is based on the following equation: CC=E-N+p
Where
Further academic information on the specifics of this can be found here. From a layman’s perspective the above equation can be pretty daunting to comprehend. Fortunately there is a simpler equation which is easier to understand and implement by following the guidelines shown below:
Let’s look at a few examples to understand how the code complexity is calculated. Example 1public void ProcessPages()
{
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 Example 2public int getValue(int param1)
{
int value = 0;
if (param1 == 0)
{
value = 4;
}
else
{
value = 0;
}
return value;
}
In the code above, we start with 1 for the routine, add 1 for the Members that have high code complexity should be reviewed for possible refactoring. The SEI provides the following basic risk assessment based on the value of code:
ToolsThere are several free tools available which help one analyze the code complexity:
Advantages
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||