Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Debuggable Blog:
Code Insults Round 1 - Why switch blocks are dumb
Oct 29, 2008 @ 13:48:28

In the first of his "I will insult your code" series, Nate Abele looks at this submitted code and points out that maybe blocks of switch/case statements aren't such a good idea after all.

The entire submission is actually two files, which together comprise a console script for interacting with a web service (the names of the entrants have been withheld to protect the identities of the guilty). Rather than examine the full entry, we're going to take a look at one part which I find comes up fairly often: switch block overkill.

The example he's talking about has a switch statement with eight different cases under it, most of them just setting two properties on the current class. Its used ot map command line parameters to their correct properties. Nate suggests a bit different method - still using switch/case but pulling the properties to be assigned from an array of options rather than hard-coding them into the evaluation.

tagged: switch case block insult refine overkill property

Link:


Trending Topics: