主要内容

himl_0007: MATLAB code switch / case / otherwise patterns

R2026b

Include at least two case statements and an otherwise statement with a meaningful comment in switch constructs

Usage: High-Integrity System Modeling

Guideline ID: himl_0007

Rules

himl_0007: MATLAB® code switch / case / otherwise patterns

For MATLAB code with switch statements, include:

  • At least two case statements.

  • An otherwise statement that at least includes a meaningful comment.

Rationale

  • Defensive programming

  • Readability

  • Traceability

Verification

Check switch statements in MATLAB Function blocks (Simulink Check)

Example — Correct

  • switch u
        case 1
          y = 3;
        case 3
          y = 1;
        otherwise
          y = 1;
      end
    

  • y = 0;
      switch u
        case 1
          y = 3;
        case 3
          y = 1;
        otherwise
          % handled before switch
      end
    

Example — Incorrect

  •   % no case statements
      switch u
        otherwise
          y = 1;
      end
    

  •   % empty otherwise statement
      switch u
        case 1
          y = 3;
        case 3
          y = 1;
        otherwise
      end
    

  •   % no otherwise statement
      switch u
        case 1
          y = 3;
      end
    

Tips

  • If there is only one case and one otherwise statement, consider using an if / else statement.

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'

  • MISRA C:2012, Rule 16.4

  • MISRA C:2023, Rule 16.4

Version History

Introduced in R2014a

expand all