Published by Fabian on 15 Apr 2008 at 06:17 pm
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
Guildenstern on 15 Apr 2008 at 11:31 pm #
I have seen this in C# code…. however the default statement was commented out BUT that great developer added an additional comment. The whole thing looked like this:
That developer doesn’t work for my company any more.
PS: Hardware Developers are used to use switch over if statements in VHDL and verilog because the synthesiser will really create a parallel structure.
Fabian on 18 Apr 2008 at 9:37 pm #
Thanks Guildenstern for explaining me via IM why you actually do want to do such crazy switches in hardware design
But thats no excuse for doing it in software, ran by regular compilers and interpreters.