Spyke
lemmy.world

Best laugh I've had in a while. That is some grade-A jank.

29

This quiz was stressful. Like, there were so many times when I knew I was being cued up for a trick question, but I still fell for it.

24

It's a mess. Expectation is always wrong, basically.

11

Hmm, I can believe that it was based on java.util.Date, but I don't remember that being as unpredictable. I guess, a different API to begin with, would have avoided a lot of problems, though...

8

It is awesome, in the same way a planet killer asteroid heading straight for us is awesome!

14

You do not, under any circumstances, "gotta hand it to javascript".

Love this quiz btw

13
lemmy.ml

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?

5
pixelyreply
lemmy.world

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

0

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.

7

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

4

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

2
lemmy.world

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.

2

(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...

0

Until the new Temporal API comes along, there are some libraries we can use.

  • luxon
  • dayjs
  • or compile chrono (Rust) to WebAssembly
5

You reached the end

JavaScript is awesome | Spyke