Been coding sometime now, gradually turning into one of the grey beards. Guess this is inevitable if you decide to remain a Unix programmer long enough.
Anyhow, not so frequent these days that something genuinely fundamental to our craft, really makes me stop and take notice. Had one of those moments this weekend and thought it might be worth sharing briefly.
Question: How many of you know for certain things like...
Total lines of code in your current application
Average lines of code in each source file in your application
Breakdowns (lines of code, lines of comments, lines of mixed, lines left blank)
Length of each function / method within source files
How difficult it is to understand the codeÂ
The mental effort needed to code a programÂ
The algorithmic complexity of code units.Â
Using Javascript heavily for client and server side coding, this is a language that really benefits from using code quality tooling and best practices.Â
One premise of trying to measure software complexity is that bug-laden code usually manifests itself in complex parts of the codebase. This stands to reason - complex components are also hard to understand, hard to test, and hard to modify. If we know where the perceived complexity is in our programs (or others programs for unfamiliar codebases) we likely get some ideas where potential bugs might exists. Â If we reduce the program complexity, the programs will improve. In fact, practical experience has shown this to be true.Â
There are two main concepts widely used for measuring code complexity today:Â
Halstead‟s theory of software metrics has its roots in evaluating complexity of low level programs like assembly language. He proposed a short set of measurable operator and operand quantities and then used them in equations to predict program features.Â
McCabe‟s model for software metrics determines the complexity of a program unit or module by measuring the amount of decision logic. The McCabe number is a predictive measure of reliability. In itself, the complexity value does not identify problems or errors but there is strong correlation between and code errors in studies conducted.
 Both Halstead and McCabe models are applied to real projects and are supported by commercial tools. The McCabe number is often also referred to as “conditional complexity” or “cyclomatic complexity metric.”
Well, I pretty much thought the same, but then installed 3 different modules for NodeJS and integrated with the DFM Build process - Â that allowed me to measure the Halstead and McCabe complexity of my code, and provide some beautiful HTML reports in the process. I set thresholds (numbers, without getting too hung up on their meanings), Green, Yellow, and Red system.
Were astonishing. With respect code, I knew where the “complexity” lurked, I had some inklings which parts of the code base should be revisited with further iterations to refactor stuff, which functions were too many LOC, etc etc. And the correlation between what I knew and what this report was able to spew out in a fraction of a second was really strong. It provides a score for overall how maintainable the code is, likelihood of bugs per function of file etc etc.
To my knowledge, writing Functional, Integration and Unit tests are something we pretty much do now by rote. We’ve had beaten into us to do so and likely know we’ve skipped something important when we don’t do it. Test coverage tools too get quite a bit of limelight. But code complexity analysis tools are not so mainstream. From this experience, the code complexity tools should play an active role in signing off a deliverable as done. Think they might really contribute towards better, cleaner, maintainable software.