Skip to content

Parenthesize comparisons before XOR in cmp() for clarity - #228

Open
TheWitness wants to merge 1 commit into
MikeMcl:mainfrom
TheWitness:clarify-cmp-operator-precedence
Open

TheWitness wants to merge 1 commit into
MikeMcl:mainfrom
TheWitness:clarify-cmp-operator-precedence

Conversation

@TheWitness

Copy link
Copy Markdown

What

Adds explicit parentheses around the comparison that precedes the ^ (XOR) in
Big.prototype.cmp, in both big.js and big.mjs:

return k > l ^ isneg ? 1 : -1;          // before
return (k > l) ^ isneg ? 1 : -1;        // after

(and the two other occurrences in the same method).

Why

> already binds more tightly than ^, so this is behaviour-preserving —
the evaluation order is unchanged. Making it explicit improves readability and
silences static-analysis tools (e.g. CodeQL's "unclear precedence of nested
operators" rule, js/unclear-operator-precedence), which flag a > b ^ c as
potentially ambiguous.

Testing

npm test passes unchanged: 47456 of 47456 tests passed (Node 16.20.2).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant