Erlang basics · Lesson 26 (open contents)
Write the project promise
Turn a log-cleaner idea into exact input, output, and error examples.
Run this first
Run it once, change one input, and compare the new result.
%% Describe observable behavior before modules
Examples = [
#{input => <<" INFO ready ">>, output => {ok, {info, <<"ready">>}}},
#{input => <<>>, output => skip},
#{input => <<"BROKEN">>, output => {error, invalid_line}}
],
length(Examples).- The brief contains
3executable examples.
Read from the first line down
- Read
#{input => ..., output => ...}: Store one executable example as a map. - 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 a map.
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 records what this small 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 records what this small 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 tuple 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 quietly changing promised behavior.
Remember these three lines
- 1
Start with observable examples.
- 2
Keep the first scope small.
- 3
Write invariants before optimizations.