E11067: module has many constants - consider a dedicated constants file

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

What went wrong

A multi-file module has 10 or more constants spread across 2 or more files. This volume of constants across multiple files indicates the module's vocabulary has grown large enough to warrant a dedicated constants file.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 1341 (Why does EK9 require a dedicated constants file once a...)

Rationale

  • A large number of scattered constants creates a maintenance burden
  • Developers cannot see the full constant vocabulary without opening multiple files
  • Consolidation into one file makes it easy to spot duplicates and near-duplicates
  • A dedicated constants file signals intent: "this is where fixed values live"
  • Threshold: 2+ files with constants AND 10+ total constants

How to fix it

Give the module a dedicated constants file and move every constant into it.

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 dedicated constants file for the module
  2. Move every constant from the other files into its single block
  3. Group related constants together with comments
  4. Consider whether the module is doing too much (a high constant count may signal this)

Common causes

  • Module vocabulary growing organically without periodic consolidation
  • Constants added alongside the functions that use them rather than centralised
  • Feature-oriented file splitting that distributes constants across features

See also

  • E11066: constants scattered across 3+ files (related organisation check)
  • E11014: low cohesion (module may need restructuring)

Examples

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

Fails with E11067: module has many constants - consider a dedicated constants file

bad/processing.ek9

#!ek9
defines module error.e11067.bad

  defines constant
    MAX_BATCH_SIZE <- 1000
    MIN_BATCH_SIZE <- 10
    DEFAULT_WORKERS <- 4
    RETRY_DELAY <- 250ms
    MAX_QUEUE_DEPTH <- 500

bad/validation.ek9

#!ek9
defines module error.e11067.bad

  defines constant
    MIN_NAME_LENGTH <- 2
    MAX_NAME_LENGTH <- 100
    DEFAULT_LOCALE <- "en_GB"
    MAX_FIELD_COUNT <- 50
    TRIM_WHITESPACE <- true

Fixed: The ten constants in one dedicated file; the processing and validation files use them

good/constants.ek9

#!ek9
defines module error.e11067.good

  defines constant
    MAX_BATCH_SIZE <- 1000
    MIN_BATCH_SIZE <- 10
    DEFAULT_WORKERS <- 4
    RETRY_DELAY <- 250ms
    MAX_QUEUE_DEPTH <- 500
    MIN_NAME_LENGTH <- 2
    MAX_NAME_LENGTH <- 100
    DEFAULT_LOCALE <- "en_GB"
    MAX_FIELD_COUNT <- 50
    TRIM_WHITESPACE <- true

good/processing.ek9

#!ek9
defines module error.e11067.good

  defines function

    batchSizeFor() as pure
      -> requested as Integer
      <- size as Integer: MAX_BATCH_SIZE <? (MIN_BATCH_SIZE >? requested)

good/validation.ek9

#!ek9
defines module error.e11067.good

  defines function

    isValidName() as pure
      -> name as String
      <- valid as Boolean: length name >= MIN_NAME_LENGTH and length name <= MAX_NAME_LENGTH