Exercise: The Smell Clinic

Diagnose Summit Gym's booking code like a doctor: five smells, one from each family, each written up as symptom, cause and cure. Then cure the three that today's changes are waiting on, with the tests green throughout.

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

By

Summit Gym's owner has a ticket for the new developer, and it starts with a question rather than a feature: what's wrong with this code? Before three waiting changes go in, you'll read the booking system the way this chapter taught, write down each smell as symptom, cause and cure, and then cure the three smells those changes are blocked by, without breaking anything that works today.

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-smell-clinic, 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-smell-clinic.git

Git makes a folder, exercise-dp-smell-clinic, with the exercise code inside.

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

  • the gym's code, as it is today: the kinds of class, the GymManager that books them, the screens that show what a member pays, and the greetings at the front desk (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 the gym, with pilates added the way the scheduling team will add it.

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

The ticket

From: the owner, Summit Gym

  1. A diagnosis. Go through the code and tell me what's wrong with it, in plain words, so the next developer doesn't have to find out the hard way.
  2. Three changes that are waiting on that code:
    • Pilates starts next month, and more kinds of class after it. The scheduling team's code calls addKind(...) with each new kind's name, length, room and drop-in price, and a new kind must work everywhere without anyone editing the gym's code.
    • The test team wants tests that don't need our database. Their code hands the gym a store of its own, new GymManager(store), and expects the bookings to go there.
    • The front desk wants a "Can I book?" button, canBook(member, kind), that answers with the price (or "full") without booking anything.

Everything that works today has to keep working.

addKind, canBook and the constructor that takes a store are already there, because the other teams' code calls them, but they don't do what the ticket asks yet.

What to do

  1. Run the tests first. Five pass: they're what the gym does today. Three fail: they're the ticket.
  2. Diagnose all five smells. The repository's README.md has a chart with one row per smell and a Where column that says where to look. For each one, write the symptom you can see, the cause underneath it, and the cure. The Smell Map is your reference; the three questions (what changes together, what has no home, who knows too much) find the family.
  3. Cure the three smells the changes are waiting on, one small step at a time, running the tests after every step. The five that pass must stay green the whole time.
  4. Stop when all eight are green and the demo prints what When you're done shows. Curing the other two smells is a bonus: no test checks it, and the solution's README explains why it leaves them.

The README's Stuck? section names the cures, 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 SmellClinic
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: after addKind, pilates is 50 minutes in Studio A, $16 drop-in.

Hints

  • Diagnose before you change anything. Each smell's Where points at a few lines. Read them and ask which family they belong to before you touch them.
  • Pilates: how many places decide what a kind of class means? What if each kind's values lived together?
  • The store: who builds the database today, and who should?
  • "Can I book?": the comments inside book are the names of the pieces the button needs.

When you're done

All 8 tests are green, your chart has all five rows filled in, and the demo prints the same six lines in every language:

Yoga: 60 minutes, Studio A, $15 drop-in
Pilates: 50 minutes, Studio A, $16 drop-in
Maya: booked into Yoga, place 1 of 12, $15
Leo can book Spin: $0
Front desk: Leo pays $30 a month
Welcome, guest | Points: none yet

Pilates arrived without a single switch hearing about it, the gym's bookings can be tested without its database, and the new button reuses the checks instead of copying them.

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-smell-clinic), 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-smell-clinic.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-smell-clinic, 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.

So what

You read a codebase you didn't write, named what was wrong with it in words another developer can use, and cured exactly the smells that were in the way. That's most of what working on old code is, and the next chapter is about doing it safely.

Key terms

Related