3 ms·
There are metrics for complexity that nobody who writes these articles ever mentions: Halstead complexity [1], LCOM4 [2], cyclomatic complexity [3] (and others)
by jackcviers3 3y ago
There are metrics for complexity that nobody who writes these articles ever mentions: Halstead complexity [1], LCOM4 [2], cyclomatic complexity [3] (and others) that directly measure how complex a given piece of code is.
Halstead complexity measures the complexity of the "vocabulary" of code and the code "size". It uses the number of unique terms (arguments, variables), the number of unique operations (functions/methods/symbolic operators), and the total number of terms and operators to calculate difficulty and effort to produce. Lower is better. Obviously, more things and aliases and combinations of things are harder to understand than fewer things.
LCOM4 measures how many different responsibilities a module of code has. It measures whether or not two things in a module belong together by treating the code as a graph of nodes and counting the unique connected components in the graph. Greater than 2 means there is too much going on in the module, and it should be split to enhance understanding and reduce churn during maintenance. Obviously, if you are reading parts of the code that end up being irrelevant to the task at hand, you are going to be slower to accomplish whatever you are trying to accomplish.
Cyclomatic complexity measures how many execution paths there are in a given piece of code. Obviously, the fewer the better.
There are others - the counting complexity of the number of possible inhabitants of a given interface is a favorite of mine as well (is your interface a product, or a sum? Do you need strings or will an enum work? Do you need Long or can you get away with something smaller like Short or Int? Sum interfaces are smaller than product interfaces, and smaller ranged sum types - Short/int vs. Long - are better than larger ones because they reduce the number of possible satisfying implementations of the interface).
But the point is that all good simplicity metrics are static analysis metrics that are quantitative and not qualitative. Most are automated checks that can be performed given an abstract syntax tree of a program.
1. https://en.wikipedia.org/wiki/Halstead_complexity_measures https://en.wikipedia.org/wiki/Halstead_complexity_measures
2. https://www.researchgate.net/publication/238729882_Measuring_coupling_and_cohesion_in_object-oriented_systems https://www.researchgate.net/publication/238729882_Measuring...
3. https://en.wikipedia.org/wiki/Cyclomatic_complexity https://en.wikipedia.org/wiki/Cyclomatic_complexity