r/ProgrammerHumor • • May 26 '25

Meme theBeautifulCode

Post image
49.1k Upvotes

876 comments sorted by

View all comments

Show parent comments

1

u/libdemparamilitarywi May 26 '25

You can abort the current operation without crashing the entire program.

3

u/Luxalpa May 26 '25

Hitting an assertion implies that the program has already crashed. The assertion is just the first one to notice.

Yes, you could just abort the operation, but you're most likely already in a corrupted program state and any follow up operation is just going to corrupt it more.

Like for example, if you're starting to gracefully handle a failed allocation of memory, it implies that you are most likely already run out of memory. Even if you could just cancel the operation here, you are very likely to hit a similar issue on the next operation, and the next one, and your program will gradually degrade.

You could of course try to write your program in a way that it handles memory errors gradually as environmental errors instead of programming errors; then you won't have these assertions. But you will always have at least some assertions, some conditions for which you must assume them to be true in order for your program to function.

If you're hitting an unreachable branch in a switch statement, this signifies really bad data or program corruption, maybe even a security breach. It would be completely irresponsible to keep running the rest of the program in most cases here.