Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't we have two nulls?

I've often wondered why languages with a null representing "no value" don't differentiate between the passive "I don't know what the value is" and the more assertive "There is no value.".

There have been several cases where I'd have liked to differentiate between the two (especially when working with user-input and databases).

I imagine the following, where we name the two states unknown and null:

var apple;  while (apple is unknown) {     askForApple(); }  if (apple is null) {     sulk(); } else {     eatApple(apple); } 

Obviously, we can get away without it by manually storing the state somwhere else, but we can do that for nulls too.

So, if we can have one null, why can't we have two?

like image 428
ajlane Avatar asked Feb 25 '09 09:02

ajlane


People also ask

What does it mean if data is null?

A NULL value is a special marker used in SQL to indicate that a data value does not exist in the database. In other words, it is just a placeholder to denote values that are missing or that we do not know.

What is null used for?

A null value in a relational database is used when the value in a column is unknown or missing. A null is neither an empty string (for character or datetime data types) nor a zero value (for numeric data types).

Why is null necessary?

Null can be useful because it is a guaranteed value that indicates that something is wrong, or outside of the domain/range of possible answers.

Can you compare a value to null?

You can't compare with NULL. You need is. null to test if something is a reference to the NULL object. @CarlesMitjans the variable is not always NULL, normally it has another integer value.


2 Answers

Isn't is bad enough that we have one null?

like image 146
Avi Avatar answered Sep 29 '22 21:09

Avi


In my programming, I recently adopted the practice of differentiating "language null" and "domain null".

The "language null" is the special value that is provided by the programming language to express that a variable has "no value". It is needed as dummy value in data structures, parameter lists, and return values.

The "domain null" is any number of objects that implement the NullObject design pattern. Actually, you have one distinct domain null for each domain context.

It is fairly common for programmers to use the language null as a catch-all domain null, but I have found that it tends to make code more procedural (less object oriented) and the intent harder to discern.

Every time to want a null, ask yourself: is that a language null, or a domain null?

like image 41
ddaa Avatar answered Sep 29 '22 22:09

ddaa