3 ms·
Null may have technical meanings in languages like C++ (0) but essentially the conceptual meaning of a null is "No Value" in every language. It's a huge mistak
by crimsonalucard 7y ago
Null may have technical meanings in languages like C++ (0) but essentially the conceptual meaning of a null is "No Value" in every language.
It's a huge mistake in every language including SQL.
If you have an entity of a type. Say Int. What does NULL mean in context of that type?
X = NULL
Y = NULL + 1
It doesn't matter what language you operate in there is no NULL + 1, because NULL is not an INT. It just doesn't exist. So your programming language, if it's good, it should throw an error. If it's a shit it will continue to propogate the y deep into your program and you won't even know X was a null.
If you think a default to zero works imagine this:
X = IntFunctionThatErrorsOutAndReturnsUnknownValueAkaNull()
Y = X + 3
Does it make any sense to you that Y will be 3? X was unknown! You don't even know that the function errored out! Either IntFunctionThatErrorsOutAndReturnsUnknownValueAkaNull should have thrown an error or it should have returned a special type called an Optional Type. Throwing an error isn't ideal because you won't know something is wrong until runtime so the Optional Type is the better deal here. Either way neither approach is taken and instead you have a null and Y propagates through your program as if NOTHING happened.
Unfortunately SQL doesn't offer The Optional type or sum types therefore we are stuck with Null.
So if you do this in a language that defaults nulls to zeros:
x = getFirstSqlColumnAsList("SELECT * FROM integerTableWithOnlyNullValues;")
y = x[0] + 1
boom you get y a garbage value propagating through your entire program thanks to SQL nulls. SQL isn't innocent here.
Now imagine Optional types with exhaustive pattern matching:
x: Maybe(int) = getFirstSqlColumnAsList("SELECT * FROM integerTableWithOnlyNullValues;")
y = x match {
Just x: Just(x[0] + 1),
Nothing: Nothing
}
If you can't understand why the above code is so much better than the previous code than you need to look up the Maybe Monad with algebraic data types. Basically the code above makes sure that there is Zero chance that an error is unhandled.
- ibrar74 7y agoYes, that's the problem this blog try to explain. NULL means "no value", but this is not true for all the languages. So you better know what is the value of NULL in the language you are trying to use.