Elixir basics · Lesson 26 (open contents)
Write the project promise
Turn a vague log-cleaner idea into input, output, and error examples.
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.
Read from the first line down
- 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.
Symbols are not secret signs
%{input: ..., output: ...}Store one executable example as data.
:skipLabel a line the project intentionally ignores.
:invalidLabel input the project must report.
Match each name to its meaning
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.
Why these forms are useful
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.
Close the answer and try
Add an example for WARN hot.
%{input: "WARN hot", output: ____}Target result: Use {:ok, {:warn, "hot"}}.
Stuck? Read one hint
Keep the same tagged shape.
After you run it, see one answer
%{input: "WARN hot", output: {:ok, {:warn, "hot"}}}Think about it: Why write examples before modules?
They keep implementation choices from changing the promised behavior.
Remember these three lines
- 1
Start with observable examples.
- 2
Keep the first scope small.
- 3
Write invariants before optimizations.