Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 15.2: MISRA warning is not generated
#5
You can have as many break statements you like inside your case, as long as it ends with one. The MISRA rules regarding break are there to ban "fall-through" between case statements.

But come to think of it, this actually seems to be a flaw in the MISRA rules. MISRA doesn't allow more than one break inside loops (14.6) nor more than one return inside functions (14.7). But it doesn't mention multiple breaks in a switch statement.

Which one of these examples is "most spaghetti"?

Code:
while(something)
{
  if(x)
  {
    ...
    break; /* non-compliant to 14.6 */
  }
  else
  {
    ...
    break; /* non-compliant to 14.6 */
  }
}

Code:
int func ()
{
  if(x)
  {
    ...
    return x; /* non-compliant to 14.7 */
  }
  else
  {
    ...
    return x; /* non-compliant to 14.7 */
  }
}

Code:
switch(something)
{
  case 0:
    if(x)
    {
      ...
      break; /* compliant? */
    }
    else
    {
      ...
      break; /* compliant? */
    }

    break; /* as long as this break is here to prevent fall-through */
}

I think the switch spaghetti is even less readable than the two non-compliant examples. Perhaps a rule similar to 14.6 should be added, to disallow more than one break in each case, ie ban more than one exit point from each case statement? For the same reason rules 14.6 and 14.7 exist.
<t></t>
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)