Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
14.10 Hiding an else if construct
#1
The following

Code:
if ( x < 0 )
{
    log_error(3);
    x = 0;
}
else if ( y < 0 )
{
   x = 3;
}
else
{
/* no change */
}

Can be coded as

Code:
if ( x < 0 )
{
    log_error(3);
    x = 0;
}
else
{
   if ( y < 0 )
   {
      x = 3;
   }
}

to hide the "else if". Is this OK?

I think the code blocks are semantically identical but the last one does not list the else.

Brian
<t></t>
#2
Your implementation makes it clear to a reviewer that there are only two paths associated with the test of x and is acceptable. The intent of Rule 14.10 is to catch instances where the final else of an if ... else if ... chain has been accidently omitted.
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 1 Guest(s)