Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Null is dangerously wrong. Languages that avoid it give a much nicer experience because you explicitly write your model considering which values can be missing.


And booom you just then reinvented a new concept of "missing" exactly as he said.

PS: To disembiguate I was speaking of the null a value in a nullable type. Maybe I got you wrong.

Also I guess Null is way less a problem in strictly typed language than in JavaScript for instance.


I didn’t reinvent anything, the concept of optional value is well ingrained in the real world. If you are registering on a website the email address is mandatory while the home address is optional. If you are sending a parcel the home address is mandatory and the email address is optional. If lazy programmers instead of modelling the optional concept in the type system use a null the result is the huge amount of null pointer exceptions that are riddling a lot of software and the useless “defensive guards” everywhere.


> If lazy programmers instead of modelling the optional concept in the type system use a null the result is the huge amount of null pointer exceptions

Null values and null pointers aren't fundamentally the same thing, though some programming languages may be implemented in a way such that use of one exposed the other. A language can have a Null value that never results in NPEs.


I think that's kind of his point - Optionals/tagged unions are "Null values", but only in the places where they semantically make sense.

The problem with nulls isn't strictly null references, in the sense of "a pointer that points to nothing" or "a pointer that throws a null pointer exception", it's that languages with nulls implicitly make null an element of every type. They tend to do this because variables are allowed to have null references, but if you changed the semantics of variables to technically forbid null references, but still had a null value as an implicit element of every type, you'd still have the problem of needing to do runtime checks for null everywhere.


But how do you handle those optional values then? Let's say you run a classifieds site, some of your users will probably prefer not to specify a price, it has to be an optional value. Or age field on a dating sites. How do you filter that data, or handle sorting on that field, without using null values to mark those that are undefined?


That has been done with option types in functional languages since at least the 80s. It's not hard.


e.g:

  sum = 0
  for entry in entries:
    case (val):
      null: pass
      number n: sum += n
    endcase
in pseudocode. Several languages have offered this since forever (eg. Haskell and Maybe).

The fun part is that the typesystem enforces that you check all possible cases -- or only lets you explicitly un wrap potentially null values.

So it's not like a C programmer checking if (x == NULL) manually.


Option types and comprehensive pattern matching in F#. If you just need to filter them you can simply use Seq.choose.


Or you write sunny day code and wonder why everyboy elses code is riddled with those strange defensive structures.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: