16 ms·
This should never happen
- ZeroGravitas 11y agoApparently things-that-shouldn't-happen happen 10x more in C code than the next nearest language.
- BinaryIdiot 11y agoI 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.
- nxzero 11y agoMaybe 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.
- speeder 11y agoI 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.
- nothrabannosir 11y agoLooking at the C results, it seems all of them are from the same file (socket.h) copied to different repositories.
- odabaxok 11y agoI 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.
- groundCode 11y agoor C developers have considered things that should not happen more than the next nearest language.
- notacoward 11y agoTotally THIS. Many C coding standards require that every return value be checked, every "switch" statement have a default case, etc. Programmers comply, but sometimes the conditions they're checking truly are impossible, so you end up with messages like these. By contrast, coding standards for languages with exceptions typically do not require such constant checking in the code. An uncaught exception is the moral equivalent of "this should never happen" but doesn't show up in a search like this. Without even getting into the issue of whether C programmers are really more diligent than Java or Python programmers (for example) there's a measurement problem to contend with here.
- amelius 11y agoassert(false);
- Kristine1975 11y ago__builtin_unreachable();
- qb45 11y agoThat's a disaster waiting to happen. See docs.
- mdjt 11y agoif happens: print "shit, that wasn't supposed to happen"
- mrspeaker 11y agoNever 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!
- kagamine 11y agoI remind myself frequently not to swear in code as I am quite juvenile on a normal day. If I'm struggling I'll just make up a word, so that if it ever happens that someone else sees it I can say it is an acronym but that I have conveniently forgotten for what.
- bryanrasmussen 11y agoYou could always put a "this should never happen" right before the swear words so they know it wasn't your fault.
- kagamine 11y agoBut there's a risk I'll become stuck in an infinite loop of writing shit! this should never happen shit! this should never happen shit! ... and so on. Still it would be a change from writing bad code.
- bryanrasmussen 11y agoWow, deja-vu, this is just what happened to me one time when I was working on a new booking system for a hotel. I was working on site over the winter season when the hotel was closed down, a lovely old hotel high up in the Rockies. Every day I sat down and wanted to write some brilliant code but all I ever ended up writing was the same comment All objects and no functions makes Jack a dull boy. over and over again. Drove me quite mad actually. Sometimes I feel like I'm still there.
- BinaryIdiot 11y agoMy 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/f0d5320e2a3d46f9fe7ea3c36fc071256e023234/src/com/googlecode/propidle/util/ThisShouldNeverHappenException.java https://github.com/TheProjecter/propidle/blob/f0d5320e2a3d46...
- stinos 11y agoNot 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.
- Kristine1975 11y agoIsn't that what assertions are for?
- Karunamon 11y agoAssertions can be disabled at runtime. If your goal is to call out the fact that something has gone fundamentally wrong with your program's state, an exception is the way to go.
- amatix 11y agoin Python, running with `-O`, from .pyo files, or equivalent settings in your webserver will all disable assertions. surprise!
- maggit 11y agoTo elaborate a bit, in C++ the relevant exception class is called `std::logic_error` (contrast with `std::runtime_error`). I like it. It is a bit more descriptive of the actual situation than "this should never happen".
- 11y ago
- bryanrasmussen 11y agohttps://github.com/search?utf8=%E2%9C%93&q=Why+does+this+happen&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=Why+does+this+hap...
- nxzero 11y ago^^ 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.
- userbinator 11y agoIn 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.
- 11y ago
- INTPenis 11y agoI remember using the comment "Craw! Polly shouldn't be!" instead, as a homage to a specific Simpsons episode. :)
- wimagguc 11y agoGitHub'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.
- ddebernardy 11y agoUsually, you'd never actually _count_ the expected results for such stuff. Instead, you'd return the estimate number of records (think SQL EXPLAIN). I'd gather the number seems very dynamic because of the constant stream of commits.
- BenoitP 11y agoHyperLogLog[1] will do that. You can find it in Redis, Lucene; and I suspect it is also hidden behind BigQuery's count(distinct X)s. [1] https://en.wikipedia.org/wiki/HyperLogLog https://en.wikipedia.org/wiki/HyperLogLog
- sammoth 11y agoWhat part of the algorithm would cause the number to change significantly every time you run the query?
- BenoitP 11y agoAll shards compute their own, and send the result in a non deterministic order. My explanation is that the merge part of HLL depends on the order. Line 95 of this method [1] from this implementation [2] shows a max(mask(x), mask(y)), which is not associative but is a close proxy to max()(which is associative). [1] https://github.com/addthis/stream-lib/blob/af045cb4199959c07fb4422e605573e180491191/src/main/java/com/clearspring/analytics/stream/cardinality/RegisterSet.java#L87 https://github.com/addthis/stream-lib/blob/af045cb4199959c07... [2] https://github.com/addthis/stream-lib/blob/master/src/main/java/com/clearspring/analytics/stream/cardinality/HyperLogLog.java https://github.com/addthis/stream-lib/blob/master/src/main/j...
- dzdt 11y ago"This should never happen" is a design pattern of defensive programming. This is the same pattern for assert. The usual use is to catch errors caused by misuse of a method. There is some invariant that the method assumes but is not enforced by the type signature of the interface. So if something goes wrong in outside code, or someone tries to use the method incorrectly, the invariant is not satisfied. When you catch such a problem, the current code context is FUBAR. The question is how aggressively to bail out : spew errors to a log and proceed with some GIGO calculation? Throw an exception? Exit the program?
- zaphar 11y agoAnd this pattern is exactly why I prefer compile time type safety in my languages. This pattern is still sometimes necessary but there is a whole class of error this pattern gets used for that you can many times eliminate.
- gue5t 11y agoWhat's interesting is that (as described in the present top comment on this article, about "CALL BRIAN"), if the abstraction of "type safety" is leaky (as it is, e.g. in the presence of memory or hardware errors), this kind of paranoia can actually have real-world benefits even though you can prove the impossibility of the code running using static analysis. Sometimes the important artifact is the executable in the larger context of the deployed system, rather than the code you generate it from.
- nightski 11y agoThere is nothing preventing modeling the contextual environment within static analysis. Static analysis/type systems help the programmer draw the line between the known and the unknown. Some conditions are just not practical or efficient to check for. However for the context you use it within you can make certain guarantees about the code. This is still incredibly useful even though it doesn't guarantee an error can never take place. You say it "is" leaky in the presence of memory corruption. However that is not necessarily true. One could model software memory verification within a type system. Meaning, you could guarantee at compile time that each time a variable is read it is verified via checksum against its last written value. This would not be particularly efficient, but the point stands that type systems can be used (and should be used) to model hardware failures. This is no different than network link failures, etc...
- cbaleanu 11y agoThis too: https://github.com/search?utf8=%E2%9C%93&q=no+idea+why+this+works&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=no+idea+why+this+... :)
- smoyer 11y agoOne of the most common causes of failures are cases that the programmer never considered. Once of my favorite test coverage tools shows you missed branches and I find that invaluable. My initial reaction was "oh no" but as I thought about it, this explicitly indicates that the programmer actually thought about a case. And is it any different than what most of us do in our unit tests? If we're expecting an exception that isn't thrown or the wrong exception is thrown, we force a test failure. One of the goals our team is working towards is more robust and complete metric and log collection. We specifically want to capture exceptions that make it to the application server for analysis, but this assumes that the developer has a) considered all cases and b) caught intermediate exceptions and continued processing (or abort).
- paulkaplan 11y agoUsing 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-best-commit-messages-of-2015 http://www.slideshare.net/bsotomay/uchicago-cmsc-23300-the-b...
- bjacobel 11y agoThis is amazing.
- thinkMOAR 11y agoamazing waste of time that is, of everybody, the commit authors and the people spending time looking them up and making slides out of them. And lets not forget the waste of space on HN regarding this referral post.
- rco8786 11y agoI bet you're a blast at parties
- thinkMOAR 11y agoAnd youR* reply is about as useful as the whole thread, the reporting of other peoples laziness of committing with proper descriptions of their own code. What a surprise, people are lazy and it bites you in the ass in the long run wowiwow, what a surprise... Perhaps if this type of post didn't come by every X time, it wouldn't annoy me so much.
- mapleoin 11y agohttp://whatthecommit.com/ http://whatthecommit.com/
- Cyph0n 11y agoIt would have been funnier with context. Most of the references they're making are related to the assigned problems, I assume.
- paul_milovanov 11y agoSearch for "WTF", results by language C++ 2.4M C 400K Java 150K ... Sounds about right.
- misframer 11y agoAlso interesting considering there are over twice as many Java repositories (>200,000) as C++ (<90,000) [0]. [0] http://githut.info/ http://githut.info/
- atemerev 11y agoI usually annotate should-never-happen asserts by the most plausible explanation how it _could_ happen, for the sake of some poor guy unlucky enough for having to debug my code. E.g. "Should not happen — probably a bug in Apache Commons Math?" Or "Shouldn't really happen, barring compiler bugs or cosmic rays" As they told us, there is no such thing as probability of zero.
- lilbobbytables 11y agoaaand naturally javascript is right up there in the top languages where this is found.
- daniel_iversen 11y agoI think thats just good common practice - the amounts of times I've seen that message when obviously I never expected to :) This one is more worrying I suppose: https://github.com/search?utf8=&q=FIXME&type=Code&ref=searchresults https://github.com/search?utf8=&q=FIXME&type=Code&ref=search...
- lilbobbytables 11y agoaaand naturally javascript is right up there in the top languages where this is found.
- insulanian 11y agoC#: 58355, F#: 464, Haskel: 6
- pjmorris 11y agocall error("in omatch: can't happen"); - Line from omatch routine on pg. 146 [1]. Snips from discussion in [1]: "We can expect problems, therefore, and should prepare for them. ... Garbage is bad enough, but garbage which is expected to contain a count to tell you how long it is can be much worse. ... The first time we ran this code it said 'can't happen' We got that message perhaps a hundred times in the process of adding the rest of the code... This experience speaks for itself; if you're going to walk a high-wire, use a net." 'Can't happen' is as much a pattern as 'Hello, World'... and it has the same genesis. [1] Brian Kernighan, P.J. Plauger, 'Software Tools', Addison-Wesley 1976
- logicallee 11y agoMaybe even more interesting: https://github.com/search?utf8=%E2%9C%93&q=%22no+idea+why+this+works%22&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=%22no+idea+why+th...
- swehner 11y agoOr "How did this ever work?" https://github.com/search?utf8=%E2%9C%93&q=%22How+did+this+ever+work%22&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=%22How+did+this+e...
- deleted 11y ago[deleted]
- shade23 11y agoI find this more entertaining : https://github.com/torvalds/linux/search?utf8=%E2%9C%93&q=fuck https://github.com/torvalds/linux/search?utf8=%E2%9C%93&q=fu...
- Cyph0n 11y agoThat jab at Sun haha!
- pgtruesdell 11y agoAs usual, these submissions from Github searches like this are just sad.
- usea 11y agoSometimes you have to satisfy the compiler because it has less information about a situation than you do. Ideally this would be captured in the type system, but limitations (language, project, politics, time, etc) may prevent this. As another comment mentions, it's a kind of invariant check. For example, based on external information you might know[1] that a condition inside of a loop will always be hit exactly one time. Your compiler or tools might not be able to determine that same thing. It may try to force you to do something like assign a value or whatever you did in that condition, that it can't guarantee has happened. In such a scenario, it might[2] make sense to have something like "this should never happen" after the loop, with a brief comment explaining why you've done this. [1] I think this is the crux of the issue. We programmers often think we "know" something, but it might be an incorrect assumption. IMO part of being a good programmer is examining your assumptions at every step. The chasm is vast, between "the framework strongly guarantees X" and "the function that gets called before this one has done X already". The former is OK if you want to get work done, while the latter is much more brittle and possibly dangerous, depending on the level of coupling you're willing to accept. [2] Nine times out of ten, a reorganization of the logic makes more sense. However, I do think there are scenarios where this pattern is the best choice given the options.
- maxaf 11y agoThere are better (aka more creative) ways of inducing rage in fellow cow-orkers. https://twitter.com/noahlz/status/709961243277332482 https://twitter.com/noahlz/status/709961243277332482
- ninjakeyboard 11y agoIllegalStateException already exists. Why would people make their own?
- w8rbt 11y agovoid(int a, int b) { const int i = 0; int result = a + b; if (i > 0) { // This will never happen ;) result = result / 0; } }
- codeulike 11y agoBut what they mean is, 'This should never happen unless there's a serious problem upstream' and so these sorts of Asserts and Throws are actually very useful. Its all about 'Fail Fast'
- Twisell 11y agoBy far not as popular as foo https://github.com/search?utf8=%E2%9C%93&q=foo&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=foo&type=Code&ref...
- raldi 11y agoIf you work for a big company, search for this string in your bug-ticketing system to see example logs from all the times these lines are triggered.
- mictlann 11y ago"Testing this shit" -> We’ve found 129,140 code results https://github.com/search?utf8=%E2%9C%93&q=Testing+this+shit&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=Testing+this+shit...
- jballanc 11y agoReminds me of the "Six Stages of Debugging": http://plasmasturm.org/log/6debug/ http://plasmasturm.org/log/6debug/
- FiatLuxDave 11y agoMy favorite example of a "this should never happen" error was when I got a call from a customer, who started the conversation by asking, "Who is Brian?". I was caught a bit off guard, but I assumed the customer must know someone at the company, since Brian was the name of the previous electrical engineer/firmware programmer. So, I told them that Brian didn't work here any more, but was there anything that I could help them with? The customer said, "Well, the device says that I should call Brian". I was confused by this, and asked a lot of questions until I determined that the device was actually displaying "CALL BRIAN" on the LCD display. This was quite unusual, and at first I didn't believe the customer, until he sent a picture of the device showing the message. So, I dug into the code, and quickly found the "Call Brian" error condition. It was definitely one of those "this should never happen" cases. I presume that Brian had put that in during firmware development to catch an error case he was afraid might happen due to overwriting valid memory locations. I got the device back, and found out that the device had a processor problem (I don't remember exactly what) that would write corrupted data to memory. So, really, it should never happen. That particular device has now been in production for 10 years, and that is the only time that error has ever appeared.
- return0 11y agoDid you tell brian?
- FiatLuxDave 11y agoOf course I did. He didn't remember putting in that error condition, but he loved the story!
- viperscape 11y agoHe doesn't remember? That's scary hah. Seems like something worth remembering. Great story, made me laugh
- sliverstorm 11y agoDo you remember all the trap code you've ever written?
- jroseattle 11y agoThis is awesome. public class ThisShouldNeverHappenException extends RuntimeException { public ThisShouldNeverHappenException(Exception cause) { super(cause); } }
- deleted 11y ago[deleted]
- cptskippy 11y agoThis is what you throw when your padLeft webservice call fails.
- ben_pr 11y agoThe shocking part is the number of times: "This should never happen": 823,044 This should never happen: 16,946,357 vs. "This is screwed up": 59 This is screwed up: 876,393
- z3t4 11y agoAt least make the error say WHY it should never happen.
- js2 11y agoAnd the related Log.wtf() on Android: http://developer.android.com/reference/android/util/Log.html#wtf(java.lang.String,%20java.lang.Throwable) http://developer.android.com/reference/android/util/Log.html...
- justspamjustin 11y agotodo: stuff https://github.com/search?utf8=%E2%9C%93&q=todo+stuff&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=todo+stuff&type=C... todo: meh https://github.com/search?utf8=%E2%9C%93&q=todo+meh&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=todo+meh&type=Cod...
- saint-loup 11y agoPrevious noteworthy Github searches, found via hn.algolia.com : https://github.com/search?q=ugly+hack&ref=cmdform&type=Code https://github.com/search?q=ugly+hack&ref=cmdform&type=Code https://github.com/search?utf8=%E2%9C%93&q=filename%3Aid_rsa&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=filename%3Aid_rsa... https://github.com/search?p=3&q=extension%3Aphp+mysql_query+%24_GET&ref=searchresults&type=Code https://github.com/search?p=3&q=extension%3Aphp+mysql_query+...
- deleted 11y ago[deleted]
- davesque 11y agoNot trolling here. I'm curious why this is getting so much attention. Isn't saying "this should never happen" just making an assertion about the behavior of your code? As far as the apparent inconsistencies in the code seen in the results go, is it really fair to judge a random piece of code from a random person (and completely out of context)? Don't we all have scratch/experimental/incomplete code hosted on our github accounts?
- heyitskevin 11y agoI wish we could sort by most starred to see what popular repositories have code like this.
- protomyth 11y agoI remember one place I worked had an error thrown that showed as hex code "48 45 4C 4C 4E 4F". As far as I know it only occurred once in test when someone did something epically stupid as a patch to the code preceding it. We removed the patch and never saw the code again. Have no real clue who put it in there, as code control was a later addition.
- kbenson 11y agoThere's almost twice as many "should not get here" results[1], which I think would mostly be used in the same situation, in case someone is looking for more. 1: https://github.com/search?utf8=%E2%9C%93&q=should+not+get+here&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=should+not+get+he...
- shimon_e 11y agoC still in the lead for: why is this happening https://github.com/search?utf8=%E2%9C%93&q=why+is+this+happening&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=why+is+this+happe...
- nevir 11y agoThe thing is, we should be applauding people for many of these assertions. At least when they're throwing errors
- nkhumphreys 11y agoI think "should not get here" highlights this point better https://github.com/search?utf8=%E2%9C%93&q=should+not+get+here&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=should+not+get+he...
- dc2 11y agohttps://github.com/search?utf8=%E2%9C%93&q=%22this+is+so+broken%22&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=%22this+is+so+bro...
- cgtyoder 11y agoI prefer using CANTHAPPEN in the comment - easier to grep for.
- nly 11y agoI'm a big fan of 'DO NOT MERGE' in git logs
- danvideo 11y agomost of those probably tack on an "until..."
- danvideo 11y agoWhy no Ruby results?
- deleted 11y ago[deleted]
- Stratoscope 11y agoOne product I worked on a number of years ago had a CantHappen() function with a simple implementation. It displayed this message box: You are not here. Another message was in the Mac installer when there wasn't room to install: Your hard disk is too small. That's the complete text of both messages, and yes, it displayed them to the customer. <sigh> After seeing these, I started going through the code and found a bunch of other rude, confusing, or jargony messages. It actually turned into a fun little project cleaning these up!
- yeukhon 11y ago"wtf": https://github.com/search?utf8=%E2%9C%93&q=%22wtf%22&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=%22wtf%22&type=Co... Anyway, if you read commits in Linux you'd find a lot of fuck in there. Quite amusing. When I was an intern, I loved looking at MXR to find words like fuck: http://mxr.mozilla.org/mozilla-central/search?string=fuck http://mxr.mozilla.org/mozilla-central/search?string=fuck
- dumaspere 11y agoThis reminds me of the sage advice, "Never check for a condition that you don't know how to handle."
- pklausler 11y agoCray Research had a linker named SEGLDR that was written in Fortran, whose STOP statement allows an optional string message, and so it was used for run-time assertion checking like IF(.NOT.CHECK()) STOP 'xxx' Anyway, somebody (not me) got into trouble when Very Serious Customers were offended by seeing the occasional STOP DAMN message at link time.
- xydac 11y agoBest one is / * This should never happen exception. Use in situation that really shouldn't happen...NEVER * * */ public class NeverHappenException extends RuntimeException {
- vitaut 11y agoSince so many people obviously need this functionality, I've created a reusable Python version here: https://github.com/vitaut/neverhappen https://github.com/vitaut/neverhappen
- JohnTHaller 11y agoA related search I like is "why does this work": https://github.com/search?utf8=%E2%9C%93&q=%22why+does+this+work%22&type=Code&ref=searchresults https://github.com/search?utf8=%E2%9C%93&q=%22why+does+this+...
- onetimePete 11y agoCould be that some of the "This should never happen" can be deduced by optimizing compilers and never exist in assembly? Not talking about the interpreted stuff.
- rubinho_ 11y agoThe best thing is that someone made a 'ThisShouldNeverHappenException'. +1