A source code metrics calculator.

It all started as an attempt to answer the question: "How complex is this software?".
The project had many files of source code spread over many directories and we wanted to know how many lines of code it all added up to. Partly out of curiosity but also because if we got a breakdown of the result we would be able to compare the complexity of the different modules. And that would help us make informed project decisions such as how to best allocate our resources. At least that was the idea.

Metrics is designed to traverse a directory and all its subdirectories and count the number of lines of all files of the specified type or list of types. The result is sent to the console window in a human readable format and is also written to the file metrics.txt in a format designed to be easy to import into a spreadsheet such as Excel.

The first version of Metrics taught us that the number of raw lines of code is not a good way to measure complexity. A well documented source file contains about 50% comments compared to a quick hack that usually contains no comments at all. To get a valid measurement of the complexity the comments must clearly be ignored. Another problem with counting raw lines of code is that different programmers have very different coding styles, ranging from terse and compact to spacious and verbose. Not only does the ratio of blank lines matter, but different indentation styles (how you put the curly brackets) also have a significant impact on the number of raw lines. To get around these pitfalls the current version of Metrics knows the syntax used for comments in C, C++ and Java and counts the number of lines with statements and the number of lines with comments.

This produces some interesting results. Not only can you compare the complexity of different modules. It is also possible to calculate the "quality" of the source code as the comments to statements ratio. This can be useful for identifying modules that are less well commented and needs be brought up to standard. A less useful but still interesting result is that you can measure the density of the code as the ratio of statements plus comments to the total number of lines. In many cases it is possible to identify who wrote the code by simply looking at the density.

The quality metric has proved so useful that we added a function that makes Metrics automatically compile a list of the files that are in greatest need of more comments. The list will not be accurate if the files contain a lot of old code that has been left as comments. But modern versioning systems have made the practice of commenting code out redundant. However, this will not stop programmers from knowingly use this trick to boast the quality rating of the code if it's used to terrorise them.

Metrics is executed from the command line. It should work on most versions of Microsoft Windows. Start it without arguments or with -? and it will tell you the correct syntax for using it.

Metrics is free to use and to distribute. Click here to download a copy (57 kB).