Friday 18 December 2020

Error Patterns - mistakes so common they become a pattern

What's wrong with this pseudo code?

"We need to get all entries for a given date, or today if the date isn't specified"

@controller

EntriesDto getEntries(Date searchDate=Date.now()){}

I made this mistake a couple of times before it properly sank in, and I just ran into it again. If you're still looking at it thinking "nope, can't see a problem", it's the assumption that the consumer and the api are in the same locale. This will work a lot of the time, but around midnight on certain days in certain months the result could be a day earlier than you expect, as daylight saving switches over.

Another daylight saving error I've run into more than once is:

"if we consistently use UTC we don't need to worry about daylight saving".

It's better, but you still do need to think about it. Because although you might be using UTC consistently you can be certain your end user isn't. Somewhere around there will have to be some conversion. I usually try to do that conversion at the view, because obviously, but where you're using some variant of Date.now() in end user client side code (e.g. browser) you're going to run into problems, because the client machine will have a locale setting convenient for the user.

I couldn't see that this class of error has a name, so I'm referring to them as 'Error Patterns', code errors that happen repeatedly in a consistent way.


No comments:

Post a Comment