Exercise: The Good-Morning Routine

Take the next ticket for the hub: new porch blinds must react whenever the front door locks or unlocks, and a vacation mode must add a check to every voice command. Change the working hub so both plug in from outside, without editing the front door for each new device or the voice handler for each new check, with the tests green throughout.

Journeyman · C# · Java · Python · JavaScript · TypeScript · Updated

By

Two other teams are waiting on the HomeBase hub. One is installing porch blinds that should open when the front door unlocks; the other is building a vacation mode that stops voice commands while the family is away. The hub works today. Your job is to change it so both teams can plug in from outside, now and every month after, without breaking anything that works.

Before you start

You need one language set up, as in Setup: VS Code and Your Language, and to know how to run the tests, as in Running the Exercise Tests. The exercise is written in all five of the course's languages; pick the one you use.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-dp-morning-routine, also linked from the Exercise box beside the lesson. Clone it into your course folder. In a terminal, in that folder:

git clone https://github.com/ask-codey/exercise-dp-morning-routine.git

Git makes a folder, exercise-dp-morning-routine, with the exercise code inside.

The repository has a folder for each language: csharp, java, python, javascript and typescript. Each folder holds:

  • the hub's code, as it is today: the voice commands and their checks, the good-morning routine, the front door, the lights, the alarm and the speaker, and the function that builds Maya's house (the repository's README.md names the files for each language);
  • the tests, which you don't need to change;
  • a demo program that runs a morning at Maya's house, with the porch blinds and vacation mode written outside the hub's code, the way the other teams will write them.

In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.

The ticket

From: Maya, who owns the house (and the HomeBase hub)

  1. The porch blinds are coming. The blinds team will call hub.when("front door unlocked", ...) with what the blinds should do, and hub.when("front door locked", ...). The blinds must react however the door is unlocked or locked: by the good-morning routine, by voice, or by hand at the door. The alarm and the hallway light must keep reacting as they do today. The motion sensor and the coffee maker will plug in the same way next month.
  2. Vacation mode is coming. The travel team will call hub.addCheck(...) with a check: a function that gets the voice command and returns a reason to stop it, or nothing to let it through. New checks run after the hub's three checks, in the order they were added; the first reason stops the command.

Everything that works today has to keep working.

when and addCheck are already there, because the other teams' code calls them, but they don't do anything yet. The repository's README.md has the ticket in full.

What to do

  1. Run the tests first. Six pass: they're what the hub does today. Six fail: they're the ticket.
  2. Read the code and find the smells. Who does the front door know, and why? What would you have to edit to add a fourth check?
  3. Change the code in small steps, and run the tests after every step. The six that pass must stay green the whole time.
  4. Stop when all twelve are green and the demo prints what When you're done shows.

The README's Stuck? section names the patterns, if you want them. Try a few minutes without it first.

Check your work

Each language has its own command, run in the terminal in your language's folder (Terminal > New Terminal in VS Code). The repository's README.md has the full list, including the one-time setup for Python (a virtual environment and pytest) and TypeScript (npm install).

Language Run the tests Run the demo
C# dotnet test dotnet run --project MorningRoutine
Java ./mvnw test (.\mvnw test in PowerShell) java src/main/java/Demo.java
Python python -m pytest python demo.py
JavaScript npm test node demo.js
TypeScript npm test node demo.ts

A failing test's message starts with Not yet: for the ticket's work, or Keep this working: if a change broke something that worked, and says what it expected, for example Not yet: unlocking the front door by hand opens the porch blinds too.

Hints

  • Start with the blinds, and the unlock "by hand" test. A hand at the door never goes through the voice handler, so whatever makes the blinds react has to start at the door itself.
  • Today's rules can be set up the same way as the blinds'. If the alarm and the hallway light reacted through when too, the door would only need to say what happened.
  • The door and the hub need each other. The function that builds the house is yours to change: build one first, then hand it to the other.
  • For the checks, look at what the three have in common: each one either gives a reason to stop, or lets the command go on. Things that share a shape can share a list.

When you're done

All 12 tests are green, and the demo prints the same nine lines in every language:

Porch blinds: open
Maya: Done: good morning
House: kitchen light on, hallway light on, front door unlocked, alarm off, playing https://tuneo.example/news
guest: Stopped: a guest can't unlock doors
Sam locks the front door by hand
Porch blinds: closed
House: kitchen light on, hallway light off, front door locked, alarm armed, playing https://tuneo.example/news
Vacation mode on
Maya: Stopped: the family is away

The blinds and vacation mode were both written outside the hub's code, and neither the front door nor the voice handler mentions them. Next month's motion sensor is one more when, and the next check is one more addCheck.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-dp-morning-routine), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-dp-morning-routine.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-dp-morning-routine, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

What's next: Pass 4

This was Pass 3's last exercise. You took the smart-home hub through brands that didn't fit, a weather service that was slow and then down, and devices that had to stop knowing each other. Pass 4, Going to Work on a Framework, builds the Riverside Community Library's checkout web API on a real framework, and finds the patterns the framework itself is made of. The checks you lined up here come back as middleware, the chain every web request passes through, and the way you built the house, handing each part what it needs, comes back as dependency injection.

So what

Two teams can now add to the hub without asking you to edit the front door or the voice handler. The devices only say what happened, the hub holds the rules, and each check is one link in a line: the next device and the next check are someone else's one line.

Key terms

Related