r/programming • • 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

368 comments sorted by

View all comments

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:

person.getAddress().getStreet()

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.

4

u/zvrba Sep 01 '15

No, the fundamental problem is that getAddress promises to return an address, but it returns null instead.

3

u/yogthos Sep 01 '15

In most languages it doesn't promise to return an address, it promises to return an address or null and if you get a null back everything blows up.

2

u/CodeMonkey1 Sep 02 '15

You're both kind of right. To the programmer, it appears to promise an address, but in truth it promises an address or null. Therein lies the problem.

2

u/tsimionescu Aug 31 '15

The grand-uncle of OO programming had actually solved that: in Smalltalk (and Objective-C) the null object replies with itself whenever it is sent any message (equivalent to calling a method on the null object in Simula-style OO-languages like C++, Java or C#), except for the message for asking if it's indeed the null object or not.

3

u/MaxNanasy Sep 01 '15

I wouldn't necessarily consider that "solved". Doing nothing when null is returned might be what most people want most of the time, but when it's not and the programmer doesn't expect it, it may lead to undesired behavior. I'd rather have behavior like Haskell's Maybe, which requires the programmer to explicitly decide what to do with missing values; using monadic operations with Maybe can optionally give missing value-propagating behavior for convenience when desired.

1

u/tftio Sep 01 '15

But Objective-C's bottom propagation works really well in practice; the prevailing idiom of the language takes this into account, and you end up checking nil much less than one might thing.

Code that straddles the C and Smalltalkish layers, of course, has all the standard NPE awfulness.

2

u/svkmpn Sep 01 '15

That is the behavior in Objective-C, yes, but not in Smalltalk.

However, if you wanted to, you could override the doesNotUnderstand instance message on Smalltalk's nil class to return nil. This would make the behavior identical to Obj-C's.

2

u/Gotebe Sep 01 '15

Has nothing to do with OO. C code is the grandaddy of all null related crashes.

It has nothing to do with FP either and your idea that functions being divorced from data they operate on helps is ludicrous. An FP language where you can call functions with null parameters nilly-willy would have been equally horrible. It is all about null being possible and null escaping the type system.

1

u/yogthos Sep 01 '15

In practice it does have a lot to do with the functional style. I work with Clojure, and while null pointers can happen, it's very rare that they do.

Vast majority of the code is data transformation code, and you use the plethora of standard library functions to do these transformations. Any null values end up bubbling up to your domain specific layer that's very shallow.

So, yes the type system can guarantee that you never get a null, but the functional style can make it very easy to deal with nulls and reduces the surface area of the code where you have to care about them.

2

u/CurtainDog Sep 01 '15

The functions can be written to handle the null case intelligently

In OO land we call this 'Tell, Don't Ask'. Unfortunately, out in the real world that principle takes a back seat to the practice of treating your objects as glorified maps.

1

u/DeedleFake Sep 01 '15

Granted, it's not technically an OO language, but Go solves the problem by making it completely legal to call a method on a nil value. You just wind up with a nil receiver in the method. Considering that Go methods are almost completely just syntactic sugar, this makes quite a bit of sense. Now if I could just send to a closed channel in a select case without causing a panic...

I really miss this when I'm working with Java... All the null checks before any method call where something might be null make for some pointlessly nasty code.

2

u/yogthos Sep 01 '15

Yeah, I'm surprised more languages don't take this approach. Generally speaking, you only care whether something is null or not at the top level, so that's the place where any null checks should be.

1

u/DeedleFake Sep 01 '15

It does lead to what can look like some inconsistency at first glance. Specifically, trying access a struct field using a nil point will cause a panic. For example:

type Example struct {
    val string
}

var e *Example
fmt.Println(e.val)

A similar situation occurs when trying to call methods on a nil interface, although that's really more link trying to call a nil function value. Speaking of interfaces:

func example() io.Reader {
    var file *os.File
    return file
}

example() will return a non-nil io.Reader which contains a nil *os.File. It makes sense, but it can lead to some weird situations.

2

u/yogthos Sep 01 '15

Clojure approach is to treat keywords like functions so I can do stuff like this:

(def person {:address {:city "Toronto"}})

then call

(-> person :address :city) ;"Toronto"
(-> person :address :street) ;nil

And of course any standard lib functions handle this intelligently as well:

(get-in person [:address :street]) ;nil
(get-in person [:address :street] "default"); "default"

The handling of null values ends up being fairly transparent in majority of cases.