3 ms·
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 coul
by userbinator 11y ago
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
- merb 11y agoMostly there is no need for "ThisShouldNeverHappenException" since java has a IllegalStateException
- stillworks 11y agoBut,but... ThisShouldNeverHappenException, NEVER ? What were they smoking.
- manyxcxi 11y agoTo be fair, someone writing a ThisShouldNeverHappenException may not be the kind of developer that knows about an IllegalStateException.
- merb 11y agoActually they at least need to know about a RuntimeException then. 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.
- aerique 11y agoI use "this should never happen" branches when creating new code fully expecting those branches to be hit so I can debug the case. Those branches ought to disappear once the code and tests improve but we all know how that goes :-)
- mbell 11y agoI found myself doing something similar to this in Java more than any other language. 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.