Conditionals
Complex conditional expressions should be avoided; introduce temporary logical variables instead
Complex conditional expressions should be avoided; introduce temporary logical variables instead
By assigning logical variables to expressions, the program gets automatic documentation. The construction will be easier to read and to debug.
if (value>=lowerLimit)&(value<=upperLimit)&~ismember(value,valueArray)
:
end
should be replaced by:
isValid = (value >= lowerLimit) & (value <= upperLimit);
isNew = ~ismember(value, valueArray);
if (isValid & isNew)
:
end
The usual case should be put in the if part and the exception in the else part of an if/else statement
The usual case should be put in the if part and the exception in the else part of an if/else statement
This practice improves readability by preventing exceptions from obscuring the normal path of execution.
fid = fopen(fileName);
if (fid~=-1)
:
else
:
end
The conditional expression if 0 should be avoided, except for temporary block commenting
The conditional expression if 0 should be avoided, except for temporary block commenting
Make sure that the exceptions don't obscure the normal path of execution. Using the block comment feature of the editor is preferred.
A switch statement should include the otherwise condition
A switch statement should include the otherwise condition
Leaving the otherwise
out is a common error, which can lead to unexpected results.
switch (condition)
case ABC
statements;
case DEF
statements;
otherwise
statements;
end
The switch variable should usually be a string
The switch variable should usually be a string
Character strings work well in this context and they are usually more meaningful than enumerated cases.