Introduction
The goal of this standard that can be automatically checked is to help you write consistent, clean, understandable and maintainable m-code. The resulting code will be more readable for both yourself and others. These rules are no strict rules that must be followed at all cost. They are simply there to help you write m-code well and you can deviate from them as you please.
Automatic compliance checking
Most of the rules described here can be checked using CC4M by MonkeyProof Solutions. The checkable rules are indicated by this logo at the top. These rules are configured in the predefined MonkeyProofMATLABCodingStandard
configurations set available with the tool. For instructions on how to check your code against a coding standard, see the video below. The steps are also described below the video.
You can check your code for compliance with the rules described here as follows:
- Get CC4M and install per installation instructions.
- Open CC4M in one of two ways:
- Click the shortcut created at the top of the screen next to the documentation search bar: .
- Run
monkeyproof.cc4m.start()
from the command window.
- Select whether you want to check one or more files, all files in a folder, or all files in a MATLAB Project.
- Select what files/folder/project to check.
- Further customization is available, for example checking a folder including or excluding all subfolders.
- Click the Run button at the bottom to start checking your code.
Realization
This coding standard has been constructed in consultation with multiple large companies that are using MATLAB on a daily basis. With years of experience in the industry, they are able to provide invaluable input on what MATLAB-related coding rules to follow and why.
With the help of -among others- Paul Jansen of TIOBE, we have been able to come to a coding standard that reflects the needs and wishes of professional teams trying to write clean and reliable MATLAB code that is easy to read and maintain.
Structure of this document
The rules are grouped under specific categories to help keep things organized:
- Naming conventions: Rules in this chapter describe how variables, functions etc. can be named in a consistent and insightful manner.
- Layout & comments: This chapter contains rules to help improve readability of your code for yourself and for others. There are rules on whitespace, comments and more.
- Statements & expressions: The rules in this chapter are about how to use constructs such as
if/else
blocks, what built-in functions or keywords to avoid, how to best structure your statements etc. - Higher level rules: This chapter contains (among others) rules on higher-level objects such as functions or files.
- Security: This chapter contains rules on potential security risks.
Rule template
Every rule is structured as follows:
Rule name
ID | A unique rule identifier. |
Title | Brief description of the rule. |
Priority | Priority of the rule can be one of Mandatory, Strongly recommended and Recommended. |
Severity level | Severity level of the rule can be in the range from 1 to 10. The most important ones are of severity level 1 and next levels are reserved for more pedantic issues. Each of the levels is described below. |
Description | A more elaborate description of the rule. Can include exceptions. |
Rationale | Rationale or justification behind the rule: why should you apply this rule and how does your code improve when applying it or deteriorate when violating it? |
Exception (Optional) | An exception might apply to a rule, i.e. a case for which the rule does not hold. |
Avoid:
One or more code examples to avoid using.
Instead use:
The example(s) above improved by complying with the rule.
Severity levels
The following definition of the severity levels is used as a guide to make it easier to group the different rules and coding standards.
- Level 1 - Certainly a programming error
- Level 2 - Very likely a programming error
- Level 3 - Programming pitfall/undesired programming construct/accident waiting to happen
- Level 4 - Design issue with high impact on reliability or maintainability
- Level 5 - Design issue with low impact on reliability or maintainability
- Level 6 - Portability or performance issue or high impact other issue
- Level 7 - Comment related issue or medium impact other issue
- Level 8 - Naming convention or low impact other issue
- Level 9 - Style related issue
- Level 10 - Controversial issue or issue with a relatively high false positive rate
Each rule in this coding standard has a severity level attached to it. These severity levels are set in the CC4M and one can configure for which severity levels to check. The severity levels are related to the severity levels of Tiobe's TICS tooling.