NULL

>> Go, ask the customer!

Null-pointer, NULL, null, nil or None as a return type for a function is useless at best, or a confusing, time-consuming and harmful null pointer exception at worst. As described on Wikipedia, a null pointer is designed to represent an invalid object and compares equal only to the null pointer type.

That implies: if we touch (dereference) a null-pointer our program goes BOOM. Our software can not handle an invalid state on its own. We as software developer have to tell the program what to do in that case, otherwise our software will fail spectacular or silently and our programs just crashes. Many developers do not know, care or just forget to handle a null-type / null-pointer-exception accordingly. Hence, relying on the calling function of handling the null type is an error in itself or, even worse, intended.

Null is a model error.

We model the needs and problems of our customer in software, The programs we write solve these problems and serves the needs of the customer. Thats how we design our software, we ask the customer what he wants and put that into software. Since programming is based on language and, if we do it right, the variable, types, functions and objects bear the wording domain specific language of the customer, e.g. then we have a BottleScanner class and a CarMatrix-Type. Then we have TemperatureSensor that reads data and calculate a forecast. Every word that lives in the language domain of our customer, sooner or later gets ported to our code base. And now go ask the customer: What is NULL?

When to use null?

We don't!

This description above shows us directly what the use case of a null-pointer should be: The moment a null-pointer is generated in our code base, exception handling should take over, i.e. WE DO NOT return null. If we see the possibility an object might be in an invalid state, we create the opportunity to branch the code flow to either shutdown gracefully with a domain specific exception or offer the caller a return type, that forces the caller to handle and recover from the invalid state or allows the caller to ignore it completely.