Understanding 'null'

I’m new to OOP. Having just read the excellent ‘a-quick-and-thorough-guide-to-null-what-it-is-and-how-you-should-use-it’ by Christian Neumanns, I’m still confused as the article doesn’t explain the reason for allowing a variable to have a null value at all.
This is what I was searching for, because I don’t see any point to declaring a variable that has no value. Maybe I’m dim or something but I just don’t get it. If a container is created to hold values then it must have a value simply because it’s been created, otherwise it’s not serving its function and thus there is no point having created the thing, whether it’s a reference or value type seems to me irrelevant.
Can someone put me straight on this with an explanation as to why it’s considered a good idea to allow null values. Seems pointless to me.

1 Like

Have you ever used an empty box? Think about it, if every box you ever saw was full, how would you then use it?

Null has many purposes.
Here is one example showing it being used

Edit: I just noticed you posted this in python sub forum. Sorry should have given you a python example of using None. Basically though, imagine that you are reading a record in a database table somewhere. Some columns may be required and some may be empty because they were optional fields. To indicate that something is empty a value like None (null) would be very useful. Or if you are implementing an API which is supposed to return some object only in some scenarios, when you want to communicate that the object could not be found/created then doing that with None is handy too. As you write more code, you will see it used more.

1 Like

Here’s the article in question for some context:

I quickly skimmed the article but noticed a few things that stand out that might help shed light on your question.

Note: This article is programming-language-neutral — as far as possible. Explanations are general and not tied to a specific language. Please consult your programming language manuals for specific advice on null . However, this article contains some simple source code examples shown in Java. But it’s not difficult to translate them into your favorite language.

The most important concept that null or whatever your language’s version of null is and why such a thing exists goes back to how data is managed in memory. This concept changes a little depending on the language your working with.

For example in C, you can directly access the memory via pointers. This allows programmers to set values at individual points in memory, allocate how much memory they will use and be responsible for cleaning up the memory they use. As said above its like having “buckets” to store information/data that you get control of.

However in a language such as Python or Java, you don’t get access to the actual memory, but this is only because the language itself will go and manage the memory for you automatically. Which is why there is mentions of a “garbage collector” being ran by Java within the article. This doesn’t mean your original variable doesn’t exist or is “nothing” but rather the variable has nothing in its “bucket”.

So this brings us back to null and the purpose of an “empty value”. Namely depending on the language a null value essentially allows you to create a variable (which can be your “bucket in memory” to store stuff) and have no value at that location.

Sure you could pass in data that you always overwrite, but that might not be necessary.

So here’s the thing with a lot of programming concepts. You don’t have to use it. It’s very possible to program “around” null values entirely where you never declare a variable and give it a value of null. This could be done for a variety of reasons, the simplest of which is well… simplicity.

However, it’s there for situations that make sense. For example in Python if you use a dictionary and try to get a key that doesn’t exist, what would be the value returned? I could be a string that’s “empty!” or 0 or something else that makes sense in this context, but why not have a specific data-type that represents “nothing”, hence it returns null.

Finally to bring things full circle a bit and to provide an example out of programming, think about the number 0 and counting. Why would the number 0 make any sense if we want to count things practically. No one cares if you have “0 apples” you either have apples or you don’t. So some early societies of humans didn’t have the concept of 0 at all. It wasn’t until later when numbers stopped representing actual physical things did the use of 0.

Thanks hbar1st for your reply.
First, I’m sorry I didn’t realise I’d posted in Python sub forum. I’m actually trying to learn C# and I find many issues explained on the web about null that would be completely avoided if declared variables were initialised or assigned at declaration, it also makes more sense (to me). For the time being that is how I will choose to use the language and avoid the null issue all together. But I am intrigued as to what real use it is to have a variable that does not have a value. I shall be using an API and your suggestion that I may well come across it’s use more as I progress, I note for the future. The only use I perceive is that when a value in a DB is nothing, ie an empty string or a zero and you run a printout, while the MT str will print nothing the zero will print a nought, which might be considered ugly. Perhaps I still have much to learn on this topic. Thanks. Vic

Thanks bradtaniguchi for your reply. I’m learning C# actually and finding it terribly complicated in lots of areas which seem unnecessary, not least is the contorted terminology. Yes I got it that it can be worked around by assigning values at declaration and for the time being i do intend to do that, but it intrigues me that the designer of the language wants specifically to have a null value. I also realise that I will have to learn how to use the language as is but I find that difficult. I’m not good at learning parrot fashion I have to understand or I just don’t get it. But thanks for your help. Vic

have you ever heard of linked-lists?

This data structure cannot be created without a NULL value in C.

Have you ever heard of B-Trees? Same comment as above.

Sometimes in learning a new language (even a spoken one), we have to “wave our hands” at a concept (or treat it like a magic box) until we have lived with the language a while. Why did English speakers invent the word “Hi” when they already had “hello”? (or pink when they already had red?) Just wave your hands at it, until you live with some people who speak English and then it suddenly makes sense.

Hope this metaphor is a useful one.
ps. in my parents’ culture, pink and red are synonymous. They can’t tell the difference! :grinning: Which drove me nuts as a child because they kept buying me red shirts when all I wanted was a pink one!! lol.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.