Using github to search like this reminds me of how a CS professor of mine would show the "best commit messages of the year" (homework was submitted via git) by looking for various patterns like all caps, all symbols, etc. http://www.slideshare.net/bsotomay/uchicago-cmsc-23300-the-b...
This should never happen
61–70 of 214 posts
Re: This should never happen
#62In 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
Re: This should never happen
#63I find this more entertaining : https://github.com/torvalds/linux/search?utf8=%E2%9C%93&q=fu...
Re: This should never happen
#64 void(int a, int b)
{
const int i = 0;
int result = a + b;
if (i > 0)
{
// This will never happen ;)
result = result / 0;
}
}Re: This should never happen
#65Using github to search like this reminds me of how a CS professor of mine would show the "best commit messages of the year" (homework was submitted via git) by looking for various patterns like all caps, all symbols, etc. http://www.slideshare.net/bsotomay/uchicago-cmsc-23300-the-b...
Re: This should never happen
#66Using github to search like this reminds me of how a CS professor of mine would show the "best commit messages of the year" (homework was submitted via git) by looking for various patterns like all caps, all symbols, etc. http://www.slideshare.net/bsotomay/uchicago-cmsc-23300-the-b...
Re: This should never happen
#67Search for "WTF", results by language C++ 2.4M C 400K Java 150K ... Sounds about right.
Re: This should never happen
#68In 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
Usually because some insane abstraction was built over a concept and some of the implementations could throw something like IOException and some could not but of course the API throws IOException as a checked exception.
Re: This should never happen
#69Earlier quoted context omitted.
Mostly there is no need for "ThisShouldNeverHappenException" since java has a IllegalStateException
To be fair, someone writing a ThisShouldNeverHappenException may not be the kind of developer that knows about an IllegalStateException.
So that would've worked, too:
throw new RuntimeException("This should never happen!");
There would've been a difference between a Exception and a RuntimeException.Also sometimes this kind of RuntimeException happens when you convert one type into another and want to explictly call all cases something like that:
interface A
class AA implements A
class AB implements A
if (x instanceof AA) {}
else if (x instanceof AB) {}
else { throw new IllegalStateException; }
It's sometimes better to explicitly call all states instead of using the last else for the AB branch, since sometimes this will be extended later or the compiler would've throw an error since you have a return inside the if or else if.
Btw. Kotlin and Scala won't have this problem due to pattern matching.Re: This should never happen
#70Earlier quoted context omitted.
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?