|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | +"-//Semmle//qhelp//EN" |
| 3 | +"qhelp.dtd"> |
| 4 | +<qhelp> |
| 5 | + <overview> |
| 6 | + <p> |
| 7 | + |
| 8 | + Defensive code can prevent unforeseen circumstances from |
| 9 | + causing fatal program behaviors. |
| 10 | + |
| 11 | + A common defensive code pattern is to guard |
| 12 | + against dereferencing the values <code>null</code> or |
| 13 | + <code>undefined</code>. |
| 14 | + |
| 15 | + However, if the situation that some defensive code guards |
| 16 | + against never can occur, then the defensive code serves no purpose and |
| 17 | + can safely be removed. |
| 18 | + |
| 19 | + </p> |
| 20 | + |
| 21 | + </overview> |
| 22 | + <recommendation> |
| 23 | + |
| 24 | + <p> |
| 25 | + |
| 26 | + Examine the surrounding code to determine if the defensive |
| 27 | + code is worth keeping despite providing no practical use. If it is no |
| 28 | + longer needed, remove it. |
| 29 | + |
| 30 | + </p> |
| 31 | + |
| 32 | + </recommendation> |
| 33 | + <example> |
| 34 | + |
| 35 | + <p> |
| 36 | + |
| 37 | + The following example shows a <code>cleanupLater</code> |
| 38 | + function that asynchronously will perform a cleanup task after some |
| 39 | + delay. When the cleanup task completes, the function invokes the |
| 40 | + provided callback parameter <code>cb</code>. |
| 41 | + |
| 42 | + To prevent a crash by invoking <code>cb</code> when it |
| 43 | + has the value <code>undefined</code>, defensive code guards |
| 44 | + the invocation by checking if <code>cb</code> is truthy. |
| 45 | + |
| 46 | + </p> |
| 47 | + |
| 48 | + <sample src="examples/UnneededDefensiveProgramming1_bad.js" /> |
| 49 | + |
| 50 | + <p> |
| 51 | + |
| 52 | + However, the <code>cleanupLater</code> function is always |
| 53 | + invoked with a callback argument, so the defensive code condition |
| 54 | + always holds, and it is therefore not |
| 55 | + required. The function can therefore be simplified to: |
| 56 | + |
| 57 | + </p> |
| 58 | + |
| 59 | + <sample src="examples/UnneededDefensiveProgramming1_good.js" /> |
| 60 | + |
| 61 | + <p> |
| 62 | + |
| 63 | + Guarding against the same situation multiple times is |
| 64 | + another example of defensive code that provides no practical use. The |
| 65 | + example below shows a function that assigns a value to a property of |
| 66 | + an object, where defensive code ensures that the assigned value is not |
| 67 | + <code>undefined</code> or <code>null</code>. |
| 68 | + |
| 69 | + </p> |
| 70 | + |
| 71 | + <sample src="examples/UnneededDefensiveProgramming2_bad.js" /> |
| 72 | + |
| 73 | + <p> |
| 74 | + |
| 75 | + However, due to coercion rules, <code>v == |
| 76 | + undefined</code> holds for both the situation where <code>v</code> |
| 77 | + is<code>undefined</code> and the situation where <code>v</code> |
| 78 | + is<code>null</code>, so the <code>v == null</code> |
| 79 | + guard serves no purpose, and can be removed: |
| 80 | + |
| 81 | + </p> |
| 82 | + |
| 83 | + <sample src="examples/UnneededDefensiveProgramming2_good.js" /> |
| 84 | + |
| 85 | + </example> |
| 86 | + <references> |
| 87 | + |
| 88 | + <li>Wikipedia: <a href="https://en.wikipedia.org/wiki/Defensive_programming">Defensive programming</a>.</li> |
| 89 | + |
| 90 | + </references> |
| 91 | +</qhelp> |
0 commit comments