5 ms·
The author makes compelling arguments but I respectfully disagree with their conclusion. Null exists for cases where you want to set the the absence of a value
by vikingcaffiene 6y ago
The author makes compelling arguments but I respectfully disagree with their conclusion. Null exists for cases where you want to set the the absence of a value intentionally. It is literally the value of having no value. Undefined exists for cases where no value has yet been set. It's a subtle but important difference and allows a certain amount of nuance and expressiveness in ones code to communicate intent. I do think it's fair to say that a lot of devs don't use them that way. They _are_ for separate use cases though.
- Aeolun 6y ago> Null exists for cases where you want to set the the absence of a value intentionally. Only in Javascript though. In Typescript this would be clear from the fact that the type has the property. Of course Typescript is not Javascript, but nobody should use plain JS anymore.
- ZephyrBlu 6y agoWhat if you want your property to have a default value, such as null?
- chousuke 6y agonull is not a very good default value in most cases though, since the operations you do with values of "real" types will not work on null. Therefore actually using your "default" value is pretty much guaranteed to be a bug, identical to using an uninitialized variable. It would be better to just error out and not allow the uninitialized usage at all, but unfortunately that's not possible in Javascript.
- ZephyrBlu 6y agoThat's the point though. I can easily do a null check to handle whether or not to process/use the value. For instance, if I'm fetching some data I usually want to initialize the data variable as null for a couple of reasons. 1) null is not truthy, empty arrays and objects are 2) It shows that the variable is supposed to be filled, unlike undefined
- Aeolun 6y agoIf you want your property to have a default value, give it an actual default value, not the explicit absence of one (kind of defeats the point to set a default value of null).
- ZephyrBlu 6y agoIt doesn't defeat the purpose of a default/initial value because it shows that it should be filled. When I'm using hooks in React this is very common. You must give an initial value, and at some point in the future you will update it. By setting the value as null initially I can easily check whether the variable has been filled or not. This is most important for initially rendering the UI.
- someUserTonight 6y agoI think the parent comment was saying that you could always make that explicit. > By setting the value as null initially I can easily check whether the variable has been filled or not I don't think there's anything wrong with this but it does mean your variable can be in two states~, one where "it shows it should be filled" and one where the value is present and can be used. You can use null for this if you want and most programmers will understand these states. You can also choose to make this even more explicit by creating a type that expresses this. example in Kotlin but you get the idea (applies to any variable in any language) sealed class ThatVariableYouCareAbout { object ShouldBeFilled: ThatVariableYouCareAbout() data class HasBeenFilled(val x: String): ThatVariableYouCareAbout() } // usage fun test(thatVar: ThatVariableYouCareAbout) { if(thatVar is ShouldBeFilled) { // } else if(thatVar is HasBeenFilled) { } } Seems overly verbose but that's the reality of the variable you're talking about. This represents those states you described. `null` is a crutch in most languages for representing this state and the cause of a lot of subtle bugs (people use it to reset/clear out values too which in reality could represent a different state depending on what you _really_ want the variable to represent). All that being said, null is generally understood to have this meaning~ and I have/do use it but with an understanding that it's of lazyness/not wanting to be overly verbose (language consideration). The case you are describing is very familiar to me (rendering UI with initial states through a framework like react) and having an explicit type for the initial state is the ideal, or at least the most explicit, solution. Option works for 2-state variables like this
- fyfy18 6y agoIn functional programming the Option type exists to make the 'literally no value' case more explicit. https://en.m.wikipedia.org/wiki/Option_type https://en.m.wikipedia.org/wiki/Option_type If you are using TypeScript (TFA mentions it), fp-ts provides a good functional programming layer. Although if you aren't already familiar with functional programming it's going to be hard as the documentation is rather lacking. https://github.com/gcanti/fp-ts/blob/master/src/Option.ts https://github.com/gcanti/fp-ts/blob/master/src/Option.ts
- UncleMeat 6y agoThe Option type isn’t really something owned by functional programming. Union types exist just fine in imperative and even declarative programming.
- Spivak 6y agoAnd the magic sauce that makes Option nice to use is the type checker that can force you to handle the None case. I would go even further that even if the semantics are a little clunky (wrap/unwrap) any programming language can implement Option.
- jolux 6y agoTrue but you can’t restrict the type if your language is dynamic.
- c-cube 6y agoThe option type also prevents you from accessing the value if it's absent. Something a C union cannot do. Is there any example of options in an imperative language that would predate ML (which AFAIK introduced Some/None as the variants of option)?
- UncleMeat 6y agoC can’t do that. But imperative programming can. Nothing about type based pattern matching is constrained to FP.