Exercise: Name Your Own Pattern
Take a ticket from Summit Gym's front-desk team: three spreadsheet importers are near-copies, and a fourth team needs one too. Pull out their shared shape so the new importer is a few lines, keep all three importers' tests green, and write the pattern's card in the README.
Freelancer · C# · Java · Python · JavaScript · TypeScript · Updated
This is the course's last exercise, and the first where the pattern isn't in any book: it's the gym's own. Summit Gym has three importers that are copies of each other, and the equipment team needs a fourth. The code that's there works; your job is to find the shape the three share, write it once, and write it down, so the next team gets it in a few lines and knows when to use it.
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-name-your-own-pattern, 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-name-your-own-pattern.git
Git makes a folder, exercise-dp-name-your-own-pattern, 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 three importers, for members, instructors and class times, and the empty function the equipment team already calls (the repository's
README.mdnames the files for each language); - the tests, which you don't need to change;
- a demo program that imports one spreadsheet from each team.
The repository's README.md also has an empty pattern card for you to fill in.
In VS Code, choose File > Open Folder and open your language's folder, not the whole repository.
The ticket
From: the front-desk team lead, Summit Gym
- The equipment team wants to import their spreadsheet too. We already have three importers, and each is a copy of the one before. The equipment team has written their side already: they call
importRows(text, columns, makeRow)with the file's text, how many values each row should have, and a function that builds one row. Make it work exactly the way our three importers do: skip the header and blank lines, trim each value, and report a row with the wrong number of values asline 3: expected 2 values, found 1.- One fix, one place. The three importers we have should use the same code, so the next fix is made once.
- Write it down. Fill in the pattern card in the README, so the next team that loads a spreadsheet finds it, and knows when to use it and when not to.
The three importers have to keep working exactly as they do.
What to do
- Run the tests first. Four pass: they're what the three importers do today. Three fail: they're the ticket.
- Put the three importers side by side. Which steps does every copy do? Which part is really different in each one?
- Change the code in small steps, and run the tests after every step. The four that pass must stay green the whole time.
- Stop when all seven are green and the demo prints what When you're done shows.
- Fill in the pattern card in the README, in the course's card shape. Take Skip it when seriously: what kind of spreadsheet would this code read wrongly?
The README's Stuck? section has hints, 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 GymImports |
| 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: the kettlebells row is missing its room, on line 3.
The tests can't check your card. Check it the way a teammate would: could someone who has never seen this code tell from the card alone when to use it, and when not to?
Hints
- Start with the equipment team's function. Copy the steps from one importer into it, with the number of values and the row-building part handed in. Run the tests.
- Then move one importer at a time onto the shared function, and run the tests after each.
- For the card, look back at the Row Importer's card in Writing a Pattern Card Your Team Will Read, and at the course's own cards: problem first, then the idea, and an honest Skip it when.
When you're done
All 7 tests are green, your card is filled in, and the demo prints the same five lines in every language:
Members: Maya (basic), Sam (unlimited), Leo (basic)
Instructors: Priya teaches yoga, Omar teaches spin
Class times: yoga at 07:00, spin at 12:00, HIIT at 18:00
Equipment: mats in studio A, bikes in studio B
Equipment problems: line 4: expected 2 values, found 1
The equipment team's importer is a few lines, and so is each of the other three. The next spreadsheet is a few lines too, and the card tells whoever writes it when to reach for the shared code and when to use a real CSV reader instead.
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.
On GitHub, click +, then New repository. Name it (for example
exercise-dp-name-your-own-pattern), choose Public, and click Create repository. Leave out the README: your folder already has one.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-name-your-own-pattern.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-dp-name-your-own-pattern, 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 found a pattern nobody handed you, wrote it once, and wrote it down so others can use it: that's how every pattern in this course began. Where to Go Next, the last lesson, looks back at what you can now do.