Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Use modulo directly for wrap-around indexing
Use remainder directly for wrapping a validated nonnegative integer index into a positive-length collection; preserve checks for invalid lengths and negative inputs.
Bad example
| 1 | if (potentialIndex >= length) { |
| 2 | return potentialIndex % length; |
| 3 | } |
| 4 | return potentialIndex; |
Explanation (EN)
For validated nonnegative indices, remainder already handles both the in-range and upper-bound cases.
Objašnjenje (HR)
Good example
| 1 | return potentialIndex % length; |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
Simplify the upper-bound branch only for finite nonnegative integer indices and a strictly positive integer length. Establish how out-of-range negative indices and empty collections must behave.
Exceptions / Tradeoffs (EN)
JavaScript % is remainder: negatives remain negative and division by zero yields NaN for numbers. Keep validation or use a separately defined wrapping contract.