65 lines
3.3 KiB
Plaintext

== Why is this an issue?
Bitwise operations are operations that manipulate individual bits in binary representations of numbers. These operations work at the binary level, treating numbers as sequences of 32 bits (in 32-bit environments) or 64 bits (in 64-bit environments). However, they should not be used in a boolean context because they have different behaviors compared to logical operators when applied to boolean values:
* When applied to boolean values, bitwise AND (`++&++`) and OR (``++|++``) perform bitwise operations on the binary representation of the numbers. They treat the operands as 32-bit signed integers and manipulate their individual bits.
* Logical AND (``++&&++``) and OR (``++||++``) are specifically designed for boolean operations. They return a boolean value based on the truthiness or falsiness of the operands.``++&&++`` returns ``++true++`` if both operands are truthy; otherwise, it returns ``++false++``. ``++||++`` operator returns ``++true++`` if at least one of the operands is truthy; otherwise, it returns ``++false++``.
Bitwise operators ``++&++`` and ``++|++`` can be easily mistaken for logical operators ``++&&++`` and ``++||++``, especially for those who are not familiar with the distinction between them or their specific use cases.
This rule raises an issue when ``++&++`` or ``++|++`` is used in a boolean context.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
if (a & b) { /* ... */ } // Noncompliant: The operator & is used in a boolean context
----
You should use the logical variant of the bitwise operator, that is, ``++&&++`` instead of ``++&++`` and ``++||++`` instead of ``++|++``.
[source,javascript,diff-id=1,diff-type=compliant]
----
if (a && b) { /* ... */ }
----
=== Exceptions
When a file contains other bitwise operations, (``++^++``, ``++<<++``, ``++>>>++``, ``++>>++``, ``++~++``, ``++&=++``, ``++^=++``, ``++|=++``, ``++<<=++``, ``++>>=++``, ``++>>>=++``, and ``++&++`` or ``++|++`` used with a numeric literal as the right operand) all issues in the file are ignored, because it is evidence that bitwise operations were truly intended.
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_AND[Bitwise AND (``++&++``)]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR[Bitwise OR (``++|++``)]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND[Logical AND (``++&&++``)]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR[Logical OR (``++||++``)]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Glossary/Truthy[Truthy]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Glossary/Falsy[Falsy]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Review this use of bitwise "x" operator; conditional "xx" might have been intended.
=== Highlighting
the operator
'''
== Comments And Links
(visible only on this page)
=== on 17 Mar 2017, 09:52:25 Elena Vilchik wrote:
\[~ann.campbell.2] Could you update this RSPEC corresponding to SONARJS-928? I've explained in comment the scope of implementation.
=== on 17 Mar 2017, 15:02:08 Ann Campbell wrote:
Done.
endif::env-github,rspecator-view[]