7 ms·
You should always put a break statement at the end of a default branch. The default case is put at the end as a convention but no one (certainly not the C stand
by cliveowen 12y ago
You should always put a break statement at the end of a default branch. The default case is put at the end as a convention but no one (certainly not the C standard) prevents you from adding a new case after, which will be promptly executed even though probably it's what you want. I think this is mentioned a best practice in the K&R.
- cperciva 12y agoYou mean, after exit(0) returns?
- AnimalMuppet 12y agoNot after exit(0) returns, no. Instead, you're going to want it after some programmer removes exit(0) six months from now.
- loup-vaillant 12y agoI'm going to write a pre-processor which adds the appropriate `break` statements automatically. No more fall-through bug. (And if you really want to fall through, I could add a `fallthrough` keyword.)
- scintill76 12y agoYep, could even re-use the "continue" keyword.
- cperciva 12y agoFor what it's worth, it's a style bug in FreeBSD to have a fallthrough which does not have a /* FALLTHROUGH */ comment.
- hamburglar 12y agosmart-ass comment: you should put two breaks in for when some programmer removes one of them six months from now. ;) edit: i know it's not totally analogous since removing the exit and not noticing there's no break is a lot more likely than just randomly removing a break, but the "let's prevent someone clumsy from screwing this code up in the future" argument always makes me laugh a little.
- AnimalMuppet 12y agoOf course, you can always take it too far. Two breaks is too far, obviously (I'm pretty sure you agree with me on that). Is a break after an exit too far? It may well be, but it's less clearly too far than two breaks are.