Sunteți pe pagina 1din 17

Cyclomatic Complexity

Where we learn to count…

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 1
Modularization with Sanity
•  Problem:
–  How can we divide design into modules such that each module
is of equal, or near equal complexity?
–  How can we make sure modules aren’t too complex?
•  Why?
–  Complex modules are prone to error
–  Complex modules require many tests
–  Complex modules are harder to understand and to modify

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 2
Modularization with Sanity
•  Solution:
–  Establish a measure of complexity
–  Evaluate options using measure
–  Select option that results in near equal measures for each
module, and less than some threshold
•  Sounds simple, but what is a good measure?
–  How can we be certain that a 5 is more complex than a 4?

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 3
Enter…Graph Theory
•  Thomas McCabe (1976):
–  Graph theory offers insight into complexities in graphs
–  CyclomaticNumber v(G) is defined for strongly
connected graphs
–  Strongly connected graph is one where every vertex
is connected to every other vertex

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 4
So what is v(G)?
•  V(G) = e –n + p
–  e = edges
–  n = vertices
–  p = connected components
(usually 1)
•  Here
–  e = 9
–  n = 6
–  p = 1
–  v(G) = 4
University of Victoria SENG 330: Object-Oriented Software
Department of Computer Science Development
Cyclomatic Complexity: Slide 5
Cyclomatic Complexity Defined
•  CC = e – n + 2p
•  Corresponds to adding
an edge from end to
beginning
•  Measures # of linearly
independent paths through
source code
•  Here CC = 5
University of Victoria SENG 330: Object-Oriented Software
Department of Computer Science Development
Cyclomatic Complexity: Slide 6
So what are those nodes & edges?

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 7
You Try

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 8
You Try

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 9
You Try

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 10
An Alternate Method
•  CC = number of
closed loops + 1
•  Equivalent to
counting the
regions

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 11
You Try

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 12
So what does it all mean?
•  Magic number is......10

•  Work to keep cyclomatic complexity to 10


or less
–  If it exceeds 10 look for point around where it
equals 10 and try to set up as module

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 13
Let’s try some examples

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 14
You Try
1 int euclid (int m, int n) {
int r;
2 if (n > m) {
3 r = m;
4 m = n;
5 n = r;
6 }
7 r = m % n;
8 while (r !=0) {
9 m = n;
10 n = r;
11 r = m % n;
12 }
13 return n;
14 }
University of Victoria SENG 330: Object-Oriented Software
Department of Computer Science Development
Cyclomatic Complexity: Slide 15
You Try

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 16
Probability of Errors in S/W

University of Victoria SENG 330: Object-Oriented Software


Department of Computer Science Development
Cyclomatic Complexity: Slide 17

S-ar putea să vă placă și