Matlab Code Analyzer
MATLAB comes with the very important tool MLINT, which can check your code for common defects. Experience shows that these hints can be very helpful for cleaning up MATLAB code, and preventing simple errors.
Crucially though, MLINT is not a style checker. That is where this program comes in:
Say you have some code in
ugly_code.m. You can analyze this code for problems using one simple command:
This might produce a report like this:
Code Analysis for ugly_code.m Required files: ugly_code.m, ugly_toolbox.m Required toolboxes: MATLAB, Signal Processing Toolbox Function ugly_code (Line 1, col 18): Number of lines: 67 (high) Number of function arguments: 2 (good) Number of used variables: 5 (good) Max level of nesting: 3 (high) Code complexity: 6 (good) Line 1, col 1: too few comments (2 comments for 67 lines of code) Line 1, col 10: return argument 'szOut' is very short (used 5 times across 38 lines) Line 1, col 18: function argument 'testInput' is not mentioned in the documentation Line 15, col 84: very long line Line 20, col 22: no spaces after operator ',' Line 27, col 1: incorrect indentation Line 27, col 1: variable 'szOut' is very short (used 5 times across 38 lines) Line 27, col 23: variable 'text' shadows a built-in Line 27, col 34: Eval should never be used Line 39, col 10: no spaces around operator '='
A report like this will be printed for every function in the file, for script-files, and for classes. The more serious of these comments will be highlighted in red, whereas less important ones will stay black. Every line number is clickable and opens directly in the editor.
Additionally, this comes with a settings file
check_settings.m, which can change the thresholds on all warnings, and even enable or disable whole categories of warnings entirely.
While this file works well for our current applications, it is a complex piece of software, and it has not been thoroughly tested yet. If you find a bug, or would like to see a new feature, or would like to contribute a new feature, please feel free to open an issue or pull request.
However, this is not my job, and I can not guarantee an immediate response, or support for every problem. That said, the code is available under the terms of the BSD 3-clause license, so feel free to use it however you like as long as you honor my authorship of it.
Also, please bear in mind that all of the warnings generated by this program are just that: Warnings. They are not laws. If a slightly longer line improves readability, please do not make it shorter just to make the style analyzer happy. Please watch this video for some context.
And finally, while this style checker can find many issues, it is by no means perfect. It can not comment on whether your variable names are good or not, whether your comments are out of date or not, or whether your code makes intuitive sense when reading, or just results in confusion. For more in-depth heuristics on how to improve these aspects of your code, pleas read the wonderful MATLAB Style Guidelines 1.0.