-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathStatementNestingDepth.qhelp
More file actions
56 lines (42 loc) · 1.29 KB
/
StatementNestingDepth.qhelp
File metadata and controls
56 lines (42 loc) · 1.29 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
A method that contains a high level of nesting can be very difficult to understand. As noted in
[McConnell], the human brain cannot easily handle more than three levels of nested <code>if</code>
statements.</p>
</overview>
<recommendation>
<p>
Extract nested statements into new methods, for example by using the 'Extract Method' refactoring
from [Fowler].</p>
<p>
For more ways to reduce the level of nesting in a method, see [McConnell].
</p>
<p>
Furthermore, a method that has a high level of nesting often indicates that its design can be
improved in other ways, as well as dealing with the nesting problem itself.
</p>
</recommendation>
<example>
<p>
In the following example, the code has four levels of nesting and is unnecessarily difficult to read.
</p>
<sample src="StatementNestingDepth.java" />
<p>
In the following modified example, some of the nested statements have been extracted into a new method
<code>PrintAllCharInts</code>.
</p>
<sample src="StatementNestingDepthGood.java" />
</example>
<references>
<li>
M. Fowler, <em>Refactoring</em>, pp. 89-95. Addison-Wesley, 1999.
</li>
<li>
S. McConnell, <em>Code Complete</em>, 2nd Edition, §19.4. Microsoft Press, 2004.
</li>
</references>
</qhelp>