NullIsObject

Misconception:

The null value corresponds to an object held in memory. If a variable has the value null, it means that it is referecing a null object in the heap.

Incorrect

null is an object

Correct

null is a reference pointing to no object

Correction
Here is what's right.

The value null is a primitive value that represents the intentional absence of any object value. This means that that it is not an object in the heap.

Consider this snippet of code:

let obj1 = {};
let obj2 = null;

It’s important to distinguish the two lines. The variable obj1 contains a reference to an empty object in the heap. On the contrary, variable obj2 does not reference any object, because it is set to null. This means that the code above introduces two variables but only allocates one object in memory.

Origin
Where could this misconception come from?

Students may have prior exposure to languages like Smalltalk or Self, where every value is an object. In a language like Self, nil indeed is an object. (Self’s nil takes the same role as JavaScript’s null.)

Symptoms
How do you know your students might have this misconception?

Watch a real student who might have this misconception solving a programming exercise!

Value
How can you build on this misconception?

This misconception provides a good opportunity to introduce the dangers of null, as explained by its inventor: Turing Award winner Tony Hoare introduced the idea of a Null reference in ALGOL W back in 1965 “simply because it was so easy to implement”. In his QCon London 2009 presentation he talks about that decision and considers it his “billion-dollar mistake”.

The “Null Object” or “Sentinel Object” idioms, or the Nothing case of a Maybe or Optional monad, all represent the absence of a value with a special object, and represent a way to fix Hoare’s billion-dollar mistake. Thus, it’s really not that crazy an idea to consider null to be a special, singleton, object.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.