by TekMol · 4y ago · 0 comments
I just use what the native Date() object offers.
Other devs always say "Just wait, one day it will fall on your feet".
But this has been going on for many years, in which my software has served millions of users and nothing ever fell on my feet. I think the complexity of a library like this (423 files, 1433 commits, 53004 lines of code) would have created more problems during this time. So not using one is a net positive.
Any minimal code example of what is prone to break without a date library and how this date library is supposed to prevent it?
- https://infiniteundo.com/post/25326999628/falsehoods-program...
- https://infiniteundo.com/post/25509354022/more-falsehoods-pr...
In my experience engineers will often brush these sorts of things off as “edge cases”, but really all of these complexities are just how dates and time work in the real life (I’d argue that they’re just “normal cases” to test for)… So any anomaly in software will cause real problems of varying importance for users.
User: set an alarm in eight hours.
It's 10PM right now. At what time should the alarm be set? 6AM? Woops, tomorrow is the switch to DST! What now? User: remind me to brush my teeth on November 21st at 8AM.
Okay, will do! Wait, you took a plane on November 2nd and are now on the other side of the world? Should I remind you at 8AM or 5PM? User: please note my doctor's appointment in two weeks at 10AM.
Sure thing bub! Wait, but you live in Morocco in 2019? And the government just announced that reverse DST will be in effect for the month of Ramadan, which begins next week?! WTF do I do now?---
tl;dr It's not just an issue of storage. Managing time is hard. There are requirements that we, as humans, will intuitively find the right interpretation for, that computers will absolutely break their teeth on.
I mean, there is no way to write code to “handle” this that isn’t just guessing on the part of the programmer. The correct thing to do is to prompt the user for verification or further information.
> There are requirements that we, as humans, will intuitively find the right interpretation for…
Interpretation is subject to error. (Yes humans have immensely more context to work with than computer programs, but just because we we’re better guessers doesn’t mean we’re not still guessing.)
You could make an argument that there are more important things to fix, but then you’d be deciding consciously that it was okay to ship software known to be incorrect.
I'm not interested in long theoretical articles. But I would look at a few lines of code.
Here is an example of some code I recently wrote:
logEvent {
'date': new Date().toISOString(),
'event': 'image_updated',
'value': image['id'],
}
This logs that an image with a given id was updated.Why would I trade in my single date line for 423 files with 53004 lines of code?
Libraries come in handy when doing things that aren’t one liners.
A colleague thought it was super easy to change that exact ISO string to use the local timezone instead of UTC, against my advice. Guess what. The minus sign broke it and he would have never known because most customers are in the US, then they travel to Singapore and your product is worthless. By the time you hear about it, you lost the customer’s money and time therefore you lost the customer.
Moment and Luxon are too heavy, I agree, but date-fns isn’t.
Those articles are more empirical in nature than theoretical.
> This logs that an image with a given id was updated
As a thought exercise, imagine that your log trail needed to be created in a distributed system, spanning multiple machine time zones, or that you needed to build a report that is read from browsers in multiple time zones that differ from multiple differing written time zones, and you need to account for human readable things like knowing what day of the week it is and what some given locale calls that day.
If you are doing the equivalent of printf debugging with your times, maybe you likely don’t need a time library.
If you are using time as a central part of a distributed algorithm, web content, or a business workflow, then you may likely need a time library.
I heard it very often. I don't buy it. Any system, no matter how complex, can be composed of small, simple systems.
I never ran against a "complexity wall" when writing software in a lean way and suddenly thought "Damn, those tens of thousands of lines of library code now would be the better approach".
In addition, should the legal authority in timezone B change their DST rules in the meantime, the local time of the event as observed in timezone B should still be what the user initially selected, which is not the same in UTC as it was at creation time
What is one month ago when you are March, 31st?
We have to deal with monthly subscriptions and it's not fun.
"one month ago" is between "a few weeks ago" and "a few months ago".
>We have to deal with monthly subscriptions and it's not fun.
it is a lot less unfun if you define a month as N calendar days in your terms of service, like virtually everyone does
This is why we don’t let engineers write contracts. Many seem to think that law is a programming language in which they can redefine reality. It ain’t.
No library can help you with that. You have to define what you actually want to calculate.
Time libraries can help you with that.
And... it's a multi tenant system, where some organizations use Sunday as the start of a week, and some use Monday. And they all want to be able to switch.
The js date is a bit primitive and moment looks easier but in practice it's actually a convoluted clunky type system that hides important nuance behind impressively bad defaults.
I approach all of these "better than what the language offers" solutions the same way: it would be truly remarkable if some kid had a more nuanced and correct take then people with years of experience and PhDs in language design who made the language but more likely, I think the kid who wrote the library is just better at drawing cartoon characters, hype posting on social media and making something popular.
I used to get immediate hostility and belittling insults when I stated this about 10 years ago. I think people have finally come around. Let's see how the replies go.
That was a reply to a comment, not to the code posted. You're being sloppy imprecise and presumptive and expressing it as haughty snide arrogance.
Cool attitude
It's been like this for decades. Emotional salience sells software better than hard practicality to just about everyone. Then people rationalize their irrational choices then the timeline slips and the product sucks.
You can just sit and watch it play out like someone studying monkeys. It's remarkably predicable.