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.
  • DP: Hi i have a problem with ezplot3, i want to plot more than i curve in the same graph but hold on command...
  • Ken: Hi Arsalan, Unfortunately there is no way to get the new Editor API in older versions of MATLAB. -Ken
  • Arsalan: Hi, I am very excited about the MATLAB API for editor because right now i am working on a project and i need...
  • Johannes: Since I started using matlab-emacs some days ago I never experienced Emacslink. But I experienced some...
  • Francisco J. Beron-Vera: Hi all, I have recently learned about ViEmu (http://www.vimemu.c om) which, for Vi/Vim...
  • OysterEngineer: When I first learned of the Publish feature in MatLab, I thought it might be useful to help to...
  • Ken: Hi Herve, I’m not quite sure what you mean by “stand-alone&# 8221; mode? -Ken
  • Herve: I wonder when the publish fonction will be supported in standalone mode.
  • Mike: Ravi, What you described should work as far I understand it. Please follow up with technical support. With a...
  • Mike: @Daniel, Thanks for that note.

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