52 lines
1.8 KiB
Plaintext
Raw Normal View History

== 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 truthy, while the ``++else++`` block executes when the expression is falsy.
2020-06-30 12:47:33 +02:00
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.
2021-02-02 15:02:10 +01:00
[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]
2020-06-30 12:47:33 +02:00
----
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]
2020-06-30 12:47:33 +02:00
----
return !!expression;
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean[Boolean]
* MDN web docs - link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else++[``++if...else++``]
* MDN web docs - link:++https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT#double_not_!!++[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[]