TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

This should never happen

626 点作者 qprofyeh大约 9 年前

58 条评论

FiatLuxDave大约 9 年前
My favorite example of a &quot;this should never happen&quot; error was when I got a call from a customer, who started the conversation by asking, &quot;Who is Brian?&quot;.<p>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&#x2F;firmware programmer. So, I told them that Brian didn&#x27;t work here any more, but was there anything that I could help them with? The customer said, &quot;Well, the device says that I should call Brian&quot;. I was confused by this, and asked a lot of questions until I determined that the device was actually displaying &quot;CALL BRIAN&quot; on the LCD display.<p>This was quite unusual, and at first I didn&#x27;t believe the customer, until he sent a picture of the device showing the message.<p>So, I dug into the code, and quickly found the &quot;Call Brian&quot; error condition. It was definitely one of those &quot;this should never happen&quot; 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.<p>I got the device back, and found out that the device had a processor problem (I don&#x27;t remember exactly what) that would write corrupted data to memory. So, really, it should never happen.<p>That particular device has now been in production for 10 years, and that is the only time that error has ever appeared.
评论 #11397370 未加载
评论 #11398924 未加载
评论 #11397440 未加载
评论 #11397479 未加载
评论 #11404264 未加载
评论 #11399984 未加载
评论 #11398542 未加载
dzdt大约 9 年前
&quot;This should never happen&quot; is a design pattern of defensive programming. This is the same pattern for assert.<p>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?
评论 #11396934 未加载
评论 #11396912 未加载
评论 #11397425 未加载
评论 #11397756 未加载
paulkaplan大约 9 年前
Using github to search like this reminds me of how a CS professor of mine would show the &quot;best commit messages of the year&quot; (homework was submitted via git) by looking for various patterns like all caps, all symbols, etc.<p><a href="http:&#x2F;&#x2F;www.slideshare.net&#x2F;bsotomay&#x2F;uchicago-cmsc-23300-the-best-commit-messages-of-2015" rel="nofollow">http:&#x2F;&#x2F;www.slideshare.net&#x2F;bsotomay&#x2F;uchicago-cmsc-23300-the-b...</a>
评论 #11398253 未加载
评论 #11397359 未加载
评论 #11397122 未加载
评论 #11396804 未加载
评论 #11396785 未加载
评论 #11396802 未加载
评论 #11396636 未加载
评论 #11399227 未加载
评论 #11396792 未加载
评论 #11396915 未加载
BinaryIdiot大约 9 年前
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 :)<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;TheProjecter&#x2F;propidle&#x2F;blob&#x2F;f0d5320e2a3d46f9fe7ea3c36fc071256e023234&#x2F;src&#x2F;com&#x2F;googlecode&#x2F;propidle&#x2F;util&#x2F;ThisShouldNeverHappenException.java" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;TheProjecter&#x2F;propidle&#x2F;blob&#x2F;f0d5320e2a3d46...</a>
评论 #11396359 未加载
评论 #11396987 未加载
评论 #11396691 未加载
评论 #11396444 未加载
评论 #11397147 未加载
评论 #11396964 未加载
wimagguc大约 9 年前
GitHub&#x27;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.<p>Which is anyways quite a lot of results, but then this search finds <i>ThisShouldNeverHappenException</i>, the string <i>&quot;this should never happen&quot;</i> and stuff like<p><pre><code> &#x2F;&#x2F; *This* gets run before every test. if (b &gt; d) { fail(&quot;XX *should never happen*&quot;); } </code></pre> With quotations it&#x27;s <i>only</i> about 500,000.
评论 #11396419 未加载
评论 #11396480 未加载
评论 #11396489 未加载
usea大约 9 年前
Sometimes 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&#x27;s a kind of invariant check.<p>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&#x27;t guarantee has happened. In such a scenario, it might[2] make sense to have something like &quot;this should never happen&quot; after the loop, with a brief comment explaining why you&#x27;ve done this.<p>[1] I think this is the crux of the issue. We programmers often think we &quot;know&quot; 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 &quot;the framework strongly guarantees X&quot; and &quot;the function that gets called before this one has done X already&quot;. 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&#x27;re willing to accept.<p>[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.
userbinator大约 9 年前
In my experience, &quot;this should never happen&quot; 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 &quot;never happen&quot;.)<p>Also funny to see Java being the most verbose as usual, with its ThisShouldNeverHappenException.java
评论 #11396324 未加载
评论 #11396424 未加载
评论 #11396833 未加载
ZeroGravitas大约 9 年前
Apparently things-that-shouldn&#x27;t-happen happen 10x more in C code than the next nearest language.
评论 #11396344 未加载
评论 #11396593 未加载
评论 #11396309 未加载
cbaleanu大约 9 年前
This too: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=no+idea+why+this+works&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=no+idea+why+this+...</a><p>:)
davesque大约 9 年前
Not trolling here. I&#x27;m curious why this is getting so much attention. Isn&#x27;t saying &quot;this should never happen&quot; 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&#x27;t we all have scratch&#x2F;experimental&#x2F;incomplete code hosted on our github accounts?
codeulike大约 9 年前
But what they mean is, &#x27;This should never happen unless there&#x27;s a serious problem upstream&#x27; and so these sorts of Asserts and Throws are actually very useful. Its all about &#x27;Fail Fast&#x27;
daniel_iversen大约 9 年前
I think thats just good common practice - the amounts of times I&#x27;ve seen that message when obviously I never expected to :)<p>This one is more worrying I suppose: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=&amp;q=FIXME&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=&amp;q=FIXME&amp;type=Code&amp;ref=search...</a>
atemerev大约 9 年前
I 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.<p>E.g. &quot;Should not happen — probably a bug in Apache Commons Math?&quot;<p>Or &quot;Shouldn&#x27;t really happen, barring compiler bugs or cosmic rays&quot;<p>As they told us, there is no such thing as probability of zero.
pjmorris大约 9 年前
call error(&quot;in omatch: can&#x27;t happen&quot;); - Line from omatch routine on pg. 146 [1].<p>Snips from discussion in [1]: &quot;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 &#x27;can&#x27;t happen&#x27; 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&#x27;re going to walk a high-wire, use a net.&quot;<p>&#x27;Can&#x27;t happen&#x27; is as much a pattern as &#x27;Hello, World&#x27;... and it has the same genesis.<p>[1] Brian Kernighan, P.J. Plauger, &#x27;Software Tools&#x27;, Addison-Wesley 1976
saint-loup大约 9 年前
Previous noteworthy Github searches, found via hn.algolia.com :<p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?q=ugly+hack&amp;ref=cmdform&amp;type=Code" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?q=ugly+hack&amp;ref=cmdform&amp;type=Code</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=filename%3Aid_rsa&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=filename%3Aid_rsa...</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?p=3&amp;q=extension%3Aphp+mysql_query+%24_GET&amp;ref=searchresults&amp;type=Code" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?p=3&amp;q=extension%3Aphp+mysql_query+...</a>
paul_milovanov大约 9 年前
Search for &quot;WTF&quot;, results by language<p>C++ 2.4M C 400K Java 150K ...<p>Sounds about right.
评论 #11396810 未加载
cptskippy大约 9 年前
This is what you throw when your padLeft webservice call fails.
smoyer大约 9 年前
One 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.<p>My initial reaction was &quot;oh no&quot; but as I thought about it, this explicitly indicates that the programmer actually thought about a case.<p>And is it any different than what most of us do in our unit tests? If we&#x27;re expecting an exception that isn&#x27;t thrown or the wrong exception is thrown, we force a test failure.<p>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).
maxaf大约 9 年前
There are better (aka more creative) ways of inducing rage in fellow cow-orkers. <a href="https:&#x2F;&#x2F;twitter.com&#x2F;noahlz&#x2F;status&#x2F;709961243277332482" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;noahlz&#x2F;status&#x2F;709961243277332482</a>
bryanrasmussen大约 9 年前
<a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Why+does+this+happen&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Why+does+this+hap...</a>
评论 #11396343 未加载
w8rbt大约 9 年前
<p><pre><code> void(int a, int b) { const int i = 0; int result = a + b; if (i &gt; 0) { &#x2F;&#x2F; This will never happen ;) result = result &#x2F; 0; } }</code></pre>
Stratoscope大约 9 年前
One product I worked on a number of years ago had a CantHappen() function with a simple implementation. It displayed this message box:<p><pre><code> You are not here. </code></pre> Another message was in the Mac installer when there wasn&#x27;t room to install:<p><pre><code> Your hard disk is too small. </code></pre> That&#x27;s the complete text of both messages, and yes, it displayed them <i>to the customer</i>. &lt;sigh&gt;<p>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大约 9 年前
&quot;wtf&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22wtf%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22wtf%22&amp;type=Co...</a><p>Anyway, if you read commits in Linux you&#x27;d find a lot of fuck in there. Quite amusing.<p>When I was an intern, I loved looking at MXR to find words like fuck: <a href="http:&#x2F;&#x2F;mxr.mozilla.org&#x2F;mozilla-central&#x2F;search?string=fuck" rel="nofollow">http:&#x2F;&#x2F;mxr.mozilla.org&#x2F;mozilla-central&#x2F;search?string=fuck</a>
kbenson大约 9 年前
There&#x27;s almost twice as many &quot;should not get here&quot; results[1], which I think would mostly be used in the same situation, in case someone is looking for more.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+here&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+he...</a>
insulanian大约 9 年前
C#: 58355, F#: 464, Haskel: 6
评论 #11396988 未加载
protomyth大约 9 年前
I remember one place I worked had an error thrown that showed as hex code &quot;48 45 4C 4C 4E 4F&quot;. 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.
pklausler大约 9 年前
Cray 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<p>IF(.NOT.CHECK()) STOP &#x27;xxx&#x27;<p>Anyway, somebody (not me) got into trouble when Very Serious Customers were offended by seeing the occasional STOP DAMN message at link time.
mictlann大约 9 年前
&quot;Testing this shit&quot; -&gt; We’ve found 129,140 code results <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Testing+this+shit&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Testing+this+shit...</a>
JohnTHaller大约 9 年前
A related search I like is &quot;why does this work&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22why+does+this+work%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22why+does+this+...</a>
nkhumphreys大约 9 年前
I think &quot;should not get here&quot; highlights this point better<p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+here&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+he...</a>
shimon_e大约 9 年前
C still in the lead for: why is this happening <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=why+is+this+happening&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=why+is+this+happe...</a>
js2大约 9 年前
And the related Log.wtf() on Android:<p><a href="http:&#x2F;&#x2F;developer.android.com&#x2F;reference&#x2F;android&#x2F;util&#x2F;Log.html#wtf(java.lang.String,%20java.lang.Throwable)" rel="nofollow">http:&#x2F;&#x2F;developer.android.com&#x2F;reference&#x2F;android&#x2F;util&#x2F;Log.html...</a>
vitaut大约 9 年前
Since so many people obviously need this functionality, I&#x27;ve created a reusable Python version here: <a href="https:&#x2F;&#x2F;github.com&#x2F;vitaut&#x2F;neverhappen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vitaut&#x2F;neverhappen</a>
Twisell大约 9 年前
By far not as popular as foo <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=foo&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=foo&amp;type=Code&amp;ref...</a>
logicallee大约 9 年前
Maybe even more interesting:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22no+idea+why+this+works%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22no+idea+why+th...</a>
评论 #11397076 未加载
shade23大约 9 年前
I find this more entertaining : <a href="https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;search?utf8=%E2%9C%93&amp;q=fuck" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;search?utf8=%E2%9C%93&amp;q=fu...</a>
评论 #11396799 未加载
heyitskevin大约 9 年前
I wish we could sort by most starred to see what popular repositories have code like this.
dc2大约 9 年前
<a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22this+is+so+broken%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22this+is+so+bro...</a>
jballanc大约 9 年前
Reminds me of the &quot;Six Stages of Debugging&quot;: <a href="http:&#x2F;&#x2F;plasmasturm.org&#x2F;log&#x2F;6debug&#x2F;" rel="nofollow">http:&#x2F;&#x2F;plasmasturm.org&#x2F;log&#x2F;6debug&#x2F;</a>
mdjt大约 9 年前
if happens: print &quot;shit, that wasn&#x27;t supposed to happen&quot;
评论 #11396358 未加载
评论 #11399487 未加载
评论 #11396486 未加载
评论 #11396465 未加载
jroseattle大约 9 年前
This is awesome.<p><pre><code> public class ThisShouldNeverHappenException extends RuntimeException { public ThisShouldNeverHappenException(Exception cause) { super(cause); } }</code></pre>
ninjakeyboard大约 9 年前
IllegalStateException already exists. Why would people make their own?
xydac大约 9 年前
Best one is<p>&#x2F;<i></i> * This should never happen exception. Use in situation that really shouldn&#x27;t happen...NEVER * * *&#x2F; public class NeverHappenException extends RuntimeException {
justspamjustin大约 9 年前
todo: stuff <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+stuff&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+stuff&amp;type=C...</a> todo: meh <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+meh&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+meh&amp;type=Cod...</a>
ben_pr大约 9 年前
The shocking part is the number of times:<p>&quot;This should never happen&quot;: 823,044 This should never happen: 16,946,357 vs. &quot;This is screwed up&quot;: 59 This is screwed up: 876,393
onetimePete大约 9 年前
Could be that some of the &quot;This should never happen&quot; can be deduced by optimizing compilers and never exist in assembly? Not talking about the interpreted stuff.
z3t4大约 9 年前
At least make the error say WHY it should never happen.
raldi大约 9 年前
If 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.
INTPenis大约 9 年前
I remember using the comment &quot;Craw! Polly shouldn&#x27;t be!&quot; instead, as a homage to a specific Simpsons episode. :)
nevir大约 9 年前
The thing is, we should be applauding people for many of these assertions. At least when they&#x27;re throwing errors
dumaspere大约 9 年前
This reminds me of the sage advice, &quot;Never check for a condition that you don&#x27;t know how to handle.&quot;
rubinho_大约 9 年前
The best thing is that someone made a &#x27;ThisShouldNeverHappenException&#x27;.<p>+1
cgtyoder大约 9 年前
I prefer using CANTHAPPEN in the comment - easier to grep for.
nly大约 9 年前
I&#x27;m a big fan of &#x27;DO NOT MERGE&#x27; in git logs
评论 #11398485 未加载
danvideo大约 9 年前
Why no Ruby results?
pgtruesdell大约 9 年前
As usual, these submissions from Github searches like this are just sad.
amelius大约 9 年前
assert(false);
评论 #11396712 未加载
lilbobbytables大约 9 年前
aaand naturally javascript is right up there in the top languages where this is found.
评论 #11396749 未加载
评论 #11396930 未加载