Published by Fabian on 15 Apr 2008
switch() – the better if() ?
today I found another piece of code, that does get extra points for style, but perhaps some deducted for readability:
switch (a_boolean) { case true: // do something break; case false: // do something else break; default: break; }
this is a nice, and so far for me unknown way of describing an if. Is this better for some reason? The only advantage I see is that it is very verbose, saying false and true. However I am unsure which compilers in which language would optimize this differently than an regular if.
What I especially like is the failsafe default statement
You never know