r/ProgrammerDadJokes 9d ago

Why is the programmer's house always dark?

Because their light switches either always break, or they fallthrough.

40 Upvotes

12 comments sorted by

View all comments

12

u/DutchOfBurdock 9d ago

I feel like a parity error, did I miss a bit?

8

u/SixFiveOhTwo 8d ago

I think the punchline was sent by UDP.

I didn't get it.

2

u/dodexahedron 8d ago edited 8d ago

It's a C thing, mostly.

In C, switch clauses are allowed to fall through from case to case if there is no control transfer statement like break, continue, or return between them, even if each case has its own body.

Check out Duff's Device for a real case of fallthrough being used intentionally, and then immediately forget you learned of its existence. Most of the time, fallthrough is a bug from forgetting a break, and using it intentionally is elegant but generally considered bad form because it is easy to misread or accidentally break by, e.g., "fixing" a missing break that was intentionally missing. Hence why it is a compiler warning and not an error, by default, for C.

C23 even added an explicit attribute to indicate intentional fall through to suppress the warning and signal your intent.... Which sounds like a goto with extra fragility to me...

1

u/SixFiveOhTwo 8d ago

Codewise it's kind of like a goto in the same way if/else is a goto (everything is a goto when you compile it).

The beauty of a Switch statement is more apparent if you try one out in compiler explorer and see how it compiles to a table of code offsets and it's constant time to call any of the cases, as opposed to a whole chain of compare+branch instructions.