mirror of
https://github.com/github/codeql.git
synced 2026-02-23 18:33:42 +01:00
59 lines
1.9 KiB
XML
59 lines
1.9 KiB
XML
<!DOCTYPE qhelp PUBLIC
|
|
"-//Semmle//qhelp//EN"
|
|
"qhelp.dtd">
|
|
<qhelp>
|
|
<overview>
|
|
<p>
|
|
Loop bodies and the 'then' and 'else' branches of <code>if</code> statements can either be block
|
|
statements delimited by curly braces, or simple statements. In the latter case, special care
|
|
must be taken to correctly indent statements to indicate whether or not they belong to the body
|
|
of the loop or the <code>if</code> statement. In particular, the statement immediately after
|
|
the loop or <code>if</code> statement should not be indented by the same amount as the body
|
|
to avoid misunderstanding of the control flow structure.
|
|
</p>
|
|
|
|
</overview>
|
|
<recommendation>
|
|
|
|
<p>
|
|
Use additional indentation to set loop bodies and then/else branches apart, but use the same
|
|
amount of indentation for statements that follow each other in a sequence of statements.
|
|
</p>
|
|
|
|
</recommendation>
|
|
<example>
|
|
|
|
<p>
|
|
In this example, the 'then' branch of the <code>if</code> statement consists of the single
|
|
statement <code>scream();</code>. Indentation makes it appear as if the statement
|
|
<code>runAway();</code> also belongs to the 'then' branch, while in fact it does not: it is
|
|
simply the next statement after the <code>if</code>, and will be executed regardless of
|
|
whether the condition <code>afraid()</code> evaluates to true or false.
|
|
</p>
|
|
|
|
<sample src="examples/MisleadingIndentationAfterControlStmt.js" />
|
|
|
|
<p>
|
|
If both statements were intended to be part of the 'then' branch, they should be enclosed
|
|
in a block of statements like this:
|
|
</p>
|
|
|
|
<sample src="examples/MisleadingIndentationAfterControlStmtGood.js" />
|
|
|
|
<p>
|
|
If the second statement does not logically belong in the 'then' branch, its indentation
|
|
should be decreased like this:
|
|
</p>
|
|
|
|
<sample src="examples/MisleadingIndentationAfterControlStmtGood2.js" />
|
|
|
|
</example>
|
|
<references>
|
|
|
|
|
|
<li>Tutorialzine: <a href="http://tutorialzine.com/2014/04/10-mistakes-javascript-beginners-make/">10 Mistakes That JavaScript Beginners Often Make</a>.</li>
|
|
|
|
|
|
</references>
|
|
</qhelp>
|