Top Banner
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
21

Code Complexity 101

Jun 21, 2015

Download

Software

Arun Saha

Code gets complex sooner than the author/team realizes.

How to keep the complexity in control?

How to measure code complexity *objectively*?
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Code Complexity 101

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

Page 3: Code Complexity 101

(Bitter) Fact of life: Code complexity keeps increasing

What

November 2014 Code Complexity 101 3

Page 4: Code Complexity 101

(Bitter) Fact of life: Code complexity keeps increasing

What

November 2014 Code Complexity 101 4

How to measure code complexity

objectively?

Page 5: Code Complexity 101

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

Page 6: Code Complexity 101

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

Page 7: Code Complexity 101

Informally,

Complexity of a module:

sum of complexities of its functions

November 2014 Code Complexity 101 7

What

Page 8: Code Complexity 101

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

Page 9: Code Complexity 101

FYI

November 2014 Code Complexity 101 9

however, Calling function does not increase complexity of either

the caller or the callee

Page 10: Code Complexity 101

FYI

November 2014 Code Complexity 101 10

however, Calling function does not increase complexity of either

the caller or the callee

Page 11: Code Complexity 101

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

Page 12: Code Complexity 101

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

Page 13: Code Complexity 101

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

Page 14: Code Complexity 101

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

Page 15: Code Complexity 101

Same function can be (correctly) written with different complexities.

FYI

November 2014 Code Complexity 101 15

Page 16: Code Complexity 101

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

Page 17: Code Complexity 101

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

Page 18: Code Complexity 101

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

Page 19: Code Complexity 101

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?

Page 20: Code Complexity 101

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?

Page 21: Code Complexity 101

November 2014 Code Complexity 101 21