Contenu principal

himl_0006: MATLAB code if / elseif / else patterns

R2026b

Terminate if/elseif/else constructs with an else statement containing meaningful comment

Usage: High-Integrity System Modeling

Guideline ID: himl_0006

Rules

himl_0006: MATLAB® code if / elseif / else patterns

For MATLAB code with if / elseif/ else constructs, terminate the constructs with an else statement that includes at least a meaningful comment. A final else statement is not required if there is no elseif.

Rationale

  • Defensive programming

  • Readability

  • Traceability

Verification

Check if/elseif/else patterns in MATLAB Function blocks (Simulink Check)

Example — Correct

  • if u > 0
        y = 1;
      end
    

  • if u > 0
        y = 1;
      elseif u < 0
        y = -1;
      else
        y = 0;
      end
    

  • y = 0;
      if u > 0
        y = 1;
      elseif u < 0
    y = -1;
      else
        % handled before if
      end
    

Example — Incorrect

  •   % empty else
      y = 0;
      if u > 0
        y = 1;
      elseif u < 0
        y = -1;
      else
      end
    

  •   % missing else
      y = 0;
      if u > 0
        y = 1;
      elseif u < 0
        y = -1;
      end
    

Industry Standards

  • IEC 61508-3, Table A.3 (3) 'Language subset'
    IEC 61508-3, Table A.4 (3) 'Defensive programming'

  • IEC 62304, 5.5.3 - Software Unit acceptance criteria

  • ISO 26262-6, Table 1(b) 'Use of language subsets'
    ISO 26262-6, Table 1(d) 'Use of defensive implementation techniques'

  • EN 50128, Table A.4 (11) 'Language Subset'
    EN 50128, Table A.3 (1) 'Defensive Programming'

  • EN 50657, Table A.4 (11) 'Language Subset'
    EN 50657, Table A.3 (1) 'Defensive Programming'

  • EN 50716, Table A.3 (1) 'Defensive Programming'

  • DO-331, Section MB.6.3.1.e 'Conformance to standards'
    DO-331, Section MB.6.3.2.e 'Conformance to standards'
    DO-331, Section MB.6.3.3.e 'Conformance to standards'

Version History

Introduced in R2014a

expand all