|
1 |
| -<!DOCTYPE qhelp PUBLIC |
2 |
| - "-//Semmle//qhelp//EN" |
3 |
| - "qhelp.dtd"> |
4 |
| -<qhelp> |
5 |
| - |
6 |
| -<overview> |
7 |
| -<p>There are a number of Boolean expression patterns that can easily be rewritten |
8 |
| -to make them simpler. |
9 |
| -Boolean expressions involving comparisons with Boolean literals, |
10 |
| -ternary conditionals with a Boolean literal as one of the results, |
11 |
| -double negations, or negated comparisons can all be changed to |
12 |
| -equivalent and simpler expressions.</p> |
13 |
| -</overview> |
14 |
| - |
15 |
| -<recommendation> |
16 |
| -<p>If <code>A</code> and <code>B</code> are expressions of Boolean type, you can |
17 |
| -simplify them using the rewrites shown below.</p> |
18 |
| - |
19 |
| -<table><tbody> |
20 |
| -<tr><th>Expression</th><th></th><th>Simplified expression</th></tr> |
21 |
| -<tr><td><code>A == true</code></td><td></td><td><code>A</code></td></tr> |
22 |
| -<tr><td><code>A != false</code></td><td></td><td><code>A</code></td></tr> |
23 |
| -<tr><td><code>A == false</code></td><td></td><td><code>!A</code></td></tr> |
24 |
| -<tr><td><code>A != true</code></td><td></td><td><code>!A</code></td></tr> |
25 |
| -<tr><td><code>A ? true : B</code></td><td></td><td><code>A || B</code></td></tr> |
26 |
| -<tr><td><code>A ? B : false</code></td><td></td><td><code>A && B</code></td></tr> |
27 |
| -<tr><td><code>A ? B : true</code></td><td></td><td><code>!A || B</code></td></tr> |
28 |
| -<tr><td><code>A ? false : B</code></td><td></td><td><code>!A && B</code></td></tr> |
29 |
| -<tr><td><code>A ? true : false</code></td><td></td><td><code>A</code></td></tr> |
30 |
| -<tr><td><code>A ? false : true</code></td><td></td><td><code>!A</code></td></tr> |
31 |
| -<tr><td><code>!!A</code></td><td></td><td><code>A</code></td></tr> |
32 |
| -<tr><td><code>A && true</code></td><td></td><td><code>A</code></td></tr> |
33 |
| -<tr><td><code>A || false</code></td><td></td><td><code>A</code></td></tr> |
34 |
| -</tbody></table> |
35 |
| - |
36 |
| -<p>Some expressions always yield a constant value. If the side-effect in |
37 |
| -<code>A</code> is intended, consider restructuring the code to make this more clear. |
38 |
| -Otherwise, replace the expression with the constant value as shown below.</p> |
39 |
| - |
40 |
| -<table><tbody> |
41 |
| -<tr><th>Expression</th><th></th><th>Value</th></tr> |
42 |
| -<tr><td><code>A && false</code></td><td></td><td><code>false</code></td></tr> |
43 |
| -<tr><td><code>A || true</code></td><td></td><td><code>true</code></td></tr> |
44 |
| -<tr><td><code>A ? true : true</code></td><td></td><td><code>true</code></td></tr> |
45 |
| -<tr><td><code>A ? false : false</code></td><td></td><td><code>false</code></td></tr> |
46 |
| -</tbody></table> |
47 |
| - |
48 |
| -<p>In addition to the rewrites above, negated comparisons can also be simplified in the following way:</p> |
49 |
| - |
50 |
| -<table><tbody> |
51 |
| -<tr><th>Expression</th><th></th><th>Simplified expression</th></tr> |
52 |
| -<tr><td><code>!(A == B)</code></td><td></td><td><code>A != B</code></td></tr> |
53 |
| -<tr><td><code>!(A != B)</code></td><td></td><td><code>A == B</code></td></tr> |
54 |
| -<tr><td><code>!(A < B)</code></td><td></td><td><code>A >= B</code></td></tr> |
55 |
| -<tr><td><code>!(A > B)</code></td><td></td><td><code>A <= B</code></td></tr> |
56 |
| -<tr><td><code>!(A <= B)</code></td><td></td><td><code>A > B</code></td></tr> |
57 |
| -<tr><td><code>!(A >= B)</code></td><td></td><td><code>A < B</code></td></tr> |
58 |
| -</tbody></table> |
59 |
| - |
60 |
| -</recommendation> |
61 |
| - |
62 |
| -<example> |
63 |
| -<p> |
64 |
| -In the following example, the properties <code>Espresso</code>, <code>Latte</code>, and <code>Grande</code> |
65 |
| -are written in a complex way and can be simplified. |
66 |
| -</p> |
67 |
| - |
68 |
| -<sample src="SimplifyBoolExprBad.cs" /> |
69 |
| - |
70 |
| -<p>The code below shows the same logic expressed in a simpler and more readable way.</p> |
71 |
| - |
72 |
| -<sample src="SimplifyBoolExprGood.cs" /> |
73 |
| -</example> |
74 |
| - |
75 |
| -<references> |
76 |
| - |
77 |
| -<li> |
78 |
| -Microsoft C# Reference: |
79 |
| -<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/logical-negation-operator">! Operator</a>, |
80 |
| -<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-comparison-operator">== Operator</a>, |
81 |
| -<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/not-equal-operator">!= Operator</a>, |
82 |
| -<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator">&& Operator</a>, |
83 |
| -<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator">|| Operator</a>, |
84 |
| -<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator">?: Operator</a>, |
85 |
| -<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/less-than-operator">< Operator</a>. |
86 |
| -</li> |
87 |
| - |
88 |
| -</references> |
89 |
| - |
90 |
| -</qhelp> |
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | + "-//Semmle//qhelp//EN" |
| 3 | + "qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | + |
| 6 | +<overview> |
| 7 | +<p>There are a number of Boolean expression patterns that can easily be rewritten |
| 8 | +to make them simpler. |
| 9 | +Boolean expressions involving comparisons with Boolean literals, |
| 10 | +ternary conditionals with a Boolean literal as one of the results, |
| 11 | +double negations, or negated comparisons can all be changed to |
| 12 | +equivalent and simpler expressions.</p> |
| 13 | +</overview> |
| 14 | + |
| 15 | +<recommendation> |
| 16 | +<p>If <code>A</code> and <code>B</code> are expressions of Boolean type, you can |
| 17 | +simplify them using the rewrites shown below.</p> |
| 18 | + |
| 19 | +<table><tbody> |
| 20 | +<tr><th>Expression</th><th></th><th>Simplified expression</th></tr> |
| 21 | +<tr><td><code>A == true</code></td><td></td><td><code>A</code></td></tr> |
| 22 | +<tr><td><code>A != false</code></td><td></td><td><code>A</code></td></tr> |
| 23 | +<tr><td><code>A == false</code></td><td></td><td><code>!A</code></td></tr> |
| 24 | +<tr><td><code>A != true</code></td><td></td><td><code>!A</code></td></tr> |
| 25 | +<tr><td><code>A ? true : B</code></td><td></td><td><code>A || B</code></td></tr> |
| 26 | +<tr><td><code>A ? B : false</code></td><td></td><td><code>A && B</code></td></tr> |
| 27 | +<tr><td><code>A ? B : true</code></td><td></td><td><code>!A || B</code></td></tr> |
| 28 | +<tr><td><code>A ? false : B</code></td><td></td><td><code>!A && B</code></td></tr> |
| 29 | +<tr><td><code>A ? true : false</code></td><td></td><td><code>A</code></td></tr> |
| 30 | +<tr><td><code>A ? false : true</code></td><td></td><td><code>!A</code></td></tr> |
| 31 | +<tr><td><code>!!A</code></td><td></td><td><code>A</code></td></tr> |
| 32 | +<tr><td><code>A && true</code></td><td></td><td><code>A</code></td></tr> |
| 33 | +<tr><td><code>A || false</code></td><td></td><td><code>A</code></td></tr> |
| 34 | +</tbody></table> |
| 35 | + |
| 36 | +<p>Some expressions always yield a constant value. If the side-effect in |
| 37 | +<code>A</code> is intended, consider restructuring the code to make this more clear. |
| 38 | +Otherwise, replace the expression with the constant value as shown below.</p> |
| 39 | + |
| 40 | +<table><tbody> |
| 41 | +<tr><th>Expression</th><th></th><th>Value</th></tr> |
| 42 | +<tr><td><code>A && false</code></td><td></td><td><code>false</code></td></tr> |
| 43 | +<tr><td><code>A || true</code></td><td></td><td><code>true</code></td></tr> |
| 44 | +<tr><td><code>A ? true : true</code></td><td></td><td><code>true</code></td></tr> |
| 45 | +<tr><td><code>A ? false : false</code></td><td></td><td><code>false</code></td></tr> |
| 46 | +</tbody></table> |
| 47 | + |
| 48 | +<p>In addition to the rewrites above, negated comparisons can also be simplified in the following way:</p> |
| 49 | + |
| 50 | +<table><tbody> |
| 51 | +<tr><th>Expression</th><th></th><th>Simplified expression</th></tr> |
| 52 | +<tr><td><code>!(A == B)</code></td><td></td><td><code>A != B</code></td></tr> |
| 53 | +<tr><td><code>!(A != B)</code></td><td></td><td><code>A == B</code></td></tr> |
| 54 | +<tr><td><code>!(A < B)</code></td><td></td><td><code>A >= B</code></td></tr> |
| 55 | +<tr><td><code>!(A > B)</code></td><td></td><td><code>A <= B</code></td></tr> |
| 56 | +<tr><td><code>!(A <= B)</code></td><td></td><td><code>A > B</code></td></tr> |
| 57 | +<tr><td><code>!(A >= B)</code></td><td></td><td><code>A < B</code></td></tr> |
| 58 | +</tbody></table> |
| 59 | + |
| 60 | +</recommendation> |
| 61 | + |
| 62 | +<example> |
| 63 | +<p> |
| 64 | +In the following example, the properties <code>Espresso</code>, <code>Latte</code>, and <code>Grande</code> |
| 65 | +are written in a complex way and can be simplified. |
| 66 | +</p> |
| 67 | + |
| 68 | +<sample src="SimplifyBoolExprBad.cs" /> |
| 69 | + |
| 70 | +<p>The code below shows the same logic expressed in a simpler and more readable way.</p> |
| 71 | + |
| 72 | +<sample src="SimplifyBoolExprGood.cs" /> |
| 73 | +</example> |
| 74 | + |
| 75 | +<references> |
| 76 | + |
| 77 | +<li> |
| 78 | +Microsoft C# Reference: |
| 79 | +<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/logical-negation-operator">! Operator</a>, |
| 80 | +<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-comparison-operator">== Operator</a>, |
| 81 | +<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/not-equal-operator">!= Operator</a>, |
| 82 | +<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator">&& Operator</a>, |
| 83 | +<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator">|| Operator</a>, |
| 84 | +<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator">?: Operator</a>, |
| 85 | +<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/less-than-operator">< Operator</a>. |
| 86 | +</li> |
| 87 | + |
| 88 | +</references> |
| 89 | + |
| 90 | +</qhelp> |
0 commit comments