This is a post in my series on things I’ve remarked on in code review.
You constantly make simplifying assumptions in code. Like this one:
1 2 3 4 5 6 7 8
The assumption in itself is a good thing: we shouldn’t design for multiple currencies if we don’t need to.
But it’s dangerous that the assumption is left unchecked. It may be true today, but will it be true tomorrow? Mixing up currencies can have pretty dire consequences.
There’s a simple solution. Make your assumptions explode!
1 2 3 4 5
You still reap the benefits of the simplifying assumption but can also trust that it remains valid.
If you make the same type of assertion in multiple places, you can of course extract it, or find some gem. We do
CurrencyVerification.assert_single_currency(records) in one project, for example.
Assertive programming is a thing, but I haven’t see it a lot in Ruby outside tests.