Code Complexity 101 Arun Saha, Ph.D. arunksaha AT gmail DOT com (A pragmatic programmer and a software craftsman) https://www.linkedin.com/in/arunksaha
Jun 21, 2015
Code Complexity 101
Arun Saha, Ph.D.
arunksaha AT gmail DOT com
(A pragmatic programmer and a software craftsman)
https://www.linkedin.com/in/arunksaha
Complexity Kills
What
November 2014 Code Complexity 101 2
Sphaghetti Junction
(Bitter) Fact of life: Code complexity keeps increasing
What
November 2014 Code Complexity 101 3
(Bitter) Fact of life: Code complexity keeps increasing
What
November 2014 Code Complexity 101 4
How to measure code complexity
objectively?
Simple Objective Metric: Cyclomatic Complexity
What
November 2014 Code Complexity 101 5
Developed by Thomas J. McCabe Sr. in 1976 Also known as McCabe Complexity
Definition and Theory: http://en.wikipedia.org/wiki/Cyclomatic_complexity
Informally,
Complexity of a function: # of independent control paths
The minimum is 1, when there is exactly one path.
November 2014 Code Complexity 101 6
What
Informally,
Complexity of a module:
sum of complexities of its functions
November 2014 Code Complexity 101 7
What
Complexity of a function: # of independent control paths
How
November 2014 Code Complexity 101 8
Approximately,
Sum of occurrence of the conditionals that influence the control flow.
For example, in C and C++, each of the following keywords increase the complexity by 1
return, if, for, while, &&, ||, ?:, case,
goto, break, continue
FYI
November 2014 Code Complexity 101 9
however, Calling function does not increase complexity of either
the caller or the callee
FYI
November 2014 Code Complexity 101 10
however, Calling function does not increase complexity of either
the caller or the callee
Example 1
// Two functions of complexity 1
int c1_direct( int x ) {
return x + 1;
}
int c1_redirect( int x ) {
int const result = add( x, 1 );
return result;
}
E.g.
November 2014 Code Complexity 101 11
Example 2
// complexity: 2
// 1 for “return”, 1 for “?:”
int c2_min( int x, int y ) {
return y < x ? y : x;
}
E.g.
November 2014 Code Complexity 101 12
Example 3
// complexity: 3
// 1 for “if”, 1 for each “return”
int c3_min( int x, int y ) {
if( y < x ) {
return y;
}
else {
return x;
}
}
E.g.
November 2014 Code Complexity 101 13
Example 4
// complexity: 3
// 1 for “return”, 1 for “&&”, 1 for “?:”
int c4_bothtrue( int x, int y ) {
return (x && y) ? 1 : 0;
}
E.g.
November 2014 Code Complexity 101 14
Same function can be (correctly) written with different complexities.
FYI
November 2014 Code Complexity 101 15
Example 5.1
Problem BothNonZero: Given two int’s, return
non-zero if both are non-zero, zero otherwise.
// BothNonZero: Approach 1, Complexity: 4
int bothNonZero_1( int x, int y ) {
if( x ) {
if( y ) {
return 1;
}
}
return 0;
}
E.g.
November 2014 Code Complexity 101 16
Example 5.2
// BothNonZero: Approach 2, Complexity: 4
int bothNonZero_2( int x, int y ) {
if( x && y ) {
return 1;
}
return 0;
}
E.g.
November 2014 Code Complexity 101 17
Example 5.3
// BothNonZero: Approach 3, Complexity: 2
int bothNonZero_3( int x, int y ) {
return x && y;
}
E.g.
November 2014 Code Complexity 101 18
FYI
November 2014 Code Complexity 101 19
A common application is to compare the measured complexity against a set of threshold values. One such
threshold set is as follows
Cyclomatic Complexity Risk Evaluation
1 – 10 A simple program, without much risk
11 – 20 More complex, moderate risk
21 – 50 Complex, high risk program
51 + Unstable program (very high risk)
Source: http://www.sei.cmu.edu/reports/97hb001.pdf p. 145
Fine, but how complex is really complex?
FYI
November 2014 Code Complexity 101 20
I use CCCC http://sourceforge.net/projects/cccc/files/cccc/3.1.4/
with my simple wrapper mccabe.sh
(available at) https://github.com/arunksaha/complexity (contains the function examples used here)
Great, how can I get started today?
November 2014 Code Complexity 101 21