Can I use single-letter variable names in EK9?
← Variable Naming Rules and Conventions · Ref: Q295
Yes. Single-character variable names are always allowed in EK9. The compiler exempts them because mathematical conventions and loop counters are universally understood.
ALWAYS FINE
Mathematical variables: x, y, z for coordinates and equations. Loop counters: i, j, k for indices. Generic type parameters: T, K, V, U, S, E for parameterised types.
ACCEPTABLE
Short lambdas and inline functions where the type makes the meaning clear from context. When iterating a typed collection, a single letter can be sufficient: for c in customers.
BETTER TO AVOID
When the meaning is not immediately clear from surrounding context. In a long function body, prefer descriptive names even for loop variables: for customerIndex in 0 ... length customers.
WHY SINGLE-CHAR IS ALLOWED
The compiler trusts single-character names as intentional. A developer who writes 'x' knows exactly what they mean in context. The danger starts with two-plus character names that LOOK meaningful but carry no information: temp, data, obj, val. These create false confidence that the name communicates something.
KEY INSIGHT
Single-letter names are honest about being short. Names like 'data' pretend to be descriptive but tell you nothing about what data they hold. EK9 bans the pretenders but trusts the honest ones.
COMPARISON
Python and Java allow any name and rely on linters. Rust warns on non-snake-case but allows any identifier. EK9 enforces at compile time but trusts single-character names as intentional shorthand.
See Q290 for banned names. See Q292 for variable conventions. See Q80 for for-range with counters.
Example
defines module qa.naming.singleletter defines function calculateDistance() as pure -> x as Float y as Float <- d as Float: sqrt (x * x + y * y) computeQuadratic() as pure -> a as Float b as Float c as Float x as Float <- y as Float: a * x * x + b * x + c defines program SingleLetterDemo() stdout <- Stdout() // === MATH VARIABLES: x, y, z === x <- 3.0 y <- 4.0 z <- calculateDistance(x, y) stdout.println(`Distance from (${x}, ${y}): ${z}`) // === QUADRATIC: a, b, c, x === a <- 1.0 b <- -3.0 c <- 2.0 result <- computeQuadratic(a: a, b: b, c: c, x: x) stdout.println(`f(${x}) = ${result}`) // === LOOP COUNTERS: i === total <- for i in 1 ... 5 <- rtn <- 0 rtn: rtn + i stdout.println(`Sum 1-5: ${total}`) // === NESTED LOOPS: i, j === grid <- for i in 1 ... 3 <- rtn <- "" row <- for j in 1 ... 3 <- inner <- "" if length inner > 0 inner: inner + "," inner: inner + $(i * j) if length rtn > 0 rtn: rtn + " | " rtn: rtn + row stdout.println(`Grid: ${grid}`) // === COLLECTION ITERATION: descriptive preferred === names <- ["Alice", "Bob", "Charlie"] for n in names stdout.println(`Hello, ${n}`) // === COMPARISON: short vs descriptive === customerNames <- ["Alice", "Bob", "Charlie"] for customerName in customerNames stdout.println(`Customer: ${customerName}`)
Common mistakes
E50001 — Renaming the variable means later references to 'z' become unresolved, triggering E50001. Variable names must be consistent. See ek9 -h E50001 for details.
Incorrect:
zXYZ <- calculateDistance(x, y)
Correct:
z <- calculateDistance(x, y)
Other ways to ask this
- Are single-character variable names allowed in EK9?
- Can I use i, j, k as loop counters in EK9?
- Why does EK9 allow single-letter names but ban two-letter names like tmp?
Coming from another language?
Java: any identifier allowed, no special treatment for single-char names, relies on IDE inspections. Python: any identifier allowed, PEP 8 advises against single-letter except for counters. Rust: any identifier allowed, clippy has no single-char restriction. Go: single-letter names are idiomatic for short-lived variables. C/C++: any identifier allowed, single-letter names common in math code. Kotlin: any identifier allowed. JavaScript: any identifier allowed. EK9: single-character names explicitly exempted from naming rules, compiler trusts them as intentional shorthand while banning multi-character generic names.
Keywords: allowed, convention, letter, short, loop, single, variable, type, math, naming, parameter, migrate, counter, exempt, character, identifier, generic