Write the project promise: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
The original English project below is a log cleaner. The current notebooks use the same file-statistics project as the Chinese route. These preserved examples show another application of the concepts; their inputs and expected outputs belong to the log cleaner.
Turn a vague log-cleaner idea into input, output, and error examples.
Write three acceptance examples before creating project modules.
Run this first
Run it once, change one input, and compare the new result.
# Describe the project before implementing it
examples = [
%{input: " INFO ready ", output: {:ok, {:info, "ready"}}},
%{input: "", output: :skip},
%{input: "BROKEN", output: {:error, :invalid_line}}
]
Enum.count(examples)The brief contains three executable examples.
Steps for the full example
- Read
%{input: ..., output: ...}: Store one executable example as data. - Read
:skip: Label a line the project intentionally ignores. - Read
:invalid: Label input the project must report.
Original lesson notes
Run the example first. Predict one result, then change one input and run it again.
A concrete input and expected output makes a requirement testable.
Scope lists what this project will and will not do.
Symbols and terms
%{input: ..., output: ...}- Store one executable example as data.
:skip- Label a line the project intentionally ignores.
:invalid- Label input the project must report.
- acceptance example
- A concrete input and expected output makes a requirement testable.
- scope
- Scope lists what this project will and will not do.
- invariant
- An invariant is a rule that must hold for every accepted result.
Original full exercise
Add an example for WARN hot.
%{input: "WARN hot", output: ____}Use {:ok, {:warn, "hot"}}.
Show hint
Keep the same tagged shape.
Show original answer
%{input: "WARN hot", output: {:ok, {:warn, "hot"}}}Why write examples before modules?
They keep implementation choices from changing the promised behavior.
Original lesson takeaways
- Start with observable examples.
- Keep the first scope small.
- Write invariants before optimizations.