-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNestedLoopsSameVariable.qhelp
More file actions
30 lines (25 loc) · 1.13 KB
/
NestedLoopsSameVariable.qhelp
File metadata and controls
30 lines (25 loc) · 1.13 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Inner loops should not write to the iteration variables of the enclosing loop. If an inner loop
changes the iteration variable of the enclosing loop this is normally because the programmer
mistyped the iteration variable of the inner loop. If this is the intended behavior, then this is
an example of bad practice. Loops should be designed so that each loop controls its own iteration.
Nested loops where iteration is shared between loops are more difficult to understand and maintain.
</p>
</overview>
<recommendation>
<p>If there is a mistake in the code then correct it. If the behavior is intended then consider
redesigning the way that iteration of the loops is controlled. This will make the code easier for
other programmers to understand and maintain.</p>
</recommendation>
<example>
<p>This example will run forever because the inner loop increments i instead of j.</p>
<sample src="NestedLoopsSameVariable.cs" />
</example>
<references>
<li>MSDN: <a href="http://msdn.microsoft.com/en-us/library/ch45axte.aspx">for</a>.</li>
</references>
</qhelp>