-
Notifications
You must be signed in to change notification settings - Fork 1
Code Complexity
-
Factors that make code difficult to understand. This includes length, syntactical elements, data flow patterns, variable names and more. [Ajami]
-
McCabe’s Cyclomatic Complexity: The complexity of a sequence of steps is equivalent to the cyclomatic complexity of the strongly connected equivalent of its control flow graph. [MCC]
-
Code Complexity Measuring Tool: Using Cognitive Code Complexity (CCC), the following metrics are evaluated to calculate the code complexity: size, type of control structures, nesting level of control structures, inheritance level of statements, try-catch blocks, recursive calls, array declarations, compound conditions, and input/output statements. [CCMT]
-
Institute of Electrical and Electronic Engineers: The extent to which the design or implementation of a system or component obstructs comprehension and verification. [IEEE]
-
Halstead Software Science: Effort required in implementing or understanding the program. It is directly proportional to difficulty and volume. [Onyango]
- Many more definitions exist, but these are the most applicable.
1. Naive:
Stop the time it takes for multiple developers to accurately interpret a snippet of code. [Ajami]
2. McCabe’s Cyclomatic Complexity:
[MCC]
Generate a control flow graph
$V(G) = e-n+1$
An alternative to this is measuring the predicates (or decisions) p in the code. Predicates can consist of more than one condition (e.g. if C1 and C2).
$V(G) = p + 1$ .
The established norm for calculation the cyclomatic complexity in software is based on this concept, but slightly different in its execution.
A node
Basic cyclomatic complexity:
$V(G) = e-n+2p$
Counting decision points:
$V(G) = p + 1$
Summing up predicate nodes:
$V(G) = p + 1$
Specifics of decision points:
- every
if
andelse if
is a decision point. Theelse
is not a decision point. - a loop setup
while
orfor
is a decision point. They can be interpreted asif -> continue loop
andelse -> exit loop
- nested
if
statements count as individual decision points [Marilyn]
example:
if(A and B): do something
else: do something different
would result in
$V(G)=2$ , since$p=1$
but
if(A):
if(B):do something
else: do nothing
else: do something different
would result in
$V(G)=3$ , since$p=2$
There exist some counting rules for common code constructs that can help determine
3. The CCC Measure:
$= 0$ for sequential statements,
$= 1$ for decision-making control statements like if.. then .. else,
$= 2$ for decision making control statements like while .. do, for loop, do .. while,
$= n$ for switch statement with n cases [Chhillar]
$= 0$ for statements inside the outermost level of inheritance,i.e inside the base class,
$= 1$ for statements inside the next level of inheritance, i.e first derived class,
$= 2$ for statements inside the next deeper level of inheritance, i.e next derived class and so on. [Chhillar]
$+=1$ for every occurrence oftry
$+=1$ for every occurrence ofcatch
[CCMT]
$ for first recursive call
$+=2$ for each additional recursive call of the same method [CCMT]
$W_a:=$ Weight of array
$S_{ad}:=$ Size of array dimension [CCMT]
for
and
andor
the equal weight as the control structure they appear in.
$+=1$ for each input/output statement
5. Halstead Software Science:
$\eta1 =$ number of (distinct) unique operators ({}
,=
,==
,;
,while()
,/
,:
,if()
,else
,return
,+
, ...)
$\eta2 =$ number of (distinct) unique operands (main()
,int
,print()
,function_name
,variable_name
,1
,0
, `',...)
$N1 =$ total number of operators
$N2 =$ total number of operands
sum of (distinct) unique operands and (distinct) unique operators
number of unique operators and total usage of operands
- Some automated measuring Tools exist, like the Complexity Measurement Tool (CMT) and Code Metrics (CM), but these are not applicable for all programming languages.[Ogunrinde]
Ajami MCC CCMT(Page 101 to 112) IEEE Chhillar Onyango Ogunrinde(Page 55-64) Marilyn