E11066: constants should be consolidated into a single file

Category: E11xxx Code quality and IR analysis · Reported in phase: DUPLICATION_CHECK · Classification: CONSTANTS_IN_MULTIPLE_FILES · All error codes

What went wrong

Constants are scattered across 3 or more source files within the same module. This fragmentation makes it difficult for developers to discover what constants a module provides and leads to inconsistent naming across files.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 863 (Why must module constants be in a single file?)

Rationale

  • Constants define a module's shared vocabulary of fixed values
  • Scattering them across files forces developers to search multiple locations
  • A single constants file provides one authoritative location for all fixed values
  • Consolidation reveals naming inconsistencies and duplicate near-values
  • Threshold: 3+ files with constants (2 files allows main + constants pattern)

How to fix it

Gather the module's constants into one dedicated file, leaving every other construct where it is.

With tooling, ek9_organize_constants does the whole move in one step: name the target file and it is created for you if it does not yet exist.

By hand:

  1. Create a constants file for the module (e.g. moduleConstants.ek9)
  2. Move every constant into its single `defines constant` block
  3. Review the names now they are visible together
  4. Leave functions, classes and records in their original files

Common causes

  • Constants added ad-hoc to whichever file was being edited at the time
  • Multiple developers independently creating constants in different files
  • Splitting code by feature without centralising the module's vocabulary

See also

  • E11067: too many constants across files (related threshold check)
  • E11014: low cohesion (related structural quality metric)

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E11066: constants should be consolidated into a single file

bad/constants1.ek9

#!ek9
defines module error.e11066.bad

  defines constant
    MAX_RETRIES <- 3
    DEFAULT_TIMEOUT <- 30

bad/constants2.ek9

#!ek9
defines module error.e11066.bad

  defines constant
    BASE_URL <- "https://example.com"
    API_VERSION <- 2

bad/constants3.ek9

#!ek9
defines module error.e11066.bad

  defines constant
    LOG_LEVEL <- "DEBUG"
    MAX_CONNECTIONS <- 100

Fixed: One dedicated constants file; the other files of the module use them by name

good/constants.ek9

#!ek9
defines module error.e11066.good

  defines constant
    MAX_RETRIES <- 3
    DEFAULT_TIMEOUT <- 30
    BASE_URL <- "https://example.com"
    API_VERSION <- 2
    LOG_LEVEL <- "DEBUG"
    MAX_CONNECTIONS <- 100

good/client.ek9

#!ek9
defines module error.e11066.good

  defines function

    endpoint() as pure
      -> resource as String
      <- url as String: `${BASE_URL}/v${API_VERSION}/${resource}`

    connectionBudget() as pure
      <- description as String: `${MAX_CONNECTIONS} connections, ${DEFAULT_TIMEOUT}s timeout, ${MAX_RETRIES} retries`

good/logging.ek9

#!ek9
defines module error.e11066.good

  defines function

    logLine() as pure
      -> message as String
      <- line as String: `[${LOG_LEVEL}] ${message}`

Found a problem?

If EK9 reported E11066 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.