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
This practice improves readability by preventing exceptions from obscuring the normal path of execution.
fid = fopen(fileName);
if (fid~=-1)
:
else
:
end
Make sure that the exceptions don't obscure the normal path of execution. Using the block comment feature of the editor is preferred.
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
Character strings work well in this context and they are usually more meaningful than enumerated cases.