The goal of this set of coding standards 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.
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 MonkeyProofSolutions 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:
Install CC4M. If you have not purchased the tool yet, you can do so here. Alternatively, you could request a free trial license there.
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.
The coding standards and 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 and coding standards 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) coding standards on higher-level objects such as functions or files.
Security: This chapter contains rules on potential security risks.
Priority of the rule can be one of Mandatory, Strongly recommended and Recommended. Exactly what each of these means for your case is up to you to decide.
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 the reasoning behind the rule: how does your code improve when applying the rule?
Rationale
One or multiple words describing what the rule is about. Examples: Readability, Stability.
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.
This standard includes the rules of the MonkeyProof MATLAB Coding Standard which can be found here and mainly adds naming conventions to it amongst some other rules. Each unique rule has its own unique rule identifier.
The following rules are different:
NAMING-1 in this standard replaces NAMING-20 from the MonkeyProof MATLAB Coding Standard. This rule is about the length of variable names.
The rules in this chapter describe how variables, functions etc. can be named in a consistent and insightful manner.
If you wish to deviate from the casing rules (for example if you want your variable names snake_cased) at least make sure to apply consistent casing.
Use the same casing scheme throughout your code so that classes, functions etc. can be easily identified by their name.
Variable names shall be at most 32 characters long. Function names shall be at least 3 and at most 32 characters long.
Priority
Recommended
Severity level
8
Description
Variable names shall be at most 32 characters long. Function names shall be at least 3 and at most 32 characters long.
Rationale
Names shall be descriptive, so should not be too short. However, variable names can sometimes be single characters (x, y) and still be descriptive. Names that are too long can reduce readability because they introduce long lines of code.
Iterator variables shall be prefixed with i, j, k etc.
Priority
Recommended
Severity level
8
Description
Iterator variable names shall be at least 3 characters long. In nested for-loops, the starting letters of the iterator names shall be subsequent letters in the alphabet.
Rationale
Iterator variables of for-loops are easily distinguishable from other variables by using these prefixes.
Functions and variables shall not shadow MATLAB-installed functionality or other code.
Priority
Mandatory
Severity level
3
Description
Functions and variables shall not shadow MATLAB-installed functionality or other code. This includes MATLAB files (.m, .p, .mlapp, .mlx), mex-files (.mexw32 etc.) and Simulink files (.mdl, .slx).
Rationale
Shadowing code can result in unexpected behaviour, because it is unclear and not properly defined for example what function is called when multiple ones share the same name.
Names of classes, functions, variables, properties and struct fields should not start with temp, my, etc.
Priority
Recommended
Severity level
8
Description
Names of classes, functions, variables, properties and struct fields should not start with temp, my and they should not have names such as myClass, testFunction, etc.
Rationale
Names like these do not properly indicate what the class, function or variable is for.
Name-value pairs shall be UpperCamelCased without numbers in them.
Priority
Recommended
Severity level
8
Description
Name-value pairs shall be UpperCamelCased (PascalCased) and contain no numbers, both when using the arguments block and when using the inputParser function.
Rationale
Consistency in name-value pair naming improves readability of the code and predictability of calling syntax. Name-value pairs in MATLAB functions are UpperCamelCased as well, which means that user-defined functions are more easily integrated with those as well.
classdef CarAnalyzer
methods
function velocity = initialVelocity(theCar)
velocity = theCar.velocity;
end
function position = initialPosition(theVehicle)
position = theVehicle.position;
end
end
end
classdef CarAnalyzer
methods
function velocity = initialVelocity(theCar)
velocity = theCar.velocity;
end
function position = initialPosition(theCar)
position = theCar.position;
end
end
end
This chapter contains rules to help improve readability of your code for yourself and for others.
The rules on indentation and whitespace make the code more visually appealing whereas rules on comments increase the usability of the code they reference.
By using ample whitespace, your code will look clean and the more complicated statements maintain their readability.
Comment blocks (with %{ notation) shall not be used. Every first comment on a line shall start with a comment indicator (% or ...). Use CTRL-R and CTRL-T to comment/uncomment blocks of code.
Indent function bodies on the root level of a file.
Priority
Recommended
Severity level
10
Description
Bodies of all functions (at any level) shall be indented. By default, only method bodies in a classdef file and nested functions are indented. This can be changed in the MATLAB preferences under Editor/Debugger / Language / Indenting by setting the Function indenting format to Indent all functions.
Rationale
This helps maintain the consistency with all other blocks in MATLAB.
Surround the following operators with spaces: = : ^ == <= >= < > ~= & && | || + - * .* / Do not add a space between the unary minus-sign and its operand.
Rationale
Whitespace around operators often leads to improved readability. As an exception, do not use spaces around the equals signs when using the Name=Value syntax introduced in MATLAB R2021a.
Lines shall be no more than 120 characters long, including comments. Use the MATLAB preference for displaying a vertical line at 120 characters. Comments can be automatically wrapped by setting a MATLAB preference.
Rationale
A line of code should fit on a screen. Long lines of code are difficult to interpret and generally indicate great complexity.
Do not use tab characters. Use spaces instead. By default, MATLAB inserts spaces when the TAB key is pressed.
Rationale
The number of characters used to display a tab character may vary depending on the MATLAB preferences. Inserting spaces prevents any display issues related to this.
The rules in this chapter are about how to use constructs such as if/else blocks, what built-in functions or keywords to avoid or how to best structure your statements.
Following these rules will ensure the individual statements in your code are complete and easy to read.
Define and document numeric values as variables before using them in an expression.
Priority
Strongly recommended
Severity level
5
Description
Do not use magic numbers in your expression. Define them as variables before using them.
Rationale
This encourages reuse and documentation of numerical constants and improves overall readability. As an exception, unit conversion (for example, multiplying by 1e6) can be directly applied.
Every if shall have an else section, even if it does not contain executable code.
Rationale
By including an else section, no execution paths are overlooked. Additionally, code coverage can be accurately reported because without else, it is unclear whether the if-condition is ever false.
Exceptions
else sections may be omitted in case of input processing or error handling.
Every switch shall have an otherwise section, even if it does not contain executable code.
Rationale
By including an otherwise section, no execution paths are overlooked. Additionally, code coverage can be accurately reported because without otherwise, it is unclear whether it happens that none of the cases occur.
Use parentheses to clarify precedence in logical expressions.
Priority
Strongly recommended
Severity level
5
Description
Use parentheses to clarify the precedence of operands in expressions with multiple logical operators. No parentheses are required when only one type of logical operator is used. For example: d = a && b && c;.
Rationale
By clarifying the precedence of the operands in such expressions, readability is improved and unexpected behaviour is prevented.
Use parentheses to clarify precedence in mathematical expressions.
Priority
Strongly recommended
Severity level
5
Description
Use parentheses to clarify the precedence of operands in expressions with multiple mathematical operators. No parentheses are required when only one type of mathematical operator is used. For example: d = a * b * c;.
Rationale
By clarifying the precedence of the operands in such expressions, readability is improved and unexpected behaviour is prevented.
Using return decreases readability because the end of the function is not always reached. By avoiding the return keyword, the flow of the code remains clear.
Make sure all functions called are known by MATLAB.
Priority
Mandatory
Severity level
1
Description
Make sure all functions called are known and accessible by MATLAB. Be aware that functions might be unknown with core MATLAB only, they might require the install of toolboxes.
Rationale
Attempting to access packages, classes and functions not on the MATLAB path will result in an error.
Only use the try construct for exception handling.
Priority
Strongly recommended
Severity level
5
Description
Only use the try construct for exception handling. An exception object must be assigned or created and an error function must be called in the catch. Do not use try/catch to suppress errors or to express simple conditions. There are other, more suitable options for that.
Rationale
Using try for simple error suppression can result in poor performance and unexpected behaviour. When a different error occurs than the one expected, it can go undetected because of the try/catch.
Avoid using cell arrays except for harnessing character arrays or objects of different classes.
Priority
Recommended
Severity level
5 if the code is meant for code generation, otherwise 7
Description
Do not use cell arrays for anything other than character arrays of objects of different classes.
Rationale
Cell arrays may be disadvantageous for the performance of the code. Readability is decreased as well when cell arrays are used instead of regular, homogeneous arrays.
Keep number of lines of code and cyclomatic complexity to an acceptable level, especially for nested functions and methods in a classdef file. Use the values of the table below.
Rationale
Short functions are more likely to be readable, reusable and testable.
Use functions even if there are no inputs or outputs.
Rationale
The scope of variables in a script is not bounded as one might expect. It is therefore safer to use functions instead. Additionally, scripts are not compatible with Matlab Coder.
Exception
Initialization of a project, or specifically creating base workspace structures that are used in models, without the need for assignin.
Prevent or suppress Code Analyzer messages shown in the MATLAB editor. When the messages cannot be prevented, suppress them on an individual basis and add the reason in the comments.
Rationale
The messages usually indicate that improvements can be made to the performance or stability of the code. Adding file-wide Code Analyzer suppressions is discouraged because new triggers of the messages may be overlooked.
The first output of a constructor should be the object of the class.
Priority
Mandatory
Severity level
1
Description
The first output of a constructor should be the object of the class.
Rationale
If the first output is not the object of the class, it will result in a runtime error. It is not logical and harder to read if the first output of a constructor is not the object of the class.
Do not use iterator variables outside the for loop.
Priority
Mandatory
Severity level
2
Description
Do not use iterator variables outside the for loop, since this might lead to unexpected behavior.
Rationale
Using a loop iterator variable outside the for loop might indicate a programming error. For example if the for loop is not entered, the iterator variable becomes an empty double.
function processTemperatures(Tin1, Tin2, Tin3)
x = calculateMaxTemperature(Tin1, Tin2, Tin3);
end
function out = calculateMaxTemperature(Tin1, Tin2)
out = max(Tin1, Tin2);
end
function processTemperatures(Tin1, Tin2, Tin3)
x = calculateMaxTemperature(Tin1, Tin2);
end
function out = calculateMaxTemperature(Tin1, Tin2)
out = max(Tin1, Tin2);
end
Classes shall not implement properties inherited from superclasses.
Priority
Mandatory
Severity level
1
Description
Classes shall not implement concrete properties that are inherited from a superclass. In order to assign default values to concrete properties inherited from a superclass, assign the values in the constructor of the subclass, or make the properties Abstract in the superclass.
Rationale
Attempting to use such a class will result in a runtime error.
Exception
There are two separate conditions under which you can redefine superclass properties: The inherited property is abstract. The values of the superclass property SetAccess and GetAccess attributes are private.
classdef Measurement
properties
measurementDate
end
end
classdef Pressure < Measurement
methods
function obj = Pressure()
obj.measurementDate = today();
end
end
end
Do not use the built-in eval. Minimize the use of feval, evalin and evalc.
Priority
Mandatory
Severity level
3
Description
Do not use the built-in eval. Minimize the use of feval, evalin and evalc.
Rationale
These functions can have harmful results and statements using them are usually difficult to read and maintain. Additionally, it may be overlooked when variables are defined or altered by calls to these functions.
When using the weboptions or matlab.net.http.HTTPOptions classes, do not set an empty certificate file name.
Priority
Mandatory
Severity level
4
Description
When using the weboptions or matlab.net.http.HTTPOptions classes, do not empty the CertificateFilename option.
Rationale
For security reasons, it is discouraged to empty the CertificateFilename property of a weboptions or matlab.net.http.HTTPOptions object because that disables certificate validation.
When reading an XML file, set the AllowDoctype option to false.
Priority
Mandatory
Severity level
3
Description
When using the xmlread or matlab.io.xml.dom.Parser functions, make sure to set AllowDoctype to false.
Rationale
In XML, doc type definitions can contain exploding content that -if not caught- can cause unexpected behavior. Therefore, doc types should not be allowed to be read.
content = xmlread(myFile, "AllowDoctype", false);
% For matlab.io.xml.dom.Parser configurations, AllowDoctype is false by default.
p = matlab.io.xml.dom.Parser();
p.parseFile(myFile);
Avoid constant variable names as: username, loginname, password etc.
Priority
Mandatory
Severity level
3
Description
Avoid the use of contant variable names as: username, loginname, password, credentials etc, instead obtain these via a user input action or read them from a (secured) file.
Rationale
For security reasons, it is discouraged to use hard-coded username and or password since this way they can be easily shared with others accidentally.
Do not use hard-coded values for Username and Password.
Priority
Mandatory
Severity level
3
Description
Do not use hard-coded values for the properties Username and Password, instead obtain these via a user input action or read them from a (secured) file.
Rationale
For security reasons, it is discouraged to use hard-coded username and password since this way they can be easily shared with others accidentally.
import matlab.net.http.Credentials
% Ask the user to provide a username and password, for example using a dialog or prompt:
[username, password] = showLoginDialog();
creds = Credentials('Username', username, 'Password', password);
weboptions("Username", username, "Password", password);
Note: This is just one example of how to obtain credentials without hardcoding them. Ask your IT department for the available options.