4 ms·
Clojure, like Ruby, does falseyness correctly in that there's only 2 cases of false: nil and false. Everything else is true. This is easy to reason about, and i
by Slackwise 4y ago
Clojure, like Ruby, does falseyness correctly in that there's only 2 cases of false: nil and false. Everything else is true. This is easy to reason about, and it allows for nils to "flow" through your logic (especially with nil-punning where in the context of collections, nil is also an empty sequence).
- tialaramex 4y agoSomething even easier to reason about: Only true is true, and only false is false!
- eropple 4y agoI disagree - presence/absence allows reuse of clear and terse operators, reduces the need for an extra null coalescence operator when logical or can do the job as well, and enables better developer experience in places like options hashes where "not set" always means the same thing as "false".
- happytoexplain 4y agoI think modern languages have demonstrated that the dev UX difference between allowing "falsey"/"truthy" and not allowing it can be reduced to be quite small. Meanwhile, consider that the bugs introduced by implicitly considering things false/true are arguably a type of "bad dev UX".
- eropple 4y agoI can't think of a case, personally, where conflating false and nil/null/undefined has created a bug. I can think of many for, like, JS treating the empty string or zero as false, but not for specifically null/undefined. Can you provide an example?
- capableweb 4y agoYou're talking about hard true/false, in which you are correct. The person you're replying to is talking about "Truthy"/"Falsy", which is different than the hard true/false you're talking about.
- happytoexplain 4y agoI read the parent as, slightly sarcastically, saying "it's easiest to reason when the concept of truthy/falsy is completely absent".
- deleted 4y ago[deleted]
- samatman 4y agoAdd Lua to the languages which chose to be sensible about this.
- ErikCorry 4y agoAnd Toit. Only null and false are falsey.
- normie3000 4y agoColloquially, “nil” and “zero” are the same thing. Presumably not the case in clojure.
- ARandomerDude 4y agoTrue, however in software there are many cases in which they're different. Simple example: How many checked luggage items do you have? nil means the user hasn't answered the question 0 means they saw the question and don't have any checked luggage.
- the_only_human 4y agoin your example, the end result is the same. the user is not interested in luggage.
- happytoexplain 4y agoThe difference between an answered and unanswered question is absolutely not the same. Nil means the user has not told us how much luggage they have. 0 means the user has 0 luggage. We can assume a lack of response means zero, or we can mark the field as required, in which case the UI needs to prompt the user for a response in the nil case but not the 0 case. This is an extremely simple, common example. There are many cases where nil is not equivalent to 0 semantically or logically.
- gryn 4y agono that's you assuming since that part was not filled.
- happytoexplain 4y agoI do not agree that treating nil (or zero, or an empty collection) as false is "correct".
- IshKebab 4y agoThat's doing it almost correctly. The correct way to do truthiness is not to do it at all. `nil` should not be automatically coerced to `bool`.