Ken & Mike on the MATLAB Desktop

September 8th, 2008

Let M-Lint help simplify your code

M-Lint is an indispensable tool when writing code in the MATLAB Editor. It spots errors and potential errors as you edit, which makes writing good code much easier. There’s one feature that M-Lint offers, though, that isn’t available through the live mode in the Editor: McCabe complexity calculation.

The McCabe complexity (also referred to as Cyclomatic Complexity) is a metric used to measure how complex a piece of software is. The more complex it is, the harder it is to maintain and debug. Thus this metric can be used in an effort to help simplify your code.

To calculate the McCabe complexity, you’ll have to run M-Lint from the command line, using the -cyc flag. We’d like to integrate this warning into the standard set of live warnings, but we’re going to leave it out until we introduce a mechanism to set the complexity threshold via the M-Lint preferences panel.

At the command prompt, type the following (the first argument is your file name):

>> mlint(‘penny’,‘-cyc’)

You’ll see something like this:

L 0 (C 0): The McCabe complexity is 1.

A value of one is really good, though probably not a realistic goal to shoot for. A commonly accepted target value is ten.

I’ve found that using this metric really helps me write clearer more maintainable code. Even being aware that I’m going to check my code using this metric helps me design with simplicity in mind.

What’s the average complexity of your files?

Check out McCabe’s original paper on measuring complexity for the math behind his measurement technique.

2 Responses to “Let M-Lint help simplify your code”

  1. per isakson replied on :

    Is “mlint(’MyClass’,'-cyc’)” supposed to work with class definitions?
    /per

  2. Steve Eddins replied on :

    Per—mlint -cyc does not work with classdef’s in R2008a. That’s been fixed for the upcoming R2008b release.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Ken & Mike work on the MATLAB Desktop team.
  • Ori: The current folder shortcut used to be alt-y. Now it is alt-o. However, while in the editor window, alt-o opens...
  • Jose Miguel: Hi First of all, thanks for the post, it is really useful. I am trying to develop a Java GUI within...
  • Ken: Hi Siddharth, There isn’t currently any way to move the docking controls. Feel free to submit an...
  • Phil: I have the same problem as described above with UITable working fine in the Matlab environment but showing no...
  • Siddharth: Is there any way to move the position of the docking controls (or eliminate them through some...
  • Chris: Yes, it is a challenge to organize functions into categories, especially with so many functions. Ken and I...
  • Mike: Thanks for fielding that one, Yair.
  • Yair Altman: Jimmy - if you mean that you wish to include hyperlinks in your function’s help comment, that will...
  • OysterEngineer: Thanks for explaining the Function Browser. I fired it up and gave it a try. It appears that it has...
  • Jimmy: Is there any way to include hyperlinks in a comment, such as the standard help at the beginning of a function?

These postings are the author's and don't necessarily represent the opinions of The MathWorks.