Live data from Hacker News

This should never happen

github.com

11–20 of 214 posts

Re: This should never happen

#11
post #10

In my experience, "this should never happen" cases often are a sign of very brittle design that branches into many separate but nearly-identical paths, and could be simplified to remove them. The other thing it points to is bad error handling paths (assuming that an error could "never happen".) Also funny to see Java being the most verbose as usual, with its ThisShouldNeverHappenException.java

Mostly there is no need for "ThisShouldNeverHappenException" since java has a IllegalStateException

But,but... ThisShouldNeverHappenException, NEVER ? What were they smoking.

Re: This should never happen

#12

https://github.com/search?utf8=%E2%9C%93&q=Why+does+this+hap...

^^ Above links to a "Why do this always happen" search on Github likely to express that if something that should never happens occurs, then you need to understand why it happened, which is often hard, since it never happens. Often solution is to be able to reproduce a bug by being able to playback what happened.

EDIT: Ha, turns out the link is just an attempt to prove that bugs that should never happen occur ten times more in C... Which is questionable.

Re: This should never happen

#14
GitHub's search is pretty interesting: every time I refresh the search page it shows a different number of results: 18,401,830; 17,751,631; 15,995,799.

Which is anyways quite a lot of results, but then this search finds ThisShouldNeverHappenException, the string "this should never happen" and stuff like

    // *This* gets run before every test.
    if (b > d) {
        fail("XX *should never happen*");   
    }
With quotations it's only about 500,000.

Re: This should never happen

#15
post #4

if happens: print "shit, that wasn't supposed to happen"

Never ever EVER put bad language in any unexpected error cases or logs - even as you're developing it... it WILL somehow magically make its way to production, and it WILL appear!

Re: This should never happen

#16

My favorite part is the Java project that has an exception class called ThisShouldNeverHappenException [1]. Only in Java would someone create an exception class for a condition that should never happen :) [1] https://github.com/TheProjecter/propidle/blob/f0d5320e2a3d46...

Not just Java. I've seen similar classes in C++ and C# to indicate things which should never occur/are clearly bad programmer mistakes/... Think InternalErrorException/DevFailedError etc. Sometimes it's just a sane thing to do, and using such names means you don't need to write the dreaded 'should never happen' comment manually anymore.

Re: This should never happen

#17

Apparently things-that-shouldn't-happen happen 10x more in C code than the next nearest language.

I didn't even notice that. Good observation. I wonder if that's simply because C is a lot older and may just have a lot more dead code or this type of code or something else entirely.

Maybe it's because C coders want to know why something happens more than coders in other popular languages; say popular since the frequency of an occurrence is proportional to the volume of source code in a given language.

Re: This should never happen

#18

Apparently things-that-shouldn't-happen happen 10x more in C code than the next nearest language.

I didn't even notice that. Good observation. I wonder if that's simply because C is a lot older and may just have a lot more dead code or this type of code or something else entirely.

I am a C lover.

It is because C has no exception handling, and C coders still want to be sure.

For example once I had a game where I ended putting a couple "should never happen" in my code related to some OS stuff, and... the "should never happen" happened once, after figuring how to reproduce it, it was a driver bug (or something like that, happened years ago, I don't remember the details anymore, only remember that it went away after I switched from Alsa to OSS4 on my Linux box).

EDIT: should never happen is good against compiler bugs too, I've seen my fair share of them.

Re: This should never happen

#19

Apparently things-that-shouldn't-happen happen 10x more in C code than the next nearest language.

Looking at the C results, it seems all of them are from the same file (socket.h) copied to different repositories.

I thought the same, then I searched for "This cygwin SIOCGIFCONF should never happen" and it finds only 319.

To me it seems like the search is grouping similar results in some way.

Re: This should never happen

#20
post #16

My favorite part is the Java project that has an exception class called ThisShouldNeverHappenException [1]. Only in Java would someone create an exception class for a condition that should never happen :) [1] https://github.com/TheProjecter/propidle/blob/f0d5320e2a3d46...

Not just Java. I've seen similar classes in C++ and C# to indicate things which should never occur/are clearly bad programmer mistakes/... Think InternalErrorException/DevFailedError etc. Sometimes it's just a sane thing to do, and using such names means you don't need to write the dreaded 'should never happen' comment manually anymore.

Isn't that what assertions are for?
Post reply on HN