r/csharp • • Jun 27 '16

Null reference exceptions hell

http://dobegin.com/npe-hell/
0 Upvotes

12 comments sorted by

View all comments

1

u/FizixMan Jun 27 '16

I really hope I'm misunderstanding this solution:

Ignoring null pointer exceptions

The programming language examples discussed above are on one side of extremity: calling a null pointer produces a runtime crash, and you have to opt-out case by case to recover. On the other side of extremity is an idea to ignore a null dereference and continue execution. This is an opt-in solution where you have to produce an exception yourself if you need to.

For a programmer who is used to deal with null pointer exceptions this might sound crazy, but this idea has proven itself to work well in production. Objective-C runtime and thus most Mac OS and iOS applications are running this way. Several things are great about this strategy:

  • A program can recover itself from an unexpected error if a higher level code works with the null condition. In other cases an operation finishes as if nothing has happened, because later calls to null are ignored too.
  • Disgusting null pointer exception errors are not thrown upon a user.
  • The app stays alive and might continue doing other useful stuff.
  • Null-checks can be avoided producing more compact and readable while still correct code.

Is this saying that code ignores null checking and exceptions are essentially ignored. The program continues executing producing null return values, doing nothing on method calls (on null instances), etc?

This seems absurdly dangerous to me. "The app stays alive and might continue doing other useful stuff" might as well read "The app stays alive and might continue doing other extremely dangerous/harmful/undefined stuff perhaps unnoticed by the user." And because the app isn't really "failing", the errors go unnoticed/unreported to the developer so you never really know how significant the problems are or when they occur and makes it even more difficult to debug when the user reports some weird undefined behaviour.

This is the behaviour in Flash ActionScript (at least ActionScript 2.0) and it was brutal to deal with.

All in all though, my first thought that if a developer is dealing with "Null reference exceptions Hell", then it suggests a serious deficiency in their application design, their code quality, and perhaps the usefulness of their testing. At least in the C#.NET world; could be a different story with other languages/runtimes/platforms. Of course, I'm just a big fan of fail fast in general and ignoring the null case altogether (because the code design usually doesn't use nulls as valid values). In general, if a developer has the discipline to stop using nulls in their code, null reference exceptions tend to also go away. Null reference exceptions rarely make it past initial development and testing stages.

1

u/battlmonstr Jun 28 '16

You understand it right. This indeed sounds crazy to many C# people.

"extremely dangerous/harmful/undefined stuff" This is only true if your app is potentially able to do that in a certain code module. If I was to write a disk formatting utility I would rather let it crash, but if I'm writing for example some UI code, I don't see a point of crashing.

Ignoring exceptions doesn't mean that you can't trap them and send a report message if you need that. Anyway, I wouldn't argue that it's unconvenient for a programmer, but users would often appreciate if you're able to recover.

Crashing the whole app is just too extreme in almost any scenario. In most cases you would be restarting and saying sorry, and retrying to continue your activity again.

It's great that you have this kind of programming discipline to avoid nulls alltogether, but my point is that sometimes people eventually screw this up and that compilers should be better at preventing this.