ProgrammerHumor

youWish

youWish
https://i.redd.it/fsz3uaajjodf1.png
Reddit

Discussion

YoukanDewitt
:js:

If you haven't written this exact comment while implementing error handling, you are not a real programmer.

4 hours ago
legendLC

// TODO: handle this properly

3 hours ago
Natural_Builder_3170
:cp:

default: break; // this is never rescheduled

3 hours ago
BeefHazard
:ts::py::j::rust:

In JS? Yes. In Rust? Fuck no.

1 hour ago
Cat7o0

suree

28 minutes ago
ziptofaf

Honestly... indeed, it should be a no for Rust. It kinda maybe perhaps doesn't really let you have null values.

You can however type .unwrap and then if you DO get a wrong result, something null-like then it will instantly panic. And often you are tempted to as you assume a given line of code can't fail.

10 minutes ago
LitrlyNoOne

I write it daily, and I distinctly notice my peers never writing it when I do code reviews. 😡

3 hours ago
Kaenguruu-Dev
:cs::gd::py:

Similarly, things like:

// NOTE: KEEP THIS STATEMENT OUTSIDE THE ASYNC CALL OR EVERY TEST WILL BREAK

4 hours ago
Fohqul

Why not throw an error? If it should never happen then you need to know if and when it does

3 hours ago
Skyswimsky

As a fan of offensive programming, that's what I like to do for things like those.

3 hours ago
Svelva

Trying to keep the PR at 4 files changed instead of 96

1 hour ago
Wertbon1789

Tbh, that would be a great place for an assert, depending if it's an actually relevant part of code.

4 hours ago
mattpark-ml

// TODO quick hack need to fix DON'T RUN IN PROD

commit date: 2007

2 hours ago
SatanicTriangle
:cp:

And you laughed when C++ added std::unreachable

4 hours ago
Mercerenies

The problem with std::unreachable is that it's Rust's unreachable_unchecked!. That is, it's not an assertion; it's UB if you're wrong. The most common thing you want is an assertion. "I, the programmer, don't think this will happen. If it does, please fail quickly". In Python, that's assert. In Rust, that's assert! (or, more directly for this use case, unreachable!).

C++'s std::unreachable and Rust's unreachable_unchecked! are performance micro-optimizations. "I, the programmer, am writing super-low-level code. I have statically verified (through other means) that this branch is mathematically unreachable. Optimize around this fact. If I'm wrong, may the god of undefined behavior strike me down"

The former is common and ordinary. It acknowledges the possibility of wrong-ness and forces the program to fail-fast (with a good error message) if it happens. The latter is an absolute assertion of unchecked correctness. The latter is, 99% of the time, arrogant and overconfident.

3 hours ago
Maleficent_Memory831

I want these sorts of things to just shut up the stupid static analysis tool. There are #ifdefs that the tool ignores, so it's unreachable in some builds but not all builds, and I dont want to make the tool artificially more unreadable just to satisfy a tool.

(yes, yes, I could configure the tool to not be so pendantic but it's controlled by another group that turns the dial to 11 to ensure that all projects are late)

1 hour ago
EtherealPheonix
:cp::cs:

What is the use case for (actually) unreachable code?

41 minutes ago
Mercerenies

It comes up a lot when you're reasoning about several cases (such as in pattern matching). As a recent example in one of my own projects, I'm parsing a data file. The file starts with a few "header" lines. The header lines each begin with name_statement or import_statement. When I encounter something that isn't a header line, I break the loop. So I have something like (obviously simplified code)

loop { let Some(next_node) = nodes.peek() else { break; }; if next_node.kind() != "name_statement" && next_node.kind() != "import_statement" { break; // Done with header } let next = nodes.next().unwrap(); let next_stmt = match next_node.kind() { "name_statement" => { // Parse name } "import_statement" => { // Parse import } _ => unreachable!(), }; decls.push(next_stmt); }

By the time I get to the match statement, you and I both know that next_node.kind() is either a name or an import statement. But the compiler can't reason about that. So I put an unreachable! in that third case. It should never happen, and on the offchance that I'm somehow wrong, I get a panic rather than unexpected behavior.

33 minutes ago
PM_BITCOIN_AND_BOOBS
:rust:

"This should never happen" belongs in the error message that the user sees, and should include the developer's name phone number.

2 hours ago
Mara_li
:py::js::ts:

I do it when on some try/catch! Not with my phone number ; but with a link to my discord server. Never happens, but at last they can call me

2 hours ago
Codexismus

There are some languages (I think mainly scripts) that there is no exception handling, you will see a lot of this

2 hours ago
Maleficent_Memory831

I hate when someone asserts on something like this, but the calling function is prepared and ready to handle the "should not happen" case and recover from it properly. As in the caller knows this case can happen. The result is that we should be able to handle the error, but the code crashes for thousands of customers because the developer thought that assert() was a good way to handle errors. Mostly, lots of people learning to program on the job all copying styles from each other and that code is in production for over a decade before suddenly it crashes everywhere all at once.

(on the other hand, an assert() in C code at the customer is slightly less annoying than a full blown Java exception backtrace because it catches all errors at main(). Have to tell people to stop printing out the backtraces because we're running out of printer paper...)

1 hour ago
Maleficent_Memory831

Wait, isn't !data also NULL, so just return data...

1 hour ago
evanldixon
:cs:

// we shouldn't get here

// shoutout to you in the future who got here anyway

1 hour ago
SegmentationFault63

Back in the early 90s, a colleague had a "this should never happen" error trap that printed "Oh no Mr. Bill!!!" to the console (trust me, back in the 90s that was a hilarious catch phrase. Ask your grandpa about it.)

Of course it happened, and our biggest client at the time called with... questions.

1 hour ago
Public_Confidence69

My framework is full of universal warnings that "should never occur"... until they do.

1 minute ago
No-Reflection-869

If it shouldn't happen you shouldn't need the if statement or yet better use non nullable variables if your language supports it.

2 hours ago
Nightmoon26

A fair number of static analyzers will throw warnings if there is any way that the enclosing function could be called in a way that would result in dereferencing a null pointer, whether or not it ever is. Unless you like squiggly lines in the editor, you learn to put those checks everywhere (a lot of languages don't have non-nullable variables or argument declarations).

I would usually throw an appropriate exception (potentially an unchecked one) with a comment explaining that "if this ever happens, something has gone horribly wrong".

Of course, it's entirely possible that, in this case, returning null would be the correct and expected thing to do, and the developer is just reminding themselves that it shouldn't actually happen in practice

29 minutes ago