
Co-authored-by: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com>
52 lines
1.7 KiB
Plaintext
52 lines
1.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The ``++if...else++`` statement is used to make decisions based on the truthiness of a boolean expression, and the ``++if++`` block executes when the expression is ``++true++``, while the ``++else++`` block executes when the expression is false.
|
|
|
|
Wrapping a boolean expression in an ``++if...else++`` statement and returning ``++true++`` or ``++false++`` in the respective blocks is redundant and unnecessary. It can also make the code harder to maintain, as it adds unnecessary lines of code that need to be read and understood.
|
|
|
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
if (expression) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
----
|
|
|
|
Simplify the code and return the boolean expression (or its negation) directly to make the code more concise and easier to read and maintain.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
return expression;
|
|
----
|
|
|
|
If the caller expects a boolean and the result of the expression is not a boolean, use double negation for proper conversion.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
return !!expression;
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean[MDN - Boolean]
|
|
* link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else++[MDN - ``++if...else++``]
|
|
* link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#double_not_!!++[MDN - Double NOT (``++!!++``)]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|