3 ms·
> it's such a joy when you define a variable of type Foo and know it won't contain null Why is that? I've never seen a good explanation of this point of view.
by jjav 3y ago
> it's such a joy when you define a variable of type Foo and know it won't contain null
Why is that? I've never seen a good explanation of this point of view.
This all feels like just syntactic sugar to me. Sure you might not have null, you might have an empty object or any other variation. At the end of the day, if a value was expected to be available but is not available (for whatever reason), reliable code is going to have to deal with that absence. No matter how it is expressed.
- doctor_phil 3y agoThe point is that every function is explicitly telling you if it can or can not return null. Your editor and typechecker will tell you if you need to handle null after calling that function. If you have a non-null returning function that you need to change so it can return null, then your typechecker will tell you all the places where you now need to handle the new null return. It doesn't need to be a very large codebase before this becomes a very useful tool to help when refactoring.