One of the most frustrating things about null is that it has so many possible meanings:
- We don’t plan to provide a value here, so use a default instead
- We plan to provide a value, but memory for this value hasn’t been allocated yet
- The memory has been allocated, but we haven’t attempted to compute/retrieve the proper value yet
- We are in the process of computing/retrieving the value
- There was a code-level problem computing/retrieving the value
- We successfully got the value, and the value is “the abstract concept of nothingness”
- or the value is “please use the default”
- or the value is “please try again”
And so on. “Null” probably has more different meanings based on context than the word “fuck”.
Many of these meanings seem to be captured in some modern solutions already:
- We plan to provide a value, but memory for this value hasn’t been allocated yet.
- The memory has been allocated, but we haven’t attempted to compute/retrieve the proper value yet
- We are in the process of computing/retrieving the value
Futures?
- There was a code-level problem computing/retrieving the value
Exception?
Result
monads? (Okay, yea, we try to avoid the m word, but bear with me there)- We successfully got the value, and the value is “the abstract concept of nothingness”
An
Option
orMaybe
monad?- or the value is “please use the default”
- or the value is “please try again”
An enumeration of return types would seem to solve this problem. I can picture doing this in Rust.
Yeah, for this reason null shouldn't be part of any production code. If there's the possibility of having a null value, you need to check every variable or returned value to be safe.
These monads tell the consumer of your functions to do something (a check for emptiness or wait for it to be ready, or iterate it) to access the value inside. In a safe language, if the value is not wrapped by a monad, then you should expect to access it without issues.
I kind of get why people don't want to call them monads, since it sounds like a heavy term and more things to learn that are not strictly "necessary", but the earlier you learn about their importance, the earlier you can use any of their benefits in your codebase.
Null was added to JavaScript because Java had it. Null is unnatural. Undefined is the canon "no value" value.
You know exactly what that picture is, and it's how I prefer to hang my toilet paper rolls because I'm in the small minority that finds it more aesthetically pleasing.
Isn't the undefined one incorrect? From what I know undefined means it can contain whatever value. So it could be no toilet paper, full toilet paper or anything in-between.
This is JavaScript undefined specifically where it refers to a key that isn't present.