r/ProgrammingLanguages • • May 25 '23

Question: Why are NULL pointers so ridiculously hated?

To start, I want to clarify that I absolutely think optional types are better than NULL pointers. I'm absolutely not asserting that NULL pointers are a good thing. What I am asserting is that the level of hatred for them is unwarranted and is even pushed to absurdity sometimes.

With every other data type in nearly every language, regardless of whether the language does or does not have pointers that can be NULL, there is an explicit or implicit "zero-value" for that data type. For example, a string that hasn't been given an explicit value is usually "", or integers are usually 0 by default, etc. Even in low level languages, if you return an integer from a function that had an error, you're going to return a "zero-value" like 0 or -1 in the event of an error. This is completely normal and expected behavior. (Again, not asserting that this is "ideal" semantically, but it clearly gets the job done). But for some reason, a "zero-value" of NULL for an invalid pointer is seen as barbaric and unsafe.

For some reason, when it comes to pointers having a "zero-value" of NULL everyone loses their minds. It's been described as a billion dollar mistake. My question is why? I've written a lot of C, and I won't deny that it does come up to bite you, I still don't understand the hatred. It doesn't happen any more often than invalid inputs from any other data type.

No one complains when a python function returns "" if there's an error. No one complains if a C function returns -1. This is normal behavior when invalid inputs are given to a language that doesn't have advanced error handling like Rust. However, seeing people discuss them you'd think anyone who doesn't use Rust is a caveman for allowing NULL pointers to exist in their programming languages.

As if this post wasn't controversial enough, I'm going to assert something else even more controversial: The level Rust goes to in order to prevent NULL pointers is ridiculously over the top for the majority of cases that NULL pointers are encountered. It would be considered ridiculous to expect an entire programming language and compiler to sanitize your entire program for empty strings. Or to sanitize the entire program to prevent 0 from being returned as an integer. But for some reason people expect this level of sanitization for pointer types.

Again, I don't think it's a bad thing to not want NULL pointers. It does make sense in some contexts where safety is absolutely required, like an operating system kernel, or embedded systems, but outside of that it seems the level of hatred is extreme, and many things are blamed on NULL pointers that actually are flaws with language semantics rather than the NULL pointers themselves.

0 Upvotes

90 comments sorted by

View all comments

7

u/mckahz May 25 '23

First off I wanna say kudos for making such an innocuous post and not deleting it in spite of the idiots on Reddit who downvote everything the disagree with.

With that said, I think don't any hatred for outdated features should be met with "but I can still write good programs with it," since that's the type of complacency that's allowed a language with fucking header files to exist in the mainstream for so long.

We have optional types and null safety now and the fact of the matter is that there's no good reason to implement null pointers into your language now. I'm not a low level programmer, and it's probably different when you don't have Rust semantics and still want to do very controlled low level code, but the whole point of having a type system is that you can guarantee stuff about your code. If my function returns a string, I should never have to ask "what if this string is secretly an error disguised as an empty string?" If it does work like that, then I'm not going to know that from the API, and now I can't use an empty string for a successful value of my function.

If your language is dynamically typed then there's not a whole lot you can do to avoid this, but that's exactly what's annoying about dynamically typed languages. When I use a value I shouldn't have to do anything to verify that the value is what I'm expecting it to be, it should just disallow people from giving me values I'm not expecting and as soon as you open up the flood gates for people to provide me with garbage I have to defensively check all the time. It's a horrible way to code, it makes it unreadable and you will forget, and you will get bugs as a result. Bugs which can be a bitch to track down without stepping through the debugger.

And here's my hot take- the debugger is the worst way to understand your code. If you need to run your code to understand what it's doing then your code isn't readable. It's hard to make it readable with C syntax and no ability to compose functions (like a pipe operator), and we should create languages that do their best to be readable without a debugger. Rust does a pretty good job at this, ML languages do a great job at this, but C does a terrible job. C++ is the worst case scenario.

That aside though null pointers and defensive null checking fly directly in the face of readability and self documenting code and I think it's fare that people complain about having to write so much bullshit lest their code is at risk of just crashing randomly. We're better than runtime errors, but when we need them it's best to make it explicit.

1

u/PurpleUpbeat2820 May 25 '23

that's the type of complacency that's allowed a language with fucking header files to exist in the mainstream for so long.

Are header files a bad idea? All of the languages I use have something similar and require files and declarations to be ordered.

4

u/mckahz May 26 '23

Yeah but I shouldn't have to specify that as a user. It's archaic and confusing. It's boilerplate for the sake of boilerplate at this point.

1

u/PurpleUpbeat2820 May 26 '23

That's really interesting. I think there are some interesting tradeoffs here.

1

u/mckahz May 26 '23

