5 ms·
I prefer to expect nothing to be nil and assert for this rather than write defensively in case it is accidentally nil. This requires a more structured approach
by hellofunk 8y ago
I prefer to expect nothing to be nil and assert for this rather than write defensively in case it is accidentally nil. This requires a more structured approach but is a better solution for me. I don’t like nil and prefer not to think of it as a valid value at all. Too many null pointer exceptions has ruined my appreciation even for the bandaids described in this article. It’s the only pain point I have in Clojure. I just think nil is a terrible idea in any language, and the way it is overloaded in Clojure is among the most troubling among languages, in my experience.
- sz4kerto 8y agoYou have to deal with it in some way. If a function should return a boolean and something bad happens then it can return Union<boolean, failure> or Optional<boolean> or nullable boolean or throw an exception. The difference is basically in compiler assistance. Null pointers are bad because the lack of good static analysis support.
- hellofunk 8y agoBut you are talking about a statically typed way of handling nil. Options and Unions are not nil, which is the point. But you don’t get this in Clojure and nil can mean a lot of different things in different contexts, which is more complicated than a simple optional.
- degrews 8y agoSo how _do_ you handle nil without those things? You've said you try to just avoid it, but many functions return nil and there's no easy way around it (e.g. trying to get the max value of an empty list)
- qop 8y agoRepresent the problem programatically. If there's some reason data is missing, use a symbol :idk, :not-provided, etc. Programs DO things, use what you're doing to describe your program, whenever possible. In a better world, max([]) # -> None(T) But sometimes we have to get creative...
- hellofunk 8y agoFunctions only return nil when nil is a valid value in your logic.
- 3pt14159 8y agoWhat do you do when you don't know a value? Say you're making an application that tracks corporate fraud and one of the changes that you need to make is to include the date of incorporation onto the corporation model. For the existing corporations in your database what do you do? What I see people do in the wild is to use a value like 1900-01-01. Or they make a ton of tables so that the nil is now just a non-existent key instead of a non-existent value. I dislike nil as well, and I try to avoid it if possible, but I can't understand how to handle certain situations without it or without re-creating it.
- hellofunk 8y ago> What do you do when you don't know a value? I use an explicit value to represent the absence of something specific like :id-unknown which is much more useful and safer than just throwing nil in there when a project scales. If something is nil in Clojure, you have to ask why is it nil? The answer is not always simple. It could be a bug, it could be legit missing data, a number of things. A simple typo in Clojure can give you nil. You don’t want to interpret that the wrong way.
- 8ytecoder 8y agoWell, now this id-unknown would be your new null and on top of it it changes by the data type as well.
- hellofunk 8y agoI disagree. A keyword is not a null. And nil is a different “type” than something not nil anyway.
- maxilulu 8y agoIt will not crash the program and is domain specific. Definitely an improvement over nil.
- MaxBarraclough 8y ago