-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCCyclomaticComplexity.qhelp
More file actions
40 lines (35 loc) · 2.23 KB
/
CCyclomaticComplexity.qhelp
File metadata and controls
40 lines (35 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>This metric measures the number of linearly independent execution paths through methods.
Linearly independent paths are calculated using the control flow diagram for a piece of code. A
linearly independent path includes at least one new edge that has not been included in any linearly
independent path counted so far. Methods with a high cyclomatic complexity can be difficult to
understand and difficult to test because there are so many different ways they could execute.</p>
<p>Consider this method:</p>
<sample src="CCyclomaticComplexity.cs" />
<p>The control flow diagram for through this method looks like this:</p>
<img src="CCyclomaticComplexity.png" alt="Control Flow Diagram" />
<p>The first linearly independent path through this method is one where the condition for every
branch point returns false. The path through the diagram would go Start → count>10 →
timesLeft>0 → End. There is also another path where the first condition returns true and
hence "The count is large" is printed. This counts as a second independent path because it adds the
edges from count>10 → "The count is large" → timesLeft>0 which have not been
included in the first path we counted. Likewise the second condition being true would add another
independent path through the switch statement and one of its cases. The switch statement has 4
cases that count as paths however one has already been counted in the previous path. As such the
switch statement adds another 3 independent paths through the method bringing the total cyclomatic
complexity to 6.</p>
</overview>
<recommendation>
<p>Complex methods should have parts of their functionality extracted to helper methods. This makes
testing easier because each helper method can be tested individually.</p>
</recommendation>
<references>
<li>Wikipedia. <a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic complexity</a>.</li>
<li>Wikipedia. <a href="http://en.wikipedia.org/wiki/Control_flow_diagram">Control flow diagram</a>.</li>
<li>Wolfram MathWorld. <a href="http://mathworld.wolfram.com/LinearlyIndependent.html">Linearly Independent</a>.</li>
</references>
</qhelp>