Why must module constants be in a single file?
← Code Quality · Ref: Q863
When constants for a module are scattered across 3+ source files, EK9 requires consolidation into a single file (E11066). This is a module-level check for discoverability.
WHY CONSOLIDATION MATTERS
Constants scattered across files are hard to find, easy to duplicate, and impossible to review as a group. Consolidating them into one file per module provides:
1. A single reference point for all module constants
2. Easy duplicate detection during code review
3. Clear organization by concern using doc comments
4. Simplified maintenance when values change
THE FIX
Move all 'defines constant' blocks for a module into one file. Organize with doc comments:
defines constant //Temperature thresholds FREEZING_POINT <- 0.0 BOILING_POINT <- 100.0 //Retry configuration MAX_RETRIES <- 3 RETRY_DELAY_MS <- 500
THIS EXAMPLE
Shows all module constants properly consolidated in a single file with clear organization.
See Q695 for named constants. See Q800 for magic literal detection. See Q310 for code quality overview.
Example
defines module qa.quality.consolidate.constants defines constant <?- Temperature thresholds. All temperature-related constants grouped together. -?> FREEZING_CELSIUS <- 0.0 BOILING_CELSIUS <- 100.0 BODY_TEMP_CELSIUS <- 37.0 <?- Retry configuration constants. Used by network and service components. -?> MAX_RETRIES <- 3 RETRY_DELAY_MS <- 500 CONNECTION_TIMEOUT_MS <- 5000 <?- Formatting constants. Used by output and report functions. -?> SEPARATOR_LINE <- "----------------------------------------" INDENT_SPACES <- " " defines function <?- Classify temperature using consolidated constants. All thresholds come from one constants block. -?> classifyTemperature() as pure -> celsius as Float <- classification as String: "warm" if celsius <= FREEZING_CELSIUS classification: "freezing" else if celsius >= BOILING_CELSIUS classification: "boiling" <?- Format a report line using the shared formatting constants. -?> formatReportLine() as pure -> content as String <- rtn as String: INDENT_SPACES + content defines program ConsolidatedConstantsDemo() stdout <- Stdout() stdout.println(SEPARATOR_LINE) stdout.println(formatReportLine("Temperature Classifications")) stdout.println(SEPARATOR_LINE) stdout.println(formatReportLine(`Ice water: ${classifyTemperature(-5.0)}`)) stdout.println(formatReportLine(`Room temp: ${classifyTemperature(22.0)}`)) stdout.println(formatReportLine(`Steam: ${classifyTemperature(105.0)}`)) stdout.println(SEPARATOR_LINE) stdout.println(formatReportLine(`Max retries: ${MAX_RETRIES}`)) stdout.println(formatReportLine(`Timeout: ${CONNECTION_TIMEOUT_MS}ms`))
Other ways to ask this
- What is E11066 CONSTANTS_IN_MULTIPLE_FILES in EK9?
- How do I consolidate scattered constants?
- Why does EK9 require constants in one file per module?
Coming from another language?
Java: no enforcement of constant file organization. Python: convention to use constants.py but not enforced. Kotlin: no enforcement. Rust: no enforcement. Go: convention to use const blocks but scattered is allowed. EK9: E11066 compile-time enforcement requiring module constants in a single file.
Keywords: consolidate, quality, E11066, module, scattered, clean-code, file, constant, organization, discoverability