Software complexity analysis for JavaScript projects

About complexity

Complexity is the quality of consisting of many interrelated parts. When software consists of many interrelated parts, it becomes more difficult to reason about. Software that is difficult to reason about is a more fertile breeding ground for bugs than software that is simple.

Every problem space contains some level of inherent complexity, which is shared by all possible solutions. However, as programmers, we can reduce the complexity of our chosen solutions by limiting the interrelatedness of their constituent components. This is commonly referred to as favouring cohesion over coupling, and forms the bedrock on which axioms such as the single responsibility principle are built.

In codebases that are large and/or unfamiliar, it can be difficult to know whether regions of complexity exist and where they might be. By defining metrics of complexity, the search for offending components can be automated and brought into the existing build process alongside other forms of static analysis and unit tests. Although the metrics themselves are far from perfect, they can be useful in helping to identify areas of code that warrant closer inspection. They can also be tracked over time, as an indicator of the direction that overall code quality may be moving in.

The metrics that are reported by this site are generated by escomplex, which can be used in such a way on JavaScript projects. Currently, it is able to report on the following complexity metrics: lines of code, number of parameters, cyclomatic complexity, cyclomatic complexity density, Halstead complexity measures, maintainability index, dependencies, first-order density, change cost and core size.

The key point with all of these metrics is that the prescribed threshold values should not be considered as definitive indicators of whether a particular piece of code is "too complex", whatever that might mean. Software development is a broad, varied practice and every project is subject to a unique set of countless environmental factors, rendering such general absolutes as essentially arbitrary. Further, complexity itself is such an amorphous, multi-dimensional continuum, that attempting to pigeon-hole chunks of code at discrete points along a single axis is an intrinsically crude model.

It is better to use them as a somewhat fuzzy, high-level mechanism, which can identify regions of potential interest or concern and from which your own programming- and domain-expertise can take over for a more comprehensive analysis.