Lesson plan

Software design and testing lesson plan

How teams keep firmware right as it grows: readable code, modules behind narrow interfaces, fakes, tests on boot, test-first fixes, safe refactors and code review. Ends with a messy sketch refactored under test.

4 chapters, 7 lessons, about 7 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Names, one job per function, and modules behind a narrow interface.
  • A harness in twenty lines, then bugs fixed test first.
  • Refactors under test, and the review and git habits of a team.
  • A messy dice sketch, fixed test first and refactored under its own self-test.

Materials for the whole unit

  • Arduino Uno R3
  • Fan module, L9110
  • Full-size breadboard
  • Half breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 3
  • Seven segment display
  • Temperature sensor

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 55 min

Code people can read

Names, one job per function, and modules behind a narrow interface.

LessonTimeWhat students do
Clean functions and namingA thermostat sketch that works and lies: one-letter names, magic numbers that drifted apart, one loop doing four jobs. Name the numbers, give each job a function, and read loop() like a table of contents.60 min
  1. Run it: a sketch only its author can read
  2. Heat it up: the sketch lies
  3. Name the number
  4. One job, one function: the report
  5. Name the timing, and give the heartbeat a function
  6. Names that say what
  7. loop() as a table of contents
Modules and interfacesA thermostat that only knows a TempSensor interface: a real TMP36 and a fake behind the same door, a rehearsal with the fake on boot, a move into sensor.h, and a calibration added without touching the thermostat.55 min
  1. Run it: a rehearsal before the real thing
  2. The interface is one question
  3. Hysteresis, tested where it is hard
  4. Swap the sensor, not the thermostat
  5. Give the sensors their own file
  6. A third sensor, and no change to the thermostat

Materials

  • Arduino Uno R3
  • Fan module, L9110
  • Half breadboard
  • Temperature sensor

Check for understanding

Each student finishes "Modules and interfaces" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A third sensor, and no change to the thermostat".

Chapter 2. About 2 h

Tests that run themselves

A harness in twenty lines, then bugs fixed test first.

LessonTimeWhat students do
Unit tests with plain assertsA test harness in twenty lines: a CHECK macro that prints PASS or FAIL with the line and the condition, a summary line and a red or green lamp. Test the edges, meet a real failure, fix it, and switch the tests off for a release.60 min
  1. Run it: eight checks before loop()
  2. How a CHECK line knows what it checked
  3. Green means all of them
  4. Test the edges
  5. A test that finds a real bug
  6. Fix it, and watch it go green
  7. Tests out of the release
Test-driven fixesA bug report against a countdown that passes all its tests: reproduce it on the hardware, pin it to one pure function, write the test that fails, fix it, triangulate the edges, and close the report.60 min
  1. Run it: green, and still wrong
  2. Reproduce it on the hardware
  3. Pin it to one function
  4. Red: the test that fails
  5. Green: the fix
  6. Triangulate the edges
  7. Close the report

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "Test-driven fixes" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Close the report".

Chapter 3. About 1 h 55 min

Changing code safely

Refactors under test, and the review and git habits of a team.

LessonTimeWhat students do
RefactoringA Beaufort wind scale as an if chain and a switch, reshaped into two tables with its tests green after every step: replace a conditional with a table, prove the net catches a slip, extract a function, and add a feature in one line.60 min
  1. Run it: a safety net first
  2. What it does today
  3. Replace the conditional with a table
  4. Slip on purpose
  5. Extract a function, so it can be tested
  6. Now a feature is two numbers
  7. Same program, better shape
Code review and git, explainedCommits, branches, diffs and pull requests explained properly, then a real review: a teammate's pull request with a millis() rollover, an off-by-one and an int overflow, each caught by a checklist question and proven fixed by the self-test.55 min
  1. Run it: a pull request lands
  2. Read the diff
  3. Comment one: time math
  4. Comment two: the loop bounds
  5. Comment three: is the type big enough?
  6. Your checklist, your commit

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • LED x 3
  • Pushbutton
  • Resistor x 3

Check for understanding

Each student finishes "Code review and git, explained" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Your checklist, your commit".

Chapter 4. About 1 h 10 min

Boss

A messy dice sketch, fixed test first and refactored under its own self-test.

LessonTimeWhat students do
Boss: refactor the dice, tests greenThe gallery's electronic dice after a game night of quick fixes: one-letter names, an if chain, pasted code and a bug report. Fix the bug test first, then refactor it clean, one step at a time, with the self-test saying all pass after every run.70 min
  1. Run it: the sketch nobody wants to touch
  2. Roll it
  3. Fix the bug, test first
  4. Refactor 1: names
  5. Refactor 2: the font as a table
  6. Refactor 3: one show(), not two pastes
  7. Leave a check behind
  8. Finish it your way

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • Pushbutton
  • Resistor
  • Seven segment display

Check for understanding

Each student finishes "Boss: refactor the dice, tests green" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Finish it your way".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.