30
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
this post was submitted on 06 Nov 2023
30 points (68.3% liked)
Programmer Humor
32745 readers
280 users here now
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
- Posts must be relevant to programming, programmers, or computer science.
- No NSFW content.
- Jokes must be in good taste. No hate speech, bigotry, etc.
founded 5 years ago
MODERATORS
One way to solve the problem is to give an error when you end up with an invalid input such as a data outside the range of valid dates for the month. The other way to solve the problem is to silently return nonsense which is what Js does. It's just a matter of doing basic input validation.
Except it's not nonsense. If you ask for 31 days after January 31st, you don't get February 28th.
A month is a malformed concept to use in conjunction with arithmetic, except for the part where people do it all the time and just ignore the fact that it often gets weird.
Do you really think you'd be happier if the answer for "what's a month from 01/31?” was "InvalidDateException"? That every other month the concept of "a month from today" is just undefined?
Saying "adding a month means adding the number of days in the starting month" is one choice of many, all of which have terrible downsides.
Are you seriously arguing that this is the behavior a person using this API would intuitively expect?
What behavior do you expect? Specifically.
I'm arguing that every answer is wrong, and will return bizarre results, be aggravating, useless or some combination therein for some conditions.
Therefore you have to know the API, because every language will fuck you, and JavaScript isn't special in this specific case.
Adding the number of days in the month to the date as "add one month" is just as rational as any other choice.
The behavior I would expect would be to throw an error when the date is outside of the range of valid dates for the month. If you try to create a date on February 31st throw an error. Have you seriously never used a good API that's intuitive and isn't full of gotchas?
Have you never done Datetime math before?
You didn't run
new Date(2019, 02, 31)
, you asked it for one month from January 31st.One month after any given day of the month is, by most people's intuition, a valid thing to ask for.
Your solution of making the API throw an exception for every 31st of the year is vastly less intuitive to me than "a month is 31 days in January, and 28 or 29 in February, so adding a month does different things in different months", because at least for those is can also query how many days are in the month.
If I say to add four days, will it throw an exception if it's the 31st? No month has 35 days, so I should get an exception, right?
Or is it just this weird caveat around months? Does it apply to time?
Nobody actually thinks this way, when you ask somebody what's the next month they don't go, oh today is the x day of the month and I'm going to add days to the current date to see what's the next month. That's not how vast majority of people think about this intuitively. Clearly you and people who designed Js Date API do though.
That's how months work. Each month has a different number of days.
Yeah, it's how months work.
In fact, this isn't a hypothetical argument this is how date APIs work in sane languages like Java. For example:
The fact that you think what Js API dies is preferable to this is frankly surreal to me.
So, you're talking about throwing exceptions if adding a month yields an invalid date if done without nuance, but then you're showing an example of just trying to instantiate an invalid date in java.
I believe java has an
addMonths
method, that in the situation we're talking about doesn't throw an exception, but rather limits the output to the 28th/ 29th/30th.Which illustrates my point: intuitive Datetime math involves choosing how you handle edge cases that are routine and not exceptional, like "a month from January 31st".
The rest of your comment arguing about how people expect months to work just makes me feel like you've never actually talked to a business stakeholder about requirements.
That's just insane. An API that doesn't increment the month when you pass the end while adding days is just broken.
I encourage you to actually try using Java API to see how it works if you don't think my example is illustrative enough. Meanwhile, the only insane thing here is you thinking that how an API works has anything to do with business stakeholders and requirements. This statement clearly illustrates that you don't understand how to translate business requirements into code and perhaps should spend some time learning how to do that instead of trolling here.