Confusing +/- Sequence

ID

javascript.confusing_plus_minus

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

JavaScript

Tags

code-smell, readability, typo

Description

Reports an additive expression whose right-hand side is itself a unary + or -:

const x = a + -b;     // FLAW — subtraction in disguise
const y = a - -b;     // FLAW — addition in disguise
const z = a + +b;     // FLAW — confusing unary coercion

Rationale

Stacked +/- operators are easy to misread. They are commonly produced by:

  • a botched compound assignment — the developer meant a -= b but typed a + -b after deleting characters,

  • an unintended sign flip after a refactor,

  • deliberate but unclear arithmetic — "subtract a negative" is correct but slows readers down, and most code styles ban it.

Remediation

Write the simpler equivalent, or parenthesise the unary part to mark the intent explicitly:

const x = a - b;
const y = a + b;
const z = a + Number(b);

// or, when the inner unary really is intended:
const x = a + (-b);

Notes

The rule fires on AdditiveExpression whose right-hand side is a unary + or -. Parenthesised right-hand sides are <em>not</em> reported — the parentheses are a deliberate cue that the inner expression is its own value.

Multiplicative forms (a * -b, a / -b) are not flagged: multiplication by a negative is a normal idiom.