-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissedTernaryOpportunity.qhelp
More file actions
25 lines (22 loc) · 1.03 KB
/
MissedTernaryOpportunity.qhelp
File metadata and controls
25 lines (22 loc) · 1.03 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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>An <code>if</code> statement where both branches do nothing but return or write to a variable can
be difficult to read. It takes up a lot of lines of code and, in the case of assignment, requires
the declaration of the variable to be on a different line to the two assignments. It also does not
adequately express the intent of the programmer to assign or return a value based on a condition.</p>
</overview>
<recommendation>
<p>This pattern can be better expressed using the ternary (<code>?</code>) operator. This solves all
the above problems by making shorter code that is easier to read and better expresses the intent of
the programmer.</p>
</recommendation>
<example>
<p>In this example the <code>if</code> statement controls only what is returned by the method.</p>
<sample src="MissedTernaryOpportunity.cs" />
<p>It could be expressed a lot more simply using the ternary operator.</p>
<sample src="MissedTernaryOpportunityFix.cs" />
</example>
</qhelp>