Monday, 25 June 2012

ARGH Using Break before Case in Switch Statements


I have a new Number 1 for my “Most Annoying pieces of Syntax use” list… A new annoying item on my personal top ten of code peeves, jumping to the number one position is this:

enum cElements {  One, Two, Three };
eElements element = <something>;

switch (element) {
      default:              throw “unknown element type”;
      break;case One:       HandleOne();
      break;case Two:       HandleTwo();
      break;case Three:     HandleThree();      break;
}

The most annoying thing about this is that it’s a rather intelligent attempt to solve the “default” being forgotten problem, to enforce the default as the first item in the code is pretty neat, but it appears the only reason for their doing this is so that they then use the default stanza’s “break” as the first command before the first case to preserve their bizarre formatting of the end of the last stanza (break) with the selective part of each following stanza (case)… And then they stick a finalizing break after the last case.

The lack of spacing makes the code hard to follow, indeed the use of the breaks in this way make the control flow hard to follow.

And if you don't know how this could be better structured, then please, please go learn, go find out... Scott Meyers has damn good books on C++ and they contain nice style tips!


----- 


Update - I don't know why this text has strange background colours, and the blog interface won't let me see the post in a WYSIWYG mode... grrrr.... Gief me the old interface back.

No comments:

Post a Comment