←back to thread

327 points AareyBaba | 1 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 #
kace91 ◴[] No.46187141[source]
I like rust matching for this reason: You need to cover all branches.

In fact, not using a default (the else clause equivalent) is ideal if you can explicitly cover all cases, because then if the possibilities expand (say a new value in an enum) you’ll be annoyed by the compiler to cover the new case, which might otherwise slip by.

replies(1): >>46190383 #
uecker ◴[] No.46190383[source]
And I like using enums in C ;-) The compiler tells you to cover all branches.

https://godbolt.org/z/bY1P9Kx7n

replies(2): >>46191651 #>>46192254 #
1. rundev ◴[] No.46191651[source]
The compiler also tells you that even if you cover all enum members, you still need a `default` to cover everything, because C enums allow non-member values.