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
This should never happen
11–20 of 214 posts
Re: This should never happen
#12https://github.com/search?utf8=%E2%9C%93&q=Why+does+this+hap...
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
#13Apparently things-that-shouldn't-happen happen 10x more in C code than the next nearest language.
Re: This should never happen
#14Which 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
#15if happens: print "shit, that wasn't supposed to happen"
Re: This should never happen
#16My 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...
Re: This should never happen
#17Apparently 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.
Re: This should never happen
#18Apparently 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.
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
#19Apparently 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.
To me it seems like the search is grouping similar results in some way.
Re: This should never happen
#20My 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.