Reading a Program as a Chain of Transformations
A program that changes data through several representations is easier to inspect when each stage has a named input and output.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 2 min
In this article4 chapters
Track the representation at each step
Suppose a program reads "3,10", splits it into strings, parses two numbers, and sums them. Joining the strings yields "310". Converting them first gives thirteen. The critical change is the move from text to numeric values. The rules behind sample input determine how to handle empty fields or separators.
Show a failed intermediate stage
An input of "3,x" produces two strings successfully and then fails during conversion. Report the field and expected kind of value at that stage. A small function contract for each stage makes it clear which caller can repair the error and which stage can assume numeric data.[1][2]
Label the representation after each step
Suppose a record arrives as the UTF-8 bytes for "north,7". Decoding gives a string. A CSV reader gives two fields under its quoting rules. Validation can accept north as a location name and convert the second field to integer seven. A calculation using those values can report fourteen after multiplying by two. Each intermediate has a different type and a particular condition it guarantees for the next step.
A superficially similar pipeline might concatenate the numeric field with itself and give the text "77". The first differing state is therefore the result of conversion, even though the output looks wrong at the end. See typed input records for keeping text labels and numerical fields separate.
Let each stage report its own failure
With the input line "north,six", decoding and splitting can both succeed. Numeric conversion supplies the first useful error. With a UTF-8 byte sequence cut inside an accented label, decoding can be the failing step instead. Distinguish those paths so the reported diagnostic names the correct boundary. Structural and domain validation explains why a numeric field that parses can still break a later rule.
For a practice trace, use an empty quantity and a valid zero quantity. Decide whether each should be rejected or represented as absent before calculation. Put those expectations in the stage's contract so they remain testable even if a different file reader is installed.