| ▲ | bunderbunder an hour ago | |
Empty string is not a valid alternative to null because the empty string is still a string. There are valid cases where “this value is not set” and “this value is set to an empty string” mean two different things. There are even more valid cases where “this integer is not set” and “this integer is set to 0” are different things. It’s also a type safety concern. Using sentinel values is comparable to the “billion dollar mistake” version of nulls because it creates a special value that implicitly exists and is not part of the variable’s type definition. This means the compiler can’t help you remember to check it. You have to just remember, and forgetting is a bug. With optionals (including opt-in nullability like in Kotlin), on the other hand, the compiler knows when you do and do not have to worry about a value not being present, and can raise a compile time type error when you forget. | ||