Constants Component
Component ID: bmforge.constants · Specification: 0.1.0 (draft)
Intent
Avoid magic strings and significant numeric literals from the first application module. Name values by their semantic domain, not by generic piles of fields/defaults/labels. This is a coding standard, not a new runtime library, service, metaclass or framework. Every BMForge greenfield application installs this skill as part of its baseline.
Approved runtime modules
| Origin | Module | Approved use |
|---|---|---|
| Python stdlib | typing |
Final to document fixed values, paths, ports, shape constants and immutable intended defaults |
| Python stdlib | enum |
StrEnum and IntEnum for closed protocol, action and status vocabularies |
Use only what the chosen constant group requires. Final expresses intent for type checkers; it is not a runtime immutability guarantee. Do not introduce a custom ConstGroup metaclass, registry, global settings cache or third-party config framework without user approval.
Naming and ownership
- Store classes in
<app>/constants/D<Name>.py; one meaningful domain group per module. Use class names such asDProject,DNetwork,DZMQ,DField,DSQL,DGameDef, orDStatus. Prefer uppercase attribute names, module/class name agreement and explicit imports from the owning module. - Define application identity and literal
VERSION, CMDB metadata, deployed component paths and network listener declarations inD<Project>(start withtemplates/project/constants/DProject.py). Maintain the literal version so release tooling can inspect it without executing code. - Use
Finalfor fixed values: configuration keys, protocol envelope fields, action identifiers, semantic defaults, ports, timeouts, file paths and SQL column mappings. UseStrEnumfor semantically closed text values andIntEnumfor stable numeric codes, preserving external serialized values. - Keep the class definitions clear and data-only. Never load environment files, perform I/O or call external services merely by importing a constants module.
- Centralize definitions once within their domain. Cross-reference named values where it improves clarity, but avoid circular imports and aliases that obscure ownership. Do not create a separate
DField,DLabel, andDDefaultfor each feature as a reflex. - Do not replace an intentional schema field, API key, status code, network method or config name with a translated value. They must remain stable across languages and are persisted as canonical identifiers.
Example:
from enum import StrEnum
from typing import Final
class DNetwork:
HTTP_PORT: Final[int] = 49700
CONFIG_FILE: Final[str] = "conf/app.json"
class DStatus(StrEnum):
RUNNING = "running"
PAUSED = "paused"
Boundary with localization
A user-visible label is not a protocol constant. Use a stable DText message key and look up the localized value through Application Localization. Example: DStatus.RUNNING remains "running" in MariaDB/JSON, while localizer.text(DText.STATUS_RUNNING, locale=language) returns the appropriate readable label. Do not build English-only *_LABEL constants into activities, interfaces or renderers.
Ordinary SQL queries, temporary local variables, MIME types, obvious one-off display prose and non-semantic numbers do not each require their own constants. Prioritize duplicated or behaviourally significant literals rather than replacing everything with indirection. Configurable runtime overrides belong in the existing Runtime Configuration process; constant defaults are not the final deployed configuration.
Frozen source examples
- Snake Lab
NadimGhaznavi/snake-labcommit52b02e054b2161701813e9231abf5db8635f3ec1, frozen undercomponents/reference/snake-lab/snakelab/constants/:DGame.py(Final,IntEnum,StrEnum),DSQL.py(stable column mapping),DMyLog.py(log-level vocabulary),DSnakeLab.py(app defaults and CMDB inventory). - BMGeoIP
NadimGhaznavi/bmgeoipcommit35c8745c7003a124dce5b5799b08b2d7c851fbfc, frozen undercomponents/reference/bmgeoip/bmgeoip/constants/:DBMGeoIP.py,DGeoIp.py,DZMQ.py(small app, domain and protocol groups).
The snapshots document patterns, not values to import into unrelated applications. No imports from Snake Lab or BMGeoIP in a generated application.
Acceptance checks
- Behavioral string/number literals live in domain groups or an explicitly selected standard enum; no scattered repeated message methods, schema keys, status codes or magic ports.
- Application-wide identity and version are literal, stable and discoverable; CMDB network and persistent-data declarations agree with deployment.
Dmodules do not import domain activities or perform side effects. No custom constant framework or unapproved dependencies appear.- Changing a constant preserves wire/storage compatibility unless an explicit migration was approved.
- Translation keys are stable
StrEnumvalues; display text is looked up from locale catalogues, not embedded as English-only constants.