←back to thread

327 points AareyBaba | 4 comments | | HN request time: 0s | source
Show context
don-code ◴[] No.46186343[source]
> All if, else if constructs will contain either a final else clause or a comment indicating why a final else clause is not necessary.

I actually do this as well, but in addition I log out a message like, "value was neither found nor not found. This should never happen."

This is incredibly useful for debugging. When code is running at scale, nonzero probability events happen all the time, and being able to immediately understand what happened - even if I don't understand why - has been very valuable to me.

replies(2): >>46187141 #>>46187743 #
YesBox ◴[] No.46187743[source]
Same. I go one step further and create a macro _STOP which is defined as w/e your language's DebugBreak() is. And if it's really important, _CRASH (this coerces me to fix the issue immediately)
replies(1): >>46187836 #
creato ◴[] No.46187836[source]
The "standard" (typically defined in projects I'm familiar with, and as of C23, an actual standard) is "unreachable": https://en.cppreference.com/w/c/program/unreachable.html
replies(2): >>46187855 #>>46188241 #
YesBox ◴[] No.46187855[source]
C++ keeps getting bigger and bigger :D

Thanks for sharing

replies(1): >>46188147 #
1. creato ◴[] No.46188147[source]
This is actually C and C++ has not done something similar AFAIK.
replies(3): >>46189837 #>>46190840 #>>46191037 #
2. hn_go_brrrrr ◴[] No.46189837[source]
Fortunately the major compiler vendors all have. Routing around the standards committee is getting more and more common.
3. Fulgen ◴[] No.46190840[source]
https://en.cppreference.com/w/cpp/utility/unreachable.html
4. TuxSH ◴[] No.46191037[source]
C++23 does have std::unreachable (as a function), and its counterpart [[assume(expr)]]