|
1 | 1 | <h2>Why is this an issue?</h2>
|
2 |
| -<p>Recursion happens when control enters a loop that has no exit. This can happen a method invokes itself, when a pair of methods invoke each other, |
3 |
| -or when <code>goto</code> statements are used to move between two segments of code. It can be a useful tool, but unless the method includes a |
4 |
| -provision to break out of the recursion and <code>return</code>, the recursion will continue until the stack overflows and the program crashes.</p> |
5 |
| -<h3>Noncompliant code example</h3> |
| 2 | +<p>Having an infinite loop or recursion will lead to a program failure or a program never finishing the execution.</p> |
6 | 3 | <pre>
|
7 |
| -int Pow(int num, int exponent) // Noncompliant; no condition under which pow isn't re-called |
| 4 | +public int Sum() |
8 | 5 | {
|
9 |
| - num = num * Pow(num, exponent-1); |
10 |
| - return num; // this is never reached |
| 6 | + var i = 0; |
| 7 | + var result = 0; |
| 8 | + while (true) // Noncompliant: the program will never stop |
| 9 | + { |
| 10 | + result += i; |
| 11 | + i++; |
| 12 | + } |
| 13 | + return result; |
11 | 14 | }
|
12 |
| - |
13 |
| -void WhileLoop() // Noncompliant; no condition under which while loop would exit |
| 15 | +</pre> |
| 16 | +<p>This can happen in multiple scenarios.</p> |
| 17 | +<h3>Loop statements</h3> |
| 18 | +<p><code>while</code> and <code>for</code> loops with no <code>break</code> or <code>return</code> statements and with the exit condition always |
| 19 | +<code>false</code> will be indefinitely executed.</p> |
| 20 | +<h3>"goto" statements</h3> |
| 21 | +<p><code>goto</code> statement with nothing that stops it from being executed over and over again will prevent the program from the completion.</p> |
| 22 | +<h3>Recursion</h3> |
| 23 | +<p>When a <a href="https://en.wikipedia.org/wiki/Recursion_(computer_science)">recursive</a> method call chain lacks an exit condition, the <a |
| 24 | +href="https://en.wikipedia.org/wiki/Call_stack">call stack</a> will reach its limit and the program will crash due to a <a |
| 25 | +href="https://learn.microsoft.com/en-us/dotnet/api/system.stackoverflowexception">StackOverflowException</a>.</p> |
| 26 | +<pre> |
| 27 | +int Pow(int num, int exponent) |
14 | 28 | {
|
15 |
| - while (true) |
16 |
| - { |
17 |
| - var line = Console.ReadLine(); |
18 |
| - Console.WriteLine(line); |
19 |
| - } |
| 29 | + return num * Pow(num, exponent - 1); // Noncompliant: no condition under which Pow isn't re-called |
20 | 30 | }
|
21 |
| - |
22 |
| -void InternalRecursion(int i) |
| 31 | +</pre> |
| 32 | +<p>In this example, <code>Pow</code> will keep calling <code>Pow</code> with <code>exponent - 1</code> forever, until the program crashes with a |
| 33 | +StackOverflowException.</p> |
| 34 | +<p>Recursion provides some benefits.</p> |
| 35 | +<ul> |
| 36 | + <li> <strong>Simplified code</strong>: recursion can often lead to more concise and elegant code by breaking down complex problems into smaller, |
| 37 | + more manageable parts. </li> |
| 38 | + <li> <strong>Improved code readability</strong>: compared to iterative solutions, recursive solutions can be easier to understand and reason about. |
| 39 | + </li> |
| 40 | +</ul> |
| 41 | +<p>However, it has disadvantages as well.</p> |
| 42 | +<ul> |
| 43 | + <li> <strong>Stack overflow</strong>: Recursive functions can lead to <a |
| 44 | + href="https://learn.microsoft.com/en-us/dotnet/api/system.stackoverflowexception">stack overflow</a> if the recursion is too deep, potentially |
| 45 | + causing the program to crash. </li> |
| 46 | + <li> <strong>Performance overhead</strong>: Recursive function calls can lead to poor performance due to the need to push and pop <a |
| 47 | + href="https://en.citizendium.org/wiki/Stack_frame#:~:text=In%20computer%20science%2C%20a%20stack,only%20exist%20at%20run%2Dtime">stack frames</a>, |
| 48 | + making them potentially slower than iterative solutions. </li> |
| 49 | + <li> <strong>Difficulty in debugging</strong>: Debugging recursive code can be challenging, as multiple recursive calls can make it harder to track |
| 50 | + the flow of execution and identify logical errors. </li> |
| 51 | + <li> <strong>Space complexity</strong>: Recursive algorithms may require more memory compared to iterative approaches, as each recursive call adds a |
| 52 | + new frame to the call stack. </li> |
| 53 | + <li> <strong>Lack of control</strong>: Recursion can sometimes lead to infinite loops or unexpected behavior if not properly implemented or |
| 54 | + terminated, making it crucial to have proper base cases and exit conditions. </li> |
| 55 | +</ul> |
| 56 | +<h2>How to fix it</h2> |
| 57 | +<p>The program’s logic should incorporate a mechanism to break out of the control flow loop. Here are some examples.</p> |
| 58 | +<h3>Code examples</h3> |
| 59 | +<ul> |
| 60 | + <li> Use a loop condition which eventually evaluates to <code>false</code> </li> |
| 61 | +</ul> |
| 62 | +<h4>Noncompliant code example</h4> |
| 63 | +<pre data-diff-id="1" data-diff-type="noncompliant"> |
| 64 | +public int Sum() |
23 | 65 | {
|
24 |
| - start: |
25 |
| - goto end; |
26 |
| - end: |
27 |
| - goto start; // Noncompliant; there's no way to break out of this method |
| 66 | + var i = 0; |
| 67 | + var result = 0; |
| 68 | + while (true) // Noncompliant: the program will never stop |
| 69 | + { |
| 70 | + result += i; |
| 71 | + i++; |
| 72 | + } |
| 73 | + return result; |
28 | 74 | }
|
29 | 75 | </pre>
|
30 |
| -<h3>Compliant solution</h3> |
31 |
| -<pre> |
| 76 | +<h4>Compliant solution</h4> |
| 77 | +<pre data-diff-id="1" data-diff-type="compliant"> |
| 78 | +public int Sum() |
| 79 | +{ |
| 80 | + var i = 0; |
| 81 | + var result = 0; |
| 82 | + while (result < 1000) |
| 83 | + { |
| 84 | + result += i; |
| 85 | + i++; |
| 86 | + } |
| 87 | + return result; |
| 88 | +} |
| 89 | +</pre> |
| 90 | +<ul> |
| 91 | + <li> As <a href="https://rules.sonarsource.com/csharp/RSPEC-907">{rule:csharpsquid:S907}</a> generally suggests, avoid using <code>goto</code> |
| 92 | + statements. Instead, you can use a loop statement or explicit recursion. </li> |
| 93 | +</ul> |
| 94 | +<h4>Noncompliant code example</h4> |
| 95 | +<pre data-diff-id="2" data-diff-type="noncompliant"> |
| 96 | +public int Sum() |
| 97 | +{ |
| 98 | + var result = 0; |
| 99 | + var i = 0; |
| 100 | +iteration: |
| 101 | + // Noncompliant: program never ends |
| 102 | + result += i; |
| 103 | + i++; |
| 104 | + goto iteration; |
| 105 | + return result; |
| 106 | +} |
| 107 | +</pre> |
| 108 | +<h4>Compliant solution</h4> |
| 109 | +<pre data-diff-id="2" data-diff-type="compliant"> |
| 110 | +public int Sum() |
| 111 | +{ |
| 112 | + var i = 0; |
| 113 | + var result = 0; |
| 114 | + while (result < 1000) |
| 115 | + { |
| 116 | + result += i; |
| 117 | + i++; |
| 118 | + } |
| 119 | + return result; |
| 120 | +} |
| 121 | +</pre> |
| 122 | +<ul> |
| 123 | + <li> For a recursion make sure there is a base case when the recursive method is not re-called. </li> |
| 124 | +</ul> |
| 125 | +<h4>Noncompliant code example</h4> |
| 126 | +<pre data-diff-id="3" data-diff-type="noncompliant"> |
32 | 127 | int Pow(int num, int exponent)
|
33 | 128 | {
|
34 |
| - if (exponent > 1) // recursion now conditional and stop-able |
35 |
| - { |
36 |
| - num = num * Pow(num, exponent-1); |
37 |
| - } |
38 |
| - return num; |
| 129 | + return num * Pow(num, exponent - 1); // Noncompliant: no condition under which Pow isn't re-called |
39 | 130 | }
|
40 |
| - |
41 |
| -void WhileLoop() |
| 131 | +</pre> |
| 132 | +<h4>Compliant solution</h4> |
| 133 | +<pre data-diff-id="3" data-diff-type="compliant"> |
| 134 | +int Pow(int num, int exponent) |
42 | 135 | {
|
43 |
| - string line; |
44 |
| - while ((line = Console.ReadLine()) != null) // loop has clear exit condition |
| 136 | + if (exponent > 1) // recursion is now conditional and stoppable |
45 | 137 | {
|
46 |
| - Console.WriteLine(line); |
| 138 | + num = num * Pow(num, exponent - 1); |
47 | 139 | }
|
| 140 | + return num; |
48 | 141 | }
|
49 | 142 | </pre>
|
| 143 | +<h2>Resources</h2> |
| 144 | +<h3>Documentation</h3> |
| 145 | +<ul> |
| 146 | + <li> <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement">The "for" |
| 147 | + statement</a> </li> |
| 148 | + <li> <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-while-statement">The "while" |
| 149 | + statement</a> </li> |
| 150 | + <li> <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/jump-statements#the-goto-statement">The "goto" |
| 151 | + statement</a> </li> |
| 152 | + <li> <a href="https://en.wikipedia.org/wiki/Recursion_(computer_science)">Recursion - wiki</a> </li> |
| 153 | + <li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.stackoverflowexception?view=net-7.0">StackOverflowException class</a> </li> |
| 154 | + <li> <a href="https://rules.sonarsource.com/csharp/RSPEC-907">{rule:csharpsquid:S907}: "goto" statement should not be used</a> </li> |
| 155 | +</ul> |
| 156 | +<h3>Articles & blog posts</h3> |
| 157 | +<ul> |
| 158 | + <li> <a href="https://www.cs.utexas.edu/users/EWD/transcriptions/EWD02xx/EWD215.html">Edsger Dijkstra: A Case against the GO TO Statement</a> </li> |
| 159 | +</ul> |
50 | 160 |
|
0 commit comments