I'm not a C programmer, so for me and any other first time user they seem to only offer confusion and nothing else. But since I've only dabbled in C I'll ask this with an open mind- what trade offs are there? Do you think it's better to have them?

2

u/PurpleUpbeat2820 May 26 '23

I'm thinking beyond C but I'd say some advantages are:

  • Clear machine-verified API.

  • A central place for doc strings that IDEs can pick up on.

  • Sorting definitions make life easier and code clearer in some ways vs arbitrary ordering.

Disadvantages:

  • Code duplication between interface and implementation.

  • Docs could go on implementations only.

  • Lots of people see sorted definitions as archaic and want any definition to be able to access any other regardless of order and they want compilation units to be order independent.

2

u/mckahz May 26 '23

Thanks for the reply! I feel like you made all my counter arguments for me lol. I'd like to clarify a few things-

What do you mean by "clear machine-verified API"? Isn't that what a compiler / type checker is for? To verify your API?

In what regards does sorting definitions make life easier/code cleaner? Do you feel as though the restriction makes it so any snippet of code makes sense if you've already read everything above it? I could get behind that but why do you need header files for that?

Just my two cents here, but in my experience using languages where order matters (basically just Python), it only ever feels like it gets in my way when I'm trying to make a clean API, but as I said earlier I'm not really sure what header files have to do with that.

1

u/PurpleUpbeat2820 May 26 '23

Thanks for the reply! I feel like you made all my counter arguments for me lol. I'd like to clarify a few things-

What do you mean by "clear machine-verified API"? Isn't that what a compiler / type checker is for? To verify your API?

Yes, exactly. The interface file (e.g. header file) is your description of your API in your own words and the compiler's type checker checks it.

I use OCaml mostly. Without an interface file the compiler infers everything which is great in some sense (e.g. less typing) but also worse because the inferred types might not be what I expected and the discrepancy is caught as an error in code using my library rather than in my library.

In what regards does sorting definitions make life easier/code cleaner?

You know which direction to look in. You can supercede definitions.

Having everything potentially mutually recursive feels like untangling a rats nest to me.

Do you feel as though the restriction makes it so any snippet of code makes sense if you've already read everything above it?

Yes. The alternative is like the movie Tenet.

I could get behind that but why do you need header files for that?

I'm really conflating headers with forward declarations there.

Just my two cents here, but in my experience using languages where order matters (basically just Python), it only ever feels like it gets in my way when I'm trying to make a clean API, but as I said earlier I'm not really sure what header files have to do with that.

I must admit that I do find myself jumping through hoops to accommodate it sometimes. Mutually-recursive modules can be painful in OCaml.

2

u/mckahz May 27 '23

compiler infers everything which is great in some sense (e.g. less typing) but also worse because the inferred types might not be what I expected

I'm again not sure why you need header files to solve this, why not just add a type annotation?

Having everything potentially mutually recursive feels like untangling a rats nest to me.

It can definitely get a bit confusing, and I feel like this is a really good argument as to why having let rec for recursive functions may be nice. I never liked it before BC the compiler can infer it, but I like that you have to specify effects and errors in the type signature in pure FP languages, so maybe explicit recursion would be similarly helpful. I think you've just changed my mind on that!

I feel as though most of your arguments in favour of header files just boil down to you prefering ordered declarations, which is orthogonal to header files. I like the argument that it gives a centralised place for your doc comments, but that seems to be your only argument actually in favour of them. With that in mind I'd like to revisit my earlier question- do you think languages should have header files / do you think header files are good? Because from my understanding of your position, a language with ordered declarations or explicit recursion and type annotations would give you the benefits you're looking for in header files, except for the doc string example.

One note on the doc strings, I feel as though they should be located next to your code. The clutter sucks but they're way more subject to rot if they're in a different file.

1

u/PurpleUpbeat2820 May 27 '23 edited May 27 '23

I feel as though most of your arguments in favour of header files just boil down to you prefering ordered declarations, which is orthogonal to header files. I like the argument that it gives a centralised place for your doc comments, but that seems to be your only argument actually in favour of them. With that in mind I'd like to revisit my earlier question- do you think languages should have header files / do you think header files are good? Because from my understanding of your position, a language with ordered declarations or explicit recursion and type annotations would give you the benefits you're looking for in header files, except for the doc string example.

Perhaps the proof is in the pudding: none of my own languages have header files. They all have explicit recursion (not only let rec but also type rec because I like the consistency).

I never conciously thought about it before though: I just did what felt good.

One note on the doc strings, I feel as though they should be located next to your code. The clutter sucks but they're way more subject to rot if they're in a different file.

And I require docstrings to be on the implementations. :-)

→ More replies (0)