C embedded - Simplicity concept

Simplicity concept

- Logical operations can always be written in many ways.

- Choose the form that is easiest to understand.

- Keep it simple

Example:


extern int x, y, z;
void f (void)
{
  if ((x < 4) && !(y || (z == 1)))
  {
     printf("do something");
  }

  if ((y == 0) && (z != 1) && (x < 4))
  {
     printf("do something");
  }
  if (!((x >= 4) || y || (z == 1)))
  {
     printf("do something");
  }
  
  if (x < 4)  
  {
    if (!(y || (z == 1)))
    {
      printf("do something");
    }
  }
}

MISRA rules

MISRA-C:2004, Rule 12.4: The right-hand operand of a logical && or || shall not
contain side effects.

MISRA-C:2004, Rule 12.5: The operands of a logical && or || shall be primary
expressions.
– i.e. constants or parenthesized.

MISRA-C:2004, Rule 12.6: The operands of logical operators shall be effectively
Boolean.
– i.e. serve a boolean purpose

Etichete

Afișați mai multe

Arhiva

Afișați mai multe