Part 6 of 6 · 2 min read
Exercises
Easy
Easy 1: Find the duplicate (20 minutes)
In a program of your own, find two places with the same check or calculation. Write down whether they are the same fact.
Done when: you have two locations and a yes or no with a reason.
Hint
Ask if both would change for the same reason.
Easy 2: Run the slot check (20 minutes)
Type the slotError example and run book(9, 11), move(9, 8) and book(-1, 3).
Done when: the three outputs match those in section 1.
Hint
The method returns null when there is no error.
Easy 3: Sort the ideas (20 minutes)
Sort these into DRY, KISS or YAGNI: "do not add a plug-in system yet", "write the tax rule once", "use a plain loop here".
Done when: each item has one label and a short reason.
Hint
One item fits two labels, so pick the stronger.
Medium
Medium 1: Remove duplication (45 minutes)
Write a program with the same discount formula in three methods. Move it to one method. Then change the discount and count edits before and after.
Done when: you show 3 edits before and 1 after.
Hint
Use a method with the rate as a named constant.
Medium 2: Keep the copy (45 minutes)
Write two methods whose formulas match today but belong to different teams, such as tax for invoices and tax for reports. Decide whether to merge, and describe a change that would break a merged version.
Done when: your note names both teams and the future change.
Hint
A tax law change for one team only.
Medium 3: Plain first (45 minutes)
Implement a notifier with one channel as a plain class. Write down the exact event that would make you add an interface.
Done when: the plain class works, and a note names the trigger.
Hint
A second channel is the usual trigger.
Hard
Hard 1: Simplify a design (90 minutes)
Take a design of your own with at least six classes. Apply Beck's four rules in order. Remove any class that does not earn its place.
Done when: a before and after list shows removed or merged classes and the reasons.
Hint
Rule 1, passing tests, comes before the rest.
Hard 2: Cost of waiting (90 minutes)
Pick three features you might build "just in case" (a cache, a plug-in system, multi-currency). For each, estimate what adding it later would cost, and decide.
Done when: a table has cost now, cost later, risk, and a decision for each.
Hint
A feature easy to add later is a good one to postpone.
Hard 3: Balance the rules (90 minutes)
Write a half-page memo for a new team: when do SOLID, DRY, KISS and YAGNI pull in different directions, and how should they decide?
Done when: the memo has two real conflicts with examples and a recommended way to settle them.
Hint
Interfaces add structure, YAGNI asks for proof first.