Style guide: Add section about switch
Closed this issue · 4 comments
Namely:
Style and assert/error on default fall-through.
switch (req.query.action) {
case 'subscribe': {
return onSubscribe(req, res, next);
} break;
case 'unsubscribe': {
return onUnsubscribe(req, res, next);
} break;
default: {
assert(false, 'some descriptive message here');
} break;
}
Is the 'break' statement on the same line as the closing curly brace a common pattern? It seems odd to me. It is also deadcode after a return statement, that doesn't bother me as a reader of the code but it may be reported by static analysis tools.
For background/motivation, the convention ensures that the scope block encompass all statements related to the case. It is arbitrary for the break;
to be on the same line as the closing }
, but it makes some sense because you really want to end the case
for that scope block.
Since break;
would be a no-op after a return
, should static code analysis tools ignore that?
I think Thierry's point is precisely that the break
is unreachable because of the return, hence it'd be reported as dead code by a naive checker. :)