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.
要发表评论，请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。