r/csharp • • Jun 27 '16

Null reference exceptions hell

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

12 comments sorted by

2

u/oftheterra Jun 27 '16

Ok, so rather than just snarkily commenting - "yup, null pointers exists..." which was my first thought, I'll instead recommend this:

  • Explain in your intro the purpose of the blog entry - at first glance it seems like you are just stating "null pointers exists in most modern languages" which pretty much any programmer already knows

  • Line up in the intro what a reader can expect to see - starting with examples/comparisons of how they look and function between languages, then ways to mitigate them, and perhaps a section on how languages which don't use them work

  • Expand a little bit more on the solutions section - maybe pointing towards things such as "Gaurd" libraries or Code Contracts

1

u/battlmonstr Jun 28 '16

I appreciate that. I should convey the point better in the beginning. My main point however was that our compilers should be better at fixing this problem instead of leaving it sensitive to human errors. There is though a chapter about mitigation solutions that I propose. Mentioning code contracts is a good idea.

0

u/[deleted] Jun 27 '16

[deleted]

2

u/oftheterra Jun 27 '16

I think you meant to reply to the author - unless you intended to ask me about all that.

1

u/FizixMan Jun 27 '16

Yes I did, sorry about that.

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.

•

u/FizixMan Jun 27 '16

Removed: Rule 3, Rule 5.

This post is fairly tangentially related to C# and .NET (you touch on it, but barely supply code examples, and don't go much into C#.NET solutions). There are so many languages in here and it's more about nulls in general, that it's probably more appropriate for /r/programming. Beyond that, you've spammed it to 9 different subreddits which I don't think should be encouraged.

1

u/battlmonstr Jun 28 '16

I completely understand why you removed it, although the article perfectly applies to NullReferenceException and has C# examples. NullPointerException is identical to NullReferenceException in practice, and I mention in the foreword that those are synonyms.

The whole idea of my blog is that different languages programmers communities should learn from each other by comparing themselves with the others and finding approaches to common problems. By restricting me to post to /r/programming only you severely limiting this. I know that many stubborn developers don't want to hear anything about the other languages, but I'm sure that they want to be compared and competitive with other technologies.

My point is to unite and discuss commonalities together. Your response to that is very offensive.

There's only a few people who would be interested in the other languages, because of such community "segregation", but I'd still like to find them, and yes, I have to go in all 9 different subreddits for that!

I'm trying to say that it's not spam, but actually an attempt to unite communities by learning from each other.

I got good feedback from the subreddits including /r/csharp that helps to improve the article, while /r/programming comments were mostly chuckles.

1

u/FizixMan Jun 28 '16

I appreciate your feedback and position on this.

Ultimately, Reddit is not a self-promotion/advertising platform, and while we are a bit more lenient about that on /r/csharp, we still have limits. If you posted material from other sources, commented in other posts/threads other than your own, and overall engaged in Reddit and /r/csharp beyond your own content, then we'd be much more lenient. But as it stands, given your current state of participation on Reddit and /r/csharp, any submissions by you (especially if the submissions is self-promotion) have a higher bar to pass for relevancy and worthwhileness for /r/csharp.

1

u/battlmonstr Jun 29 '16 edited Jun 29 '16

I understand this. I'm fairly new to reddit, and I will try to adjust.

How do you measure the reputation? Would more karma help or are there some requirements? If first, what karma level do I need to post here?

I see that "Rule 5: Do not frequently spam your own blogs or tools", but I just post like 1 page per month. Is it too frequent? Your rule text says once a week actually.

If I come across other related materials, would it help to post them as well?

Also I don't quite understand a point about "self-promotion". Do you want that I ask somebody to post here, or it's just that you don't want any kind of promotion? I felt like this "self" means something bad.

From your side author postings should be more valuable, because it's much less like spam and the author can engage in discussions first-hand much better. Also I don't get any money out of it. It's like volunteer activity to basically entertain your people and give some ideas.

1

u/FizixMan Jun 29 '16

(Reddit is giving me errors posting this, so I'll have to make multiple comments)

How do you measure the reputation? Would more karma help or are there some requirements? If first, what karma level do I need to post here?

Reputation/karma is irrelevant. Your comment/link karma does not restrict posting/commenting privileges on /r/csharp.

I see that "Rule 5: Do not frequently spam your own blogs or tools", but I just post like 1 page per month. Is it too frequent? Your rule text says once a week actually.

There is no hard and fast rule, the rules are flexible and left to the moderator's discretion to remove content for the betterment of the subreddit. In your case, Rule 5 was invoked due to widespread submission of your post to a lot of subreddits. While cross-posting is permitted, posting the same link to 9 (at the time, the tally is now up to 13) is way over the top.

Once a week is just a guideline. Typically if a poster is predominantly or only submitting and commenting on their own content, then the rules will be applied more harshly. If a user is submitting from a variety of sources (of which they are unaffiliated) and are engaging in conversation/comments in a variety of submissions, then the rules tend to be applied more leniently. Essentially, if you are a user of the subreddit first that engages with the membership with more-or-less altruistic intent, and thus essentially respect your fellow developer/subscriber here, you will be met with respect. If you are using Reddit primarily as a promotional medium to plug your own content/blog/product, you'll be viewed as an advertiser and not afforded much leeway.

1

u/FizixMan Jun 29 '16

If I come across other related materials, would it help to post them as well?

Yes, absolutely, post other related materials that are not your own. /r/csharp only exists because people post worthwhile/interesting/new content that other people want to read. Don't only post stuff because you want it to be read for your personal gain (e.g., page views on your blog), post stuff for the betterment of your fellow developer.

Also I don't quite understand a point about "self-promotion". Do you want that I ask somebody to post here, or it's just that you don't want any kind of promotion? I felt like this "self" means something bad.

From your side author postings should be more valuable, because it's much less like spam and the author can engage in discussions first-hand much better. Also I don't get any money out of it. It's like volunteer activity to basically entertain your people and give some ideas.

That's nice and all, but Reddit is not a platform for advertising. The moderator on /r/javascript gave you some advice (https://www.reddit.com/r/javascript/comments/4q5dyk/null_undefined_typeerror_hell/d4qveug). I recommend you read through (fully) both links they provided there.

And yes, we do treat original content made by authors here posted here as valuable. If we were to take Reddit's baseline spam rules (greater than 10% of your submissions are your own content, then you are likely a spammer) then a lot of good content being submitted here to /r/csharp would be blocked and many users banned. All we ask is that these authors try to not cross the threshold into spam territory, that they try to make submissions that are highly relevant to C#, and ideally try to engage with /r/csharp outside their own content.