Basic question here - I have many lines of code that look something like:
var a = (long_expression == null) ? null : long_expression.Method();
I found this answer insightful.
By doing this assignment, you are propagating the null deeper into the system. You'll have to write your null handling condition again (and again and again) to handle these propagations.
Instead of allowing execution to continue with nulls, replace the null with a better representation (quoted from link)
- If the null represents an empty collection, use an empty collection.
- If the null represents an exceptional case, throw an Exception.
- If the null represents an accidentally uninitialized value, explicitly initialize it.
- If the null represents a legitimate value, test for it - or even better use a NullObject that performs a null op.
In particular, replacing a null collection reference with an empty collection has saved me many null tests.