Why does EK9 reject self-cancelling arithmetic?
← Code Quality · Ref: Q818
EK9 detects self-cancelling arithmetic expressions at compile time. Operations like x - x, x / x, x mod 1, and x rem 1 always produce a constant result regardless of x, making them dead code or logic errors.
DETECTED PATTERNS
amount - amount // always 0 amount / amount // always 1 (or division by zero if 0) amount mod amount // always 0 amount rem amount // always 0 amount mod 1 // always 0 amount rem 1 // always 0
WHY THIS MATTERS
These patterns usually indicate copy-paste errors where the developer meant to use a different variable on one side. The compiler catches the mistake immediately.
HOW TO FIX
- Check if you meant a different variable on one side
- If you genuinely want 0, assign it directly: result: 0
- If you genuinely want 1, assign it directly: result: 1
See Q310 for code quality overview. See Q734 for other tautology detection.
Example
defines module qa.quality.constant.arithmetic defines function calculateRemaining() as pure -> amount as Integer deduction as Integer <- result as Integer? result: amount - deduction defines program ConstantArithmeticDemo() stdout <- Stdout() remaining <- calculateRemaining(100, 25) stdout.println(`Remaining: ${remaining}`)
Common mistakes
E08083 — Subtracting a variable from itself always produces 0. This is usually a copy-paste error where you meant a different variable. See ek9 -h E08083 for details.
Incorrect:
result: amount - amount
Correct:
result: amount - deduction
Other ways to ask this
- What is E08083 CONSTANT_ARITHMETIC?
- Why can't I write x - x or x / x in EK9?
- How does EK9 detect tautological arithmetic?
Coming from another language?
Java: no detection — self-cancelling arithmetic compiles silently. Python: no detection. Rust: clippy warns about some patterns. Go: no detection. EK9: compile-time error for all self-cancelling arithmetic.
Keywords: dead, constant, self-cancelling, quality, E08083, code, arithmetic, tautology