Naming Intermediate Results
An intermediate binding can reveal the source, unit, or invariant of a value and make errors easier to locate.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Name a meaningful step
Imagine a parcel fee of four credits plus two credits per started kilogram. For 1.4 kilograms, the charge is eight. Split the computation into a step named billable_kg and one named fee_credits. When a test observes six credits, you can inspect whether the first step returned one or the second used the wrong rate. Choose names that express units.
Account for the lifetime of the value
A value captured from a clock or read from a stream describes one moment. Calling the source again may produce a fresh value. Saving the result before making two related calculations ties those calculations to the same reading. Refer to evaluation order when choosing where that binding should happen.[1][2]
Separate a transformation from its use
Consider a parcel cost based on two credits per started kilogram, plus a fixed charge. Someone entering 1400 grams needs a conversion to 1.4 kilograms and then a ceiling to two billable kilograms. Multiply that number by the rate and add the fixed charge afterward. With a four-credit fixed amount, the result is eight. The same rule applied to exactly 1000 grams gives six.
Make a trace with the original grams, kilograms, billable kilograms, and credits. If the first incorrect value is one billable kilogram for 1400 grams, investigate the rounding operation. If two billable kilograms becomes six credits before the fixed charge, investigate the rate. The representation of units lets you give each intermediate a name expressing its meaning rather than its position in the routine.
Keep repeated observations consistent
A well-chosen intermediate can also freeze one observation for use in several calculations. Suppose two deadlines in the same report are measured from the time at which the report started. Reading the clock separately for each deadline can shift their reference points. Capture the start once and pass it to the relevant functions. A fake clock or explicit time makes that behavior straightforward to verify.
An intermediate earns its name when it exposes a changing unit, a meaningful check, or a reusable observation. Use a direct expression for an operation whose components already tell the story. For calculations using shared or effectful sources, an explicit binding can make the sequence easier to audit than a deeply nested expression.