DRY and Shared Rules
DRY and The Pragmatic Programmer
BOTIE
That framing matters. Two calculations can contain similar syntax while expressing different policies. Sharing a greenhouse safety limit is useful because the reports must follow the same decision. Combining an unrelated comfort limit merely because it is also a comparison would make later changes harder to understand.
Sources for this history
Why Did the Evening Report Keep the Old Alert Rule?
The greenhouse team changes its alert limit. Botie updates the morning report, but the evening report still uses yesterday’s value. The same policy was copied into two programs, and only one copy changed.
We need one authoritative place for the rule. Both reports can call the same function and receive the same current setting. The useful question is which pieces must change together, rather than how many lines happen to look alike.
The shared wire makes one policy visible. In Python, DRY also concerns the shared calculation and other knowledge that must stay consistent.
Step 1 of 1
Which changes belong together?
Compare the reason for changing each rule before deciding what to share.
| Rule | Why it changes | Owner of the decision |
|---|---|---|
| morning safety alert | greenhouse safety policy changes | shared safety rule |
| evening safety alert | the same safety policy changes | shared safety rule |
| visitor comfort warning | visitor comfort guidance changes | separate comfort policy |
morning safety alert
- Why it changes
- greenhouse safety policy changes
- Owner of the decision
- shared safety rule
evening safety alert
- Why it changes
- the same safety policy changes
- Owner of the decision
- shared safety rule
visitor comfort warning
- Why it changes
- visitor comfort guidance changes
- Owner of the decision
- separate comfort policy
Find repeated knowledge
The reports keep their own observations while depending on one definition of the alert limit.
Use One Alert Limit for Both Reports
The morning readings are [18, 24, 27] degrees Celsius and the evening readings are [24, 28]. Both reports must use the same alert limit. At 24 degrees, each report contains two alerts.
BOTIE
Morning and evening alert counts
- morning
- 2
- evening
- 2
Follow the operations
1. Morning Celsius readings
- 018
- 124
- 227
2. Shared alert limit in Celsius
24
3. Keep readings at or above the limit
- 024
- 127
4. Count items
2
5. Evening Celsius readings
- 024
- 128
6. Keep readings at or above the limit
- 024
- 128
7. Count items
2
8. Collect morning and evening counts
- morning
- 2
- evening
- 2
9. Result
- morning
- 2
- evening
- 2
The shared wire makes one policy visible. In Python, DRY also concerns the shared calculation and other knowledge that must stay consistent.
1. Find repeated knowledge
Repeated knowledge can be a comparison, a unit conversion or a configuration value. If the same business decision appears in several places, a future change must find and update all of them.
Move the shared rule into a named function and pass shared configuration through an explicit boundary. Merely replacing a repeated number with a vague global variable can leave its ownership and lifetime unclear.
Ask who owns the decision and which callers must follow it.
2. Give both callers the same operation
A shared function makes a rule reusable. Its arguments let different reports supply different readings while using the same policy. The caller still chooses the current limit, rather than embedding another copy in each report function.
Changing the rule has one implementation to review; changing a setting has one authoritative input.
Reuse an inclusive predicate
Both reports use the same comparison while receiving different measurements.
def reaches_limit(value, *, limit):
return value >= limit
reports = {"morning": [18, 24], "evening": [24, 27]}
limit_c = 24
counts = {name: sum(reaches_limit(value, limit=limit_c) for value in readings)
for name, readings in reports.items()}
print(counts){'morning': 1, 'evening': 2}The report names and readings differ. The inclusive policy and its current limit are shared.
Check yourself
Questions on Parts 1 to 2
Question 1 of 2
What dependency caused this inconsistency?
Question data
Morning and evening reports follow the same safety policy. Only the morning code was updated.3. Leave unrelated policies separate
Similar code does not establish shared meaning. A safety alarm and a visitor comfort notice may both compare temperatures, yet one may include equality while the other does not. Their settings can also change independently.
An abstraction with many switches for unrelated policies can be harder to maintain than two short explicit functions. Extract shared knowledge when the agreement is real. Do not force callers through a general framework to avoid a few ordinary lines.
Some repetition is cheaper than the wrong dependency. Revisit that choice when actual requirements reveal a useful common operation.
4. Check every caller after a shared change
A shared rule expands the effect of a change. Test both reports with a boundary value, then try another limit. Verify that empty reports keep their own identity and do not inherit a previous count.
Behavior tests establish the returned answers. They do not prove that an implementation has no duplicated knowledge, so review the source as well: can we identify the single rule both callers should use?
The workshop changes one supplied limit and asks whether both branches follow it.
Check yourself
Questions on Parts 1 to 4
Question 1 of 4
Which cases should be tested?
Question data
Both reports call count_alerts(readings, limit_c=current_limit). The shared limit changes.Try it with Botie
Build with Botie
Write and run Python