/cognitive-complexity
if
statements) and structural complexity (e.g., nested loops or conditions), it aids in pinpointing exact code segments that contribute disproportionately to overall complexity.
/elseif-without-else
if ... else if
chain ends with an else
block, enforcing a default action. This makes sure that all possible branches of execution are accounted for, improving the reliability of the code.if ... else if
chain is not met and no default action is specified. This is particularly useful in scenarios like input validation or branching based on variable values, where overlooking a condition can lead to unintended consequences.else
clause. It acts as a self-documenting feature indicating that the developer has considered all possible outcomes. This clarity is beneficial for future code reviews and maintenance, ensuring that every decision branch is concluded with a purposeful action.if ... else if
sequence. Without an else
clause, diagnosing why none of the specified conditions were met can be more challenging and time-consuming./max-switch-cases
/no-all-duplicated-branches
/no-collapsible-if
/no-collection-size-mischeck
.length
for arrays, .size
for Sets, etc.), thereby teaching or reinforcing correct usage patterns to developers, especially those who may be less familiar with the nuances of JavaScript or the specific collection types being used./no-duplicate-string
/no-duplicated-branches
/no-element-overwrite
/no-empty-collection
/no-extra-arguments
arguments
object, avoiding unnecessary arguments can prevent undue processing and memory usage, especially in critical performance paths or in functions called frequently./no-gratuitous-expressions
/no-identical-conditions
/no-identical-expressions
password === password
in a password check is clearly a mistake and should be password === confirmPassword
.
/no-identical-functions
/no-ignored-return
checkInventory
function, where ignoring the return value could lead to making decisions without considering critical information.
checkInventory
could result in attempting to process an order for a product that is out of stock, leading to inconsistencies in application state or user experience.
/no-inverted-boolean-check
/no-nested-switch
switch
statements considerably reduce the readability of the code. This rule helps in maintaining a cleaner, more understandable codebase by preventing deep nesting which can make logic flow hard to follow, especially for new developers or when revisiting code after some time.
switch
statements, this rule encourages the use of separate functions or strategies to handle complex conditional logic. This separation simplifies debugging and testing individual components of the logic, as well as making the codebase more maintainable by isolating changes to specific functions.
switch
cases, increase the complexity and cognitive load required to understand the code. By avoiding nested switches, this rule helps in maintaining a flatter code structure, which is generally easier to work with and less prone to errors during modifications or additions to the codebase.
/no-nested-template-literals
/no-one-iteration-loop
/no-redundant-boolean
if (isUserActive === true)
to if (isUserActive)
makes the condition easier to read and understand at a glance.if (isUserActive)
are less prone to mistakes than more complex ones like if (isUserActive === true)
, especially for inexperienced developers or in complex codebases./no-redundant-jump
return;
at the end of a function or a redundant continue;
in the last loop iteration, which can improve code readability by removing superfluous lines. This makes it easier for developers to understand the flow of the program without getting distracted by unnecessary statements.
return;
and continue;
statements, it aligns with the principle of writing clear and purposeful code. This adherence to best practices can be especially important in collaborative environments, where consistent coding standards facilitate easier code reviews and integration.
/no-same-line-conditional
/no-small-switch
switch
statements only when they manage a considerable number of cases. This prevents misuse for scenarios better served by if-else
constructs, maintaining cleaner and more intuitive code.
if-else
) for scenarios with fewer cases. This minimizes the cognitive load when interpreting the control flow, making future updates or reviews less error-prone.
switch
versus if-else
statements. This consistency aids in code understanding and standardization among team members.
if-else
statements might execute faster than switch
statements due to fewer cases to evaluate.
/no-unused-collection
/no-use-of-empty-return-value
console.log
to return a value, which it does not, leading to potentially incorrect conditional checks or assignments./no-useless-catch
catch
clauses that merely rethrow errors without handling them, making the code more readable and concise./non-existent-operator
+=
, -=
, and logical NOT assignment (=!
to =!
), which are common sources of logic errors due to their similarity in appearance.
/prefer-immediate-return
/prefer-object-literal
{}
) instead of new Object()
followed by individual property assignments simplifies object initialization.
new Object()
syntax can lead to confusion with Object.create(null)
, which creates an object with no prototype. This distinction is clearer when using object literal syntax.
new Object()
and then populated with properties cannot be inspected as a whole until runtime.
/prefer-single-boolean-return
if-then-else
statements, it simplifies the logic flow, making it easier for other developers to understand the intention of the code.if-then-else
statement that solely returns true
or false
. This reinforcement of best practices can significantly improve the quality of the codebase over time by gradually eliminating these patterns./prefer-while
init
and update
expressions are absent and only a test
condition is present. This is because a “while” loop would be more semantically appropriate in such cases, improving readability by directly conveying the intent of looping based on a condition without initializing or updating loop variables.init
and update
sections are not utilized, this rule helps maintain consistency across the codebase. Consistent use of loop constructs based on their semantic purpose can make the code easier to understand and maintain.test
condition is present can lead to slight performance optimizations. Removing unnecessary syntax from a “for” loop (absence of init
and update
) could potentially reduce the overhead of evaluating expressions that are not needed, making the “while” loop a more efficient choice in such specific cases.