149
submitted 2 months ago by tfm@europe.pub to c/programmerhumor@lemmy.ml

cross-posted from: https://programming.dev/post/33754840

new Date("wtf")

you are viewing a single comment's thread
view the rest of the comments
[-] mogoh@lemmy.ml 5 points 2 months ago

Ok, I get that the Date API is problematic, but I wouldn't expect anything meaningfull from new Date("not a date").getTime() anyway. Why would you in the first place?

[-] blaue_Fledermaus@mstdn.io 13 points 2 months ago

Parsing user input? Nonsense data coming from an API?

[-] pixely@lemmy.world 0 points 2 months ago* (last edited 2 months ago)

If you’re expecting shit data then you’d have unit tests for those cases so you’d know what to expect.

[-] WildPalmTree@lemmy.world 4 points 2 months ago

Maybe you don't expect shit data; you just get it. I know, always expect it....

[-] Ephera@lemmy.ml 7 points 2 months ago

It's mainly horrid, because it means you have to code extremely defensively (or I guess, use a different API).
You can't rely on new Date("not a date") aborting execution of your function by throwing an error. Instead, you have to know that it can produce an Invalid Date object and check for that. Otherwise a random NaN shows up during execution, which is gonna be extremely fun to try to find the source of.

I understand that it's implemented like that partially for historical reasons, partially because it's often better to display "NaN" rather than nothing, but it's still the sort of behavior that puts me in a cold sweat, because I should be memorizing all kinds of Best Practices™ before trying to code JavaScript.

[-] scott@lemmy.org 4 points 2 months ago

You think you'd get an error from the constructor.

[-] WildPalmTree@lemmy.world 2 points 2 months ago

Because this is a simplified example? Maybe you create the object in one place (saying something more realistic like "2015" or whatever your inexperience or AI told you to) and use getTime() at a later place where you thought you created it in a correct way.

[-] Ephera@lemmy.ml 0 points 2 months ago

(saying something more realistic like “2015” or whatever your inexperience or AI told you to)

User input is probably the big one where this API is gonna get stress-tested...

[-] PeriodicallyPedantic@lemmy.ca 2 points 2 months ago

Because reasonable APIs have input validation provided out of the box, so that not everybody has to reinvent the validation wheel (inevitably incorrectly).

this post was submitted on 12 Jul 2025
149 points (99.3% liked)

Programmer Humor

38555 readers
291 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS