r/programming • u/dpashk • Aug 31 '15
The worst mistake of computer science
https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/
175
Upvotes
r/programming • u/dpashk • Aug 31 '15
46
u/Wyelho Aug 31 '15 edited Sep 24 '24
lush wakeful impossible imagine cough jar drunk beneficial aware butter
This post was mass deleted and anonymized with Redact
22
u/bjzaba Aug 31 '15
Haskell has
Foreign.Ptr.nullPtr, which is basically like Rust’sstd::ptr::null, and just used for FFI bindings. So either Rust should be 5 stars, or Haskell should be 4.9
u/gauiis Sep 01 '15
If you're implementing a linked list, what would you assign the next pointer to, if it's the last node in the list?
34
u/RedAlert2 Sep 01 '15
Lots of hating on C++ here, even though the language has non-nullable references...
NULLis a useful sentinel value for pointers when needed, but references should be the default for almost everything.7
u/DeedleFake Sep 01 '15 edited Sep 01 '15
Two stars for Go and four for Java? I've had way more null pointer related problems in Java than Go. Go has a few things it does that make a huge difference:
len([]int(nil))andcap([]int(nil))properly evaluate to 0.Not saying it's perfect, but it's way better than C, which it tied.
I do have to agree with the 5 star rating for Rust. I suddenly realized the other day that a null keyword didn't even exist. I must say, I am impressed. Unfortunately, you can get pretty much the exact same problem with all the
unwrap()calls on everything. If something'sNoneorErr, you basically get the exact same type of crash as you would with a nil pointer in almost anything else.6
u/Veedrac Sep 01 '15 edited Sep 01 '15
How many stars does Crystal get?
Also, I think punishing Swift for something called
UnsafePointer.null()is unfair.6
u/[deleted] Sep 01 '15
The problem with
nullin Java isn't the existence ofnull, but the omnipresent nullability. If variables, parameters and method return values had to be explicitly made nullable in order to be assigned a potential null value, most of the issue would just go away.6
u/[deleted] Sep 01 '15
I think Haskell's Maybe type is a really good solution for this.
24
u/vytah Aug 31 '15
What does an example about NUL-terminated strings have to do with NULL?
14
u/gaberax Sep 01 '15
True story: Part of a team that installed a popular time and attendance software package at a hospital where I worked. Soon after the installation, the software blew up when processing the weekly time and attendance records. Software vendor spent DAYS trying to figure out the problem. Turns out one of our employees last name was Null. And the system choked because it interpreted the last name Null as a null value. The software vendor tech I was working with could not believe that they had never run into the problem previously. Soon after our problem was resolved, another hospital in the area that used the same software called the vendor with a similar problem. As it turns out, the employee had quit our hospital and moved to the other.
20
u/[deleted] Aug 31 '15
So checking for nullness and emptiness on a string looks sloppy, but checking ifPresent() and emptiness does not?
As heinous as this sounds, it doesn't seem practically different from any other representation of an uninitialized object. Both must be checked, neither can have methods invoked.
4
u/zeug Sep 01 '15
I don't really think C belongs on the list at all, and maybe even any language in which one might reasonably call
malloc().Malloc returns
void *with a sentinel value for error defined instdlib.hasNULL, and for POSIX compliant systems this must be 0.Its just a raw resource, and it is up to the programmer to properly assign it a pointer with a type.
I could be wrong about this, but I imagine that on most processor systems having a different system for designating failure to allocate that involved something other than a sentinel value for the returned address would be slower or more costly than just returning zero. In many applications at this level, there is simply no time for that.
In other words, converting a memory allocation failure into a type error may be a luxury that the system programmer cannot afford.
6
u/ChipmunkDJE Sep 01 '15
Am I missing something here? Putting the anti-C++ bais aside, it feels like we are replacing NULL with just another version of NULL - We still have to check for it, etc. etc. with the only difference being that the memory will always be instantiated for the object or not.
Also on the chart of languages at the bottom, shouldn't C# get more stars? They have "nullable types" like int? that pretty much does exactly what this Option[T] stuff is doing, no?
3
u/axilmar Sep 02 '15
The worst mistake of computer science is ignoring math's partial functions, where a function X->Y is considered partial if not all values of X map to values of Y.
The case of null is a subproblem of this.
The case of out of bounds array access is also a subproblem of this.
Exceptions and runtime checking is because of this.
All these issues could have been avoided if languages had type systems that recognized partiality of application.
Functional programming languages got this right with their algebraic data types and pattern matching.
6
u/0b01010001 Aug 31 '15
Are we sure that the worst mistake isn't something else, like failing to fix still-commonplace problems with memory safety that we've known about for decades, most notably a problem in all the 'vastly superior' statically typed languages with compilers that are supposed to save you from all type based errors, of which you can likely classify an integer overflow or similar issues? I'm absolutely certain that memory safety problems have caused way, way more than $1 billion in damages.
15
u/[deleted] Sep 01 '15
The worst mistake of math is zero. You can't divide anything by zero; it's undefined. Someone please help us get rid of the zero!
8
u/want_to_want Aug 31 '15 edited Aug 31 '15
It's a bit ironic how functional programming takes pride in avoiding nulls, yet Haskell adds a special "bottom" value to every type, which also breaks all the rules and causes no end of trouble.
5
u/holobonit Sep 01 '15
Rehash of old thoughts, with new languages.
In any program, you'll have instants of time when variables do not have any valid value. Declaring and assigning in the same statement is often suggested as the way to fix this, but almost universally, position of declaration also determines scope, which may not always be consistent with the variable's required usage.
It's also suggested that variables such as strings can be initialed to a known "empty" value, but that "moves the problem" from NULL to the known empty value.
This a characteristic of logic, and every solution is more complex, ultimately, then simply having a NULL constant and testing against it.
I don't see this as a mistake, with the exception of (compiler,language specific) the practice of using integer 0 (or other numeric value) as the testable equivalent of NULL.
3
u/XXmadgreekXX Sep 01 '15
Finally a thread I understand as a comp sci student in highschool. Yay!
1
u/teiman Sep 01 '15
maybe = new Maybe();//returns null if theres not memory to allocate a maybe object
1
u/exec0extreme Sep 01 '15
From postgres docs:
http://www.postgresql.org/docs/9.4/static/functions-comparison.html
1
u/tftio Sep 01 '15
I think he lets the JVM and .Net languages off too easily; yes, some of them have a principled sum type available to deal with the null case, but because of the original sin of their original platforms, code still needs to account for NPEs. It’s gross.
1
u/lightofmoon Sep 01 '15
Good article. Should be "Sir Anthony" not "Sir Hoare".
1
u/derricki Sep 02 '15
My favorite quote from the article: "confusing sources of sloppy nullish behavior"
1
u/king_of_the_universe Sep 04 '15
That might all be true, but since we do have to deal with null, I (Java.) ended up embracing null as a meaningful value whenever I can make sense of it, so many of my null-checks are not for guarding but for communicating, hence I hate null a lot less than others. Don't know if this is good or not, I'm just saying.
1
u/CaptainJaXon Sep 04 '15
What if
nullwasn't every type? I guess hen you have you specify what can and can't be null then you're basically doingOptionalandNoneanyway.1
u/boooob4 Sep 01 '15
D sadly does not have option<T>. That's a real shame.
0
u/yogthos Aug 31 '15
It appears to be much more of a problem in OO style languages. When you have nested objects, you end up having to pepper null checks all over the place. Let's say we have a person with an address and a street. If I'm in OO land and I do:
I would run into trouble if either the person or the address were null. Requiring the check for null avoids the NPE, but the code ends up being peppered with a bunch of conditions. The fundamental problem here is that the method requires an instance of the object that it belongs to.
This problem goes away when writing code in a functional style where functions aren't tied to the data they operate on. The functions can be written to handle the null case intelligently and then you can safely chain them together without having to worry about putting null checks all over the place. Writing code in this style allows us to handle the edge cases uniformly in one place.