For the most part, there is no difference between
assert(X,...)
and
if (~X) error(...) end
and your choice between them is a matter of convenience or style.
The difference between non-production and production code in MATLAB projects often does not coincide with the difference in projects based on other languages.
This is partly because, as you say, MATLAB is usually interpreted, not compiled; although it is possible to create applications using MATLAB Compiler or Builder, which, although not strictly “compiled,” do not have visible source code and cannot be debugged. For such applications, you will need to handle exceptions and errors as carefully as with a compiled language.
This is also partly because “production” often means something different for projects that use MATLAB than for projects in other languages; for example, this may mean that the MATLAB code is automatically converted to C for deployment on a car engine controller, or it may mean that some MATLAB code ran a financial forecasting model and wrote the results to a production database.
There is a special case where assert should be used instead of if..error..end , which is used when you use MATLAB Coder to generate C code from MATLAB code. MATLAB Coder checks the assert in MATLAB code to determine the properties of the variables that it needs to convert to C, and can generate more efficient C code if it can accept facts about the variables you state (for example, sizes and types of arrays).
One last point: for the specific activity that you are mentioning, I would not use a single method for enforcing the function signature - inputParser , as a rule, is more reliable and consistent (albeit a little detailed), but, more importantly, it encourages Design you first.
source share