DRY
DRY prevents the same business knowledge from being maintained in several places. Similar-looking syntax is not automatically the same knowledge; extract a shared rule only when the reasons to change are the same.
What you’ll understand
- DRY prevents the same business knowledge from being maintained in several places. Similar-looking syntax is not automatically the same knowledge; extract a shared rule only when the reasons to change are the same.
- Both callers use one discount function parameterized by the current rate.
- Evaluate the tradeoff: A shared rule reduces drift but couples its callers. If promotional and accounting discounts have different policies, separate them rather than forcing one universal helper.
Useful first: Classes and Objects
The idea and when to recognize it
DRY prevents the same business knowledge from being maintained in several places. Similar-looking syntax is not automatically the same knowledge; extract a shared rule only when the reasons to change are the same.
- Requirement: A cart summary and a receipt both calculate the discount on the same subtotal.
- Change to anticipate: Change the discount rate once and obtain matching totals in both outputs.
- Design goal: Both callers use one discount function parameterized by the current rate.
Start with the concrete problem
A cart summary and a receipt both calculate the discount on the same subtotal.
Change the discount rate once and obtain matching totals in both outputs.
| Direct implementation | Revised design |
|---|---|
| Updating the cart discount while leaving a copied receipt formula at the old rate creates inconsistent totals. | Both callers use one discount function parameterized by the current rate. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | A shared rule reduces drift but couples its callers. If promotional and accounting discounts have different policies, separate them rather than forcing one universal helper. |
Responsibilities and contracts
Both callers use one discount function parameterized by the current rate.
- DiscountRule: Owns one pricing rule. Public operations: apply(total,percent)
- Cart: Requests the current rule. Public operations: total()
- Receipt: Uses the same policy. Public operations: total()
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A shared eligibility rule can keep an enrollment preview and final enrollment decision consistent.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
A shared rule reduces drift but couples its callers. If promotional and accounting discounts have different policies, separate them rather than forcing one universal helper.
Common mistake: Updating the cart discount while leaving a copied receipt formula at the old rate creates inconsistent totals.
- Explain the choice: Both callers use one discount function parameterized by the current rate.
- Challenge your choice: A shared rule reduces drift but couples its callers. If promotional and accounting discounts have different policies, separate them rather than forcing one universal helper.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…