
Getting rid of NULL - relaxnow
http://www.ibuildings.nl/blog/2016/01/programming-guidelines-php-developers-part-2-getting-rid-null
======
sebhack
Good article. Suggestion: When you post an article like this to hacker news it
would help to include the programming language in the title, like "Getting rid
of NULL (in PHP)". I wasn't aware that this is a PHP-related post until I've
read the first bunch of paragraphs until you actually mention "PHP".

~~~
diggan
I'm not sure that's necessary, it's not a PHP specific post. The author is
just using PHP to demonstrate his point. Most, if not all object oriented
languages should be able to replicate this functionality.

~~~
jcyw
I was expecting Java8 Optional...

~~~
iamrohitbanga
Or Ruby 2.3's lonely operator (&.) or swift's optional chaining (?.)

------
EvanPlaice
Null is simply a marker for non-value. Meaning the variable os initialized (ie
not undefined) but the value hasn't been explicitly set.

To cheery-pick an example from the article.

> When your function usually returns an integer, return 0 instead of null.

0 carries a meaning that may be directly relevant to the context of where it's
used.

Using that as a default state will eventually cause unexpected side effects

Look no further than the kerfuffle caused by c using true==1 & false==0 to see
how this approach may cause unexpected bugs.

A more specific use case that wasn't mentioned is bool. I ran into this issue
in one of the libraries I wrote. It contained inline hooks into a parser where
user-defined code could be injected or returned false if the hook is unset.

Unfortunately, eventually I had a user that was using the hook that needed to
return the value false. The only way to fix it with out breaking further edge
cases would be to use null.

Null doesn't exist because of environments where the dev can easily reason
about how the code will be used. It's used for code that will be used by
others where it's not easy to reason about all of the possible use cases.

With that said, I think languages should have built-in null checking support
of some form. Requiring if (val == null) checks everywhere makes its usage
seem like an anti-pattern.

Null has a clearly defined and useful purpose. Even if most devs never have to
explicitly set a value to set a value to null themselves.

------
ranko
See also the null object pattern:
[https://en.wikipedia.org/wiki/Null_Object_pattern](https://en.wikipedia.org/wiki/Null_Object_pattern)

