rspec/rules/S5261/rule.adoc

42 lines
1.0 KiB
Plaintext
Raw Permalink 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.

== Why is this an issue?
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++;
}
----
== Resources
* https://en.wikipedia.org/wiki/Dangling_else