The Baseball Rule
Posted on April 19, 2024 • 2 min read • 364 wordsVery often, in a course of the engineering life, whether it is a process or code, repetition happens. I suggest a simple rule to decide what to do.
I have been using a simple heuristic to deal with slowly emerging patterns.
At 3 occurrences , I pause and review the context and approach.
Hence the name “Baseball rule”, a reference to the “3 strikes and you’re out” rule. (One of the very few things I know about Baseball).
Deciding not to do anything at this point is a valid outcome. Those 2, 3 occurrences could be all there is. Or the change could be slow and an investment now would only be amortized far in the future.
if price < 2000:
tax = price * 0.04
else:
tax = price * 0.06
if price < 2000:
tax = price * 0.03
elif price >= 2000 and price < 6000:
tax = price * 0.04
else:
tax = price * 0.06
tax_schedule = new TaxSchedule(...)
tax = tax_schedule.calculate_tax(price)
2 occurrences should feel like a tingle and there is often opportunity to easily remove duplication. However, in many cases it is too early to invest in more elaborate solutions.
The logic is similar when dealing with a process in general: