-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMissedTernaryOpportunity.qhelp
More file actions
36 lines (32 loc) · 1.17 KB
/
MissedTernaryOpportunity.qhelp
File metadata and controls
36 lines (32 loc) · 1.17 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
<!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
better expressed using the ternary <code>?</code> operator.
</p><p>
</p><p>
Use of the ternary operator enhances readability in two ways:</p>
<ul>
<li>
It focuses the reader's attention on the intent of the code (to return or write) rather than the
testing of a condition.</li>
<li>
It is more concise, reducing the amount of code that needs to be read.</li>
<li>
You can initialize a variable conditionally on the line on which it is declared, rather than
assigning to it after initialization. This ensures that you initialize the variable as you intended.</li>
</ul>
</overview>
<recommendation>
<p>Consider using a ternary operator in this situation.</p>
</recommendation>
<example>
<p>The following code includes two examples of <code>if</code> statements, <code>myAbs1</code> and
<code>1</code>, which can be simplified using the ternary operator. <code>myAbs2</code> and <code>s2</code>
show how the statements can be improved.</p>
<sample src="MissedTernaryOpportunity.java" />
</example>
</qhelp>