14.10 /* no else needed */

6.14 Control Flow

Moderators: misra-c, david ward

Post Reply
ABR
Posts: 1
Joined: Tue Oct 06, 2009 10:06 am
Company: Marturion

14.10 /* no else needed */

Post by ABR » Thu Oct 22, 2009 2:15 pm

In rule 14.10 (all if..... else if constructs shall be terminated with an else clause":

is the /* no else needed */ comment on the single if block with no else a suggestion for use in code, where seems useful?

jbezem
Posts: 20
Joined: Mon Aug 01, 2005 8:24 pm
Company: JB Enterprises
Location: Germany
Contact:

Re: 14.10 /* no else needed */

Post by jbezem » Tue Oct 27, 2009 6:37 am

IMHO, no, a simple if-statement without an else-clause need not be accompanied by a comment indicating the lack of an else-clause. Also, no empty else-clause shall be required. Especially when simple if-statements are encountered, such comments would clutter the code and impair understandability.
E.g.:

Code: Select all

if (a == 1)
{
  call_come_function();
}
/* No else needed */
If the comment indicates _why_the else-clause is unneeded, that maybe helpful, but should be left to the developer or the code review.
Rule 14.10 explicitly refers to chained if ... else if ... else if ... statements, where for better clarity the braces around the subsequent if-statements are omitted. To compensate for the missing braces (and extra indentation), the final else-clause shall be provided.

Regards,

Johan

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: 14.10 /* no else needed */

Post by misra-c » Tue Nov 10, 2009 12:31 pm

The comment in the example indicates that no else clause is required in this case. It is not the intention of this rule to require such comments for a simple if statement that has no else clause.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.14 Control Flow”