0

Possible Duplicate:
What is the purpose of null?

Is there an actual need for NULL or not? In most of the OO languages i have programmed in there has always been a way to set a variable to a NULL value which lead to all sorts of funny problems.

What are your thoughts?

Community
  • 1
  • 1
amit-agrawal
  • 1,563
  • 2
  • 13
  • 24

4 Answers4

3

NULL is a little like God. If it didn't exist, we would wind up having to create one. Something has to represent the value of a reference that is unassigned (whether that be because it was never assigned or it was cleared at some point). The only alternative is to use an object that, effectively, substitutes for NULL. The problem with that is that if you did all that to avoid the NullPointerException, now you're going to simply replace it with UnexpectedObject exception or ClassCastException or what not.

nsayer
  • 16,925
  • 3
  • 33
  • 51
  • Definitely +1 for the Voltaire reference. – Charlie Martin May 08 '09 at 23:15
  • +1 ha ha.. that's sort of funny, but true I guess.. – Mike Dinescu May 08 '09 at 23:17
  • 1
    NULL is a little like man also. For God (Anthony Hoare) created man (NULL) and then a few years later wished he hadn't. – Daniel Earwicker May 08 '09 at 23:38
  • 5
    Downvoted. Please try a language of the ML family before telling such a thing. Of course you can create a function that will throw on Nothing/None, but as you hint by doing so you just basically throw away the additional safety. Also, keep in mind that usually most of your references are in essence not nullable. By removing Null, you get to remove useless checks in your program and by introducing Nothing/None (which is NOT equivalent) you get to insert the checks were they matter (with assistance from the compiler so you don't forget any). – bltxd Nov 09 '10 at 07:33
1

It's possible to design a language that doesn't have a NULL but instead uninitialised values point to a singleton dummy object that doesn't actually do anything. You could compare pointers against the reference of this dummy object, and calls to methods on the object would result in no action or a runtime error.

This technique is hard to implement for statically typed languages like C++ or Java.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Yeah, apparently (dynamically-typed) Objective-C does nothing when you call a method on a nil object, though it does have nil. – Drew Hoskins May 08 '09 at 23:24
1

In languages with garbage collection where variables are actual storage locations (as opposed to Python's labels), the NULL value is required to allow memory to be freed in a clean manner before the end of the variable's scope.

Also, even many algorithms written in pseudo code make use of the special NULL value. It pops up literally everywhere. It is a central concept in computer science.

Stephan202
  • 59,965
  • 13
  • 127
  • 133
0

There are normally two special values. Some languages handle both, some only 1 and throw an error with the other, and some merge the two. Those two values are Null and Undefined.

  • Undefined would be trying to use a variable that flat out doesn't exist.

  • Null would be trying to use a variable that exists but has no value.

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. Take Java for instance:

If you did not have null, what if you did a lookup in a HashMap for something that didn't exist in the Map? What would you return? If you returned an Object then how would you know wether that Object meant nothing was there or this was what was actually in the Map. Workarounds could include creating your own "NON_EXIST" Constant Object, but thats essentially the same thing as what null already is anyways. Another workaround might be throwing an Exception. Now you're looking at major performance impacts.

Again, its the idea of having a guaranteed allowable value that you can use. It is always available to you and its always disjoint from the set of "real values" that would normally return from an operation you're performing. Null is therefore intentionally used to mean something special.

Like I hinted before, there are also optimizations that can be done here as well because Null is a special value. In languages like Java if you originally referenced an Object solely by a variable, then set that variable to null you're removing that reference and allowing Java's Garbage Collector to collect that now unreferenced data. If instead you let that variable sit around forever that hunk of memory that may never be used again will continue to hold resources. This is a contrived example, but it proves a point and in some resource intensive Java programs you will see these explicit assignments to null.

Joseph Pecoraro
  • 2,858
  • 1
  • 20
  • 25
  • Your example is a non-problem, as it's a very-common failure of the SRP. If the object doesn't exist, the hash-map should throw an exception. There's no major performance problems, because there should be a complementary `Exists` method. This kind of abuse of `Null` gets my goat. Enormously common, though, as it's so convenient. – nicodemus13 Dec 10 '12 at 15:52