Literals, Calculated Values, and Domain Constants
A literal supplies a concrete value in the source. A well-named constant records a policy, unit conversion, or invariant shared across calculations.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Keep stable rules at one location
A fixed conversion of 60 seconds per minute can be expressed with a name when the unit is otherwise unclear. A business rule such as a fourteen-day return window deserves a named definition because changing the policy should affect all callers consistently. Use comments explaining decisions to document where the value comes from.
Retain the relationship
A 14-day window converted to hours gives 336. Defining both 14 and 336 independently permits one to change without the other. Compute the hours from the days when a caller needs that unit. Verify the result using intermediate values or a small test that exercises the shared definition.[1][2]
Choose which numbers are facts and which are results
Suppose a display reports elapsed hours as minutes. An hour contains sixty minutes under that unit convention, so a conversion can use a named constant or a duration type. If a policy retains documents for fourteen days, that fourteen belongs to configuration or a named rule. Computing the supported hour count from the selected days preserves the relationship when policy changes. A second independently assigned number for the hour count could continue encoding the old policy after an edit.
Try revising the window from fourteen to fifteen days in a feature test. A dependent hour value should move from 336 to 360. A fixture that changes the definition and checks retrieval at both boundaries is more useful than a test reading the default only. See units in computations.
Preserve units and assumptions when inputs become configurable
A default expression can be evaluated at a different time from the code using it. For example, a duration configured once at process startup can intentionally stay fixed during a job. A value read for each request can change as inputs arrive. Default argument timing shows why a literal constant and a call such as “read the current day” have different lifetimes.
When reviewing a number, ask which interface or rule owns it, what unit it has, and whether it ought to change independently. Give separate names to two equal numbers if their meanings differ. That distinction helps when a later change correctly updates one while leaving the other's policy intact.