A little tale from the current project.
We have a front end with a date range search. We also have a back end which can do that search. No shocks so far.
For reasons of no particular important, we decided that the back end would take the range parameters as
<=. In other words, it’s an inclusive search for both parts of the range.
This less to an interesting challenge in the front end. The user could enter the target date as a year, a month/year or a day/month/year. That was a side effect of the UI widget we were using. So depending on what they entered, the less-than-or-equal-to had to be last second of the year, last second of the last day in the month, or last second of the whole year.
As it was, adding 1 to the date entered, in the unit it was entered, gave us the second AFTER the inclusive range, so we could reasonably have said
to date = to date + (1 * unit) - 1second – but the code then had more moving parts than necessary.
What if the back end could change?
Changing the back end to query
>= from and
< to was essentially a single character change (plus a little test tweaking). It made the back end no more complex, but reduced the complexity in the front end.
What Do We Learn?
It’s easy to get tunnel vision on your own internal specs and end up driving more complexity into the code just because you made an arbitrary decision that made something else a little harder.
We don’t need to adhere 100% to the letter of our specs if we can change them slightly and then adhere 100% to the spirit of them. A standard that prevents us from getting simple code is a bad standard.
We don’t need to injure ourselves over some spurious duty to an arbitrary perfection. Some standards are less important than others.
In this case, the algorithm was chosen just because. In fact the new version is a nicer way to do ranges, especially time ranges, not that it will make any difference in this use case.
By the way, this advice also works if you apply it across teams. Negotiating the right details of our standards to make everyone’s life easier is a good thing.