| MathWorks.com |
| MATLAB Central > File Exchange > How are code metrics defined? |
How are code metrics defined?
We publish six numbers (explained in detail below) associated with each submission. The two most important metrics, cyclomatic complexity and M-Lint message count, appear on the main page for each submission. The other metrics are available on that submission's Code Metrics page.
Notes- Metrics are calculated for M-files only.
- The code we use to score all entries is available for download from the File Exchange (add link when ready), so you can see how your code does before you submit it.
Cyclomatic Complexity
"Cyclomatic complexity", also known as McCabe complexity, measures the number of independent paths through a program. It correlates well with how difficult a file is to understand or test. The higher the number, the more complex the function. A score of 10 or less is good, while scores above 50 should be avoided at all costs. More information about cyclomatic complexity is available here.
If your code has a high complexity score, consider breaking out subfunctions and simplifying your code flow where possible.
When a submission contains more than one function, the maximum cyclomatic complexity is used for the aggregate score.
M-Lint Messages
M-Lint is a MATLAB function that inspects M-files and generates a list of problems, suspicious constructs, and opportunities for performance improvement. When a submission contains more than one M-file, the average number of M-Lint messages per M-file is used for the aggregate score.
When a submission contains more than one M-file, the average number of M-Lint messages per M-file is used for the aggregate score.
Notes and Exceptions:
Generally M-Lint provides helpful advice that will improve your code, but occasionally it tells you something that is either impossible or impractical to act on. For example, if you need only the second output from a two-output function, M-Lint will complain that no use was made of the first output. That is, suppose you need to know the sort index matrix for the vector x.
[y, i] = sort(x)
You have no way to generate i without also generating y, but M-Lint nevertheless says "The value assigned here to variable 'y' might never be used." Because of situations like this, it is not always reasonable to have an M-Lint metric of zero. Functions automatically generated by MATLAB's GUI editing tool GUIDE have many instances of messages like this.
Also, keep in mind that we're using the latest version of MATLAB for our M-Lint messages. As a result, the messages we display may not match the ones you see on your machine.
Help Warnings
Help warnings occur when your file's help is missing certain sections. We check each file for the following:
- Is there any help at all?
- Does the help begin with a single descriptive help line (sometimes known as an H-1 line)?
- Is there an example?
Score 1 for each negative answer to the above questions. The best possible score is 0.
The word "example" should appear on a line by itself, with runnable code appearing below it. The metrics code detects examples that follow this general pattern.
% Example:% Examples% Example 1
When a submission contains more than one M-file, the average number of help warnings is used for the aggregate score.
Filename Clashes
This number represents the number of occurrences of filenames that clash with functions in MATLAB and other MathWorks products. For instance, don't call your plotting function PLOT.M, because PLOT is a standard MATLAB function. When naming your functions it's generally a good idea to avoid extremely generic names because of the likelihood of name clashes.
When a submission contains more than one M-file, the total number of name clashes is used for the aggregate score.
Cloned Code
When a submission has long stretches of the exact same code in multiple places, it is ripe for refactoring. Whenever you clone code, you're creating a need to update the same code in multiple places next time around. Cloned code increases program size, costs more to maintain, and is more likely to break.
When a submission contains more than one M-file, the total amount of cloned code is used for the aggregate score.