Tuesday 8 December 2009

Call to arms: Ban Case / Switch statements

Of late I've been keeping up with the Hashrocket bookclub which is streamed on Tuesday evenings at 5.12pm GMT. Each week the folks discuss a chapter or two of the current book Refactoring: Ruby Edition

I noticed there was a running theme through a number of the 'Organising Data' refactorings which was removing case statements.

I've been at this coding game for about a decade now and I've only seen what I would call one valid use of a case statement. Every other use benefitted from being replaced by polymorphism.

With that in mind I'd like to see the case and switch keywords removed from all OO languages as it actively encourages poor design and less flexible code.

Any language designers out there reading?
The one case I mentioned above where a switch was useful could easily be replaced by an if, else if condition. Save yourself the hassle of implementing case or switch.

No comments: