complexity-report is a tool for reporting code complexity metrics in JavaScript projects. As a general rule, the more complex the code, the more buggy it is going to be. Therefore, it is a good idea to try to keep your code simple.
complexity-report is a node package which can be installed using the following command:
npm install -g complexity-report
The tool can then be run from the command line. See the complexity-report website for a list of the available options.
The tool currently reports the following metrics for each function:
- Line No.
- The line number on which the function’s code starts.
- Physical SLOC
- SLOC stands for “source lines of code”. Physical SLOC is therefore the number of physical lines of code.
- Logical SLOC
- The number of logical lines of code.
- Parameter count
- A large number of parameters can indicate that a new object should be created to group them.
- Cyclomatic complexity
- The number of linearly independent paths through a program’s source code. A high number is bad and generally leads to more bugs. As a rule of thumb, aim to keep the cyclomatic complexity under ten for each function.
- Halstead difficulty, volume and effort
- The number of bugs (B) increases with difficulty (D), volume (V) and effort (E). Estimates for the number of delivered bugs can be calculated from these metrics as B = E 2/3 / 3000 or B = V / 3000.
In addition, complexity-report also reports an overall figure for:
- Maintainability
- The maintainability index aims to measure the ease with which the code can be maintained in order to isolate and correct defects, meet new requirements or cope with environmental changes. It is calculated from metrics such as the number of lines of code and Halstead complexity measures.
- Aggregate cyclomatic complexity
- An attempt at measuring the overall cyclomatic complexity of the code.
- Mean parameter count:
- The mean number of parameters per function.
Phil Booth gives a good overview of the different types of complexity measured at JSComplexity.org. You also might like to take a look at Wikipedia’s entry on Halstead complexity measures for further details, including how they are calculated.