logoalt Hacker News

slifintoday at 3:12 PM3 repliesview on HN

This is a case I never really thought about - if the key is missing today you'll get nil as the value and since Clojure is a nil punning language it usually does sensible behaviour in your program

I know this sounds unreliable but in practise I like a language that defaults to pragmatic code paths so I don't have to stay up at night imagining a million code paths

This adds a throwing codepath which is quite drastic so I'm glad people don't build this into programs everywhere - I'd be nice to hear what the team imagine as the use case for this

Normally for correctness I'd like to see specs at the boundaries for programs and different test suites for internal behaviours


Replies

kevincoxtoday at 4:45 PM

The problem in my experience is that while nil is a perfectly reasonable default 9/10 times that 1/10 happens often enough and causes major problems that it is worth taking the extra few seconds to write it explicitly in the code to acknowledge that case and that you have checked that it is fine in the 9/10 cases or handle it in the 1/10 case.

I have seen multiple major production outages in Golang code because people accidentally read a non-existent map key and used the default value. As a funny bonus in one of those cases we were stumped when debugging because this code had tests, but the tests were also reading the default values out of the map and asserting that "" was in fact a valid textproto (it always is!) so silently testing nothing.

So even if defaults are useful 9/10 times that 1/10 is so painful and expensive that it isn't worth it in my experience. The time spent responding to, debugging and fixing those outages far, far outweighed the time saved by the convenient default values in the 9/10 times.

xoxoliantoday at 4:49 PM

For me: documentation at the "front door" of an interface, especially in that long moment before you decide to add a spec or Malli schema.

show 1 reply
jimbokuntoday at 6:16 PM

This is really the kind of thing you want to fail at compile time which isn’t real possible in a dynamic language like Clojure.

show 2 replies