views:

350

answers:

7

I'm thinking in terms of Objects here. I think it's important to simplify ideas. If you can explain this to a 6-year old, you can teach new programmers the difference.

I'm thinking that a cookie object would be apropos:

public class Cookie {
  public string flavor {get; set; }
  public int numberOfCrumbs { get; set; }
}
+44  A: 

Empty would be , looking into a Cookie Jar, and finding nothing.

NULL would be not having a Cookie Jar.

astander
+1 Good analogy!
Dan Diplo
Better than mine by far.
Michael Dorgan
I thought of this one... but then I thought that would be a `CookieJar` or `CookieCollection`. My co-worker and I were discussing this... and I said to him: I had a `cookie` today for lunch... but I ate it. (I instantiated a `Cookie`) and now I have empty space where the cookie was. However, since he had no `Cookie`, he had a null `cookie`. The `cookie` never existed, from his point of view... however, he knows the definition of the `Cookie`
Atømix
+1 Perfect analogy. Also note it's the shortest answer.
Bob Kaufman
Just to add on, it depends on if you're referring to an object as a container or not. Null means no container, empty means no contents in the container. The cookie itself can also be a container. An 'empty' cookie could mean it has no chocolate chips. A null cookie would mean there is no cookie.
adam0101
Good point adam... or perhaps instead of chocolate chips... I'd explain it in terms of crumbs... an empty cookie has no crumbs inside it... a null cookie was never baked. A `NULL` object doesn't take up space in memory ( apart from the reference or pointer ) An empty object has space reserved for it. Although the cookie jar is a simple analogy, I'm not 100% convinced that is the most accurate one.
Atømix
Neo had a null spoon.
adam0101
A: 

Typically, null refers to an unknown value, so if you had phone number, they'd know their classmates have a phone number, but not necessarily what it was.

Rowland Shaw
And the downvote was because... ?
Rowland Shaw
Not me, but do you think your 6 year old would get that. Maybe if the child has visited some programming lectures/classes? Maybe I was the slow one at 10? X-)
astander
I see the problem with explaining null is that it is sued as an abstract concept - a cookie jar contains a number of cookies, whether that zero, or a number greater than that, which is why I fear that analogy may be flawed. I think they'd get the concept of something being unknown, although maybe in this day and age, it'd be people's phone numbers...
Rowland Shaw
In this day an age... it would be "They know their classmates have a Facebook page, but it just doesn't exist yet." :-)
Atømix
+1  A: 

NULL is a marker for invalid like a blinking warning light or perhaps like a period at the end of a sentence showing that the thought/sequence is complete.

Empty just means that there is nothing in a container, but it could contain an object if you wanted to put something in it.

Michael Dorgan
Oh, I know what it means... but explaining it in terms of a 6-year old...
Atømix
+4  A: 

I think NULL means "I don't know"

Even if there is a cookie jar, if you can see inside, and count 0, then you know there are none.

If you are asked how many cookies, and you see the jar and it is not possible to look in the cookie jar, then you say "I don't know".

Jeremy
+2  A: 

Empty = Air

Null = Vacancy

Kasturi
I could see how you could apply this to rooms in a Motel/Hotel. A null room would be ready for occupancy, an empty room would be a room that is allocated (rented) but not yet occupied.
Atømix
or.. a null room would be a room that has been reserved previously by a customer but not yet allocated. if it is allocated after the user arrives it would be lazy loading!
Kasturi
+3  A: 

I'm a Common Lisp programmer, you insensitive clod!

(They're identical, by definition, in Common Lisp.)

Ken
+8  A: 

Empty means the cookie jar you're pointing to is empty. Null means you're not pointing to anything.

So if you're pointing to a cookie jar and ask a kid "What are the contents of the cookie jar that I'm pointing to?" then you'll get the answer that it's empty. If you ask that question without pointing to anything, well, then it depends on what language you're asking in. If your native tongue is Java, the kid gets confused and upset; if you're from a country where C++ is spoken, the kid crashes.

(And God help you if you accidentally tell them to interpret the refrigerator as a cookie jar.)

Jon Purdy
I like this answer. It best illustrates how you can explain the concept simply instead of just extending the analogy. Though, I feel that the CookieJar idea is a bit flawed ( unless it only holds one cookie ) the idea of including how objects point to an area of memory in here is quite relevant.
Atømix