Recursion is a powerful tool, but it can be tricky to get right. Getting it wrong can lead to stack overflow errors and cause system problems. Even
when you do get it right, recursive code can be difficult to understand, perhaps leading to maintenance problems in the future. Therefore recursion
should be avoided in general and used only with due deliberation and caution when it is strictly necessary.
This rule checks for direct recursion (when a function calls itself).
Noncompliant code example
int pow(int num, int exponent) {
if (exponent > 1) {
num = num * pow(num, exponent-1); // Noncompliant; direct recursion
}
return num;
}
Compliant solution
int pow(int num, int exponent) {
int val = num;
while (exponent > 0) {
val *= num;
--exponent;
}
return val;
}