Main Content

mp_0022: Format of Conditional Expressions

Since R2024b

Guideline Publication

Control Algorithm Modeling Guidelines - Using MATLAB®, Simulink®, and Stateflow®

Sub ID Recommendations

  • NA-MAAB — a, b, c1/c2

  • JMAAB — a, b, c1/c2

MATLAB Versions

All

Rule

Sub ID a

Conditional expressions shall be written as expressions that evaluate to a single logical value (true or false).

Example — Correct
% foo
・・・ a non-logical variable
if (foo ~= 0)
Example — Incorrect
% foo
・・・ a non-logical variable
if foo

Sub ID b

The number of binary operators per line in a conditional expression shall be defined.

Example — Correct
% If up to 3 is allowed 
if (foo1 && foo2) ...
   || (foo3 && foo4 && foo5)
Example — Incorrect
% If up to 3 is allowed
if (foo1 && foo2) || (foo3 && foo4 && foo5)
Custom Parameter

The number of binary operators per line in a conditional expression.

Sub ID c1

When dividing a conditional expression into multiple lines, line breaks shall be inserted immediately after a logical operator, and the subsequent lines shall be indented.

Example — Correct
if foo1 || ...
   foo2 || ...
   foo3
end
Example — Incorrect
if foo1 ...
   || foo2 ...
   || foo3
end

Sub ID c2

When dividing a conditional expression into multiple lines, line breaks shall be inserted immediately before a logical operator, and the subsequent lines shall have aligned indentation.

Example — Correct
if foo1 ...
   || foo2 ...
   || foo3
end
Example — Incorrect
if foo1 || ...
   foo2 || ...
   foo3
end

Rationale

Sub ID a:

  • The conditions for true and false are clarified.

Sub ID b:

  • Limiting the number of operators makes it easy to understand the relationships between each condition.

Sub ID b:

  • Limiting the number of operators makes it easy to understand the relationships between each condition.

Sub IDs c1, c2:

  • The relationships between each condition become easier to understand.

Verification

Model Advisor check: Check description of conditional expressions (Simulink Check)

Last Changed

R2024b

Version History

Introduced in R2024b