The python metrics code pymetrics can be used to analyze the complexity of code files. They create two different metrics:
McCabe Complexity Metricfor functions and classes- A
COCOMO 2's SLOC Metricfor the whole file
I would like to know what the numerical values of these metrics mean, not really their definition. What does a value of ‘1’ mean? A number of ‘5’? A number of ’10’? Is higher better or lower? What numerical value refers to good code, what to really bad code? Searching the internet gives some hints, but I am not sure if the classification refers to metric (1) or metric (2). I want to have a meaning for both of these numbers.
I think there is a problematic assumption in your question that good code has a certain value for a certain metric. You can’t really measure the quality of code with a single metric, it’s a combination of things and also highly dependent on context. Really really super efficient code is usually somewhat difficult to understand, does that make it bad code?
IBM decided to measure the quality of their programmers in the 70’s by the lines of code (SLOC) they produce. Needless to say that resulted in some really lengthy and stupid code.
If you wanted to get an understanding of the quality of your code, then what you need is other developers to look at it. Preferably developers that are much more experienced than you. A friendly code review is fantastic for learning, it’ll also force you to think about why you’ve done something the way you did, instead of some other way that it could’ve been done. Luckily stackexchange provides with exactly that.
From wikipedia
Cyclomatic complexity
You can’t say that
1is better than2, it depends on the context (what language are you writing, who are writing the code for etc.). You should think of the cyclomatic complexity value as giving you a hint of how easy it will be to understand the control flow of the code. Lots of nestedifstatements will lead to high a CC. So ideally you’d have a CC of 1 (per function perhaps), that is one function does one thing in one way and nothing else, but obviously that’s not always possible. You have to evaluate whatever value you get for the metric in context.What kind of values do you tend to see in other libraries written in the same language? I can’t give you a figure for that though (sorry). I can say that a CC value of 15 is probably a bit over the top and the code should be refactored. That’s 15 different ways of executing the script/function. 15 different conditions you need to account for in your tests of that one function, and don’t forget all the things that can make those 15 things not work. You’ll need another unit test for them, and combinations of values (you get the point).
Source Lines of Code
COCOMO
The COCOMO on the other hand isn’t really measuring the quality of code, it’s a kind of a costing model for software projects (COCOMO II) being an update for more recent (after 90s) software projects.
Softwaresystems.com has this to say
A function point is a unit of functionality, KSLOC is lines of code in thousands. So the COCOMO model is for estimating the cost, time, required resources etc. of software project, not about evaluating the quality of code.