rspec/rules/S5261/rule.adoc
2022-02-04 16:28:24 +00:00

40 lines
1.0 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

The dangling ``++else++`` problem appears when nested ``++if++``/``++else++`` statements are written without curly braces. In this case, ``++else++`` is associated with the nearest ``++if++`` but that is not always obvious and sometimes the indentation can also be misleading.
This rules reports ``++else++`` statements that are difficult to understand, because they are inside nested ``++if++`` statements without curly braces.
Adding curly braces can generally make the code clearer (see rule S121 ), and in this situation of dangling ``++else++``, it really clarifies the intention of the code.
== Noncompliant Code Example
[source,text]
----
if (a)
if (b)
d++;
else // Noncompliant, is the "else" associated with "if(a)" or "if (b)"? (the answer is "if(b)")
e++;
----
== Compliant Solution
[source,text]
----
if (a) {
if (b) {
d++;
}
} else { // Compliant, there is no doubt the "else" is associated with "if(a)"
e++;
}
----
== See
* https://en.wikipedia.org/wiki/Dangling_else