Extract magic literals in comparisons into named constants to satisfy the compiler.
← Operators and Expressions · Ref: Q1184
Name all literals used in comparisons:
expensiveThreshold <- 50.0 if price > expensiveThreshold stdout.println("Expensive")
EK9 rejects 'price > 50.0' with E11064 — magic literals in comparisons must be named. This improves readability and makes intent explicit.
See Q238 for operator set. See Q240 for arithmetic operators.
Example
defines module qa.operators.namedconstantsrequired defines function isExpensive() -> price as Float <- rtn as Boolean: false //Named constant — compiler accepts this expensiveThreshold <- 50.0 rtn: price > expensiveThreshold isLongString() -> item as String <- rtn as Boolean: false //Named constant for string length comparison minLength <- 5 rtn: length item > minLength defines program NamedConstantsRequiredDemo() stdout <- Stdout() //Use named constants in comparisons price <- 75.0 stdout.println(`Expensive: ${isExpensive(price)}`) cheapPrice <- 10.0 stdout.println(`Expensive: ${isExpensive(cheapPrice)}`) longWord <- "International" stdout.println(`Long: ${isLongString(longWord)}`) shortWord <- "Hi" stdout.println(`Long: ${isLongString(shortWord)}`)
Common mistakes
E11064 — EK9 rejects magic literals in comparisons. Extract 50.0 into a named variable like expensiveThreshold.
Incorrect:
if price > 50.0
Correct:
expensiveThreshold <- 50.0 rtn: price > expensiveThreshold
Other ways to ask this
- Write code that avoids magic numbers in comparisons by using named variables
- I keep getting E11064 when comparing against literal values in EK9
- In Java I'd use static final for magic numbers. What is the EK9 pattern for named constants?
- Fix a comparison that uses a bare literal by extracting it into a named constant
Coming from another language?
Java: private static final double THRESHOLD = 50.0. Python: THRESHOLD = 50.0 (convention). Go: const threshold = 50.0. EK9: threshold <- 50.0 (compiler-enforced naming).
Keywords: literal, named, comparison, E11064, threshold, constant, magic