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):
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.
By
Ken Orr
Ken is a developer on the MATLAB Desktop team. He loves the art of graphic design as well as developing visually pleasing user interfaces - he's one of those 'crazy' Mac guys!
07:23 UTC |
Posted in Command Window, Editor |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
Is “mlint(’MyClass’,'-cyc’)” supposed to work with class definitions?
/per
Per—mlint -cyc does not work with classdef’s in R2008a. That’s been fixed for the upcoming R2008b release.