Useful Script: Export All Metric Values, Not Just Violations

TLDR:

CodeCheck will currently tell you the violations of the metrics, but not all the details. This script exports a CSV that lets you know all the values, whether they pass the standard or not. We do this for the Hersteller Initiative Software Metrics, but you can easily tweak it to do all or any you choose. If you need help, hit us up at [email protected].

Details:

Metrics teach you a lot about your project’s code. Understand checks your code with hundreds of published metrics, including the Hersteller Initiative Software (HIS) Metrics. The initiative, which originated in the automobile industry, sets standards for common metrics that will help your code have clarity, simplicity, and higher quality. Interesting metrics like the number of paths, call tree depths and the cost of maintaining a function.

CodeCheck simplifies a project of any size. But, how do you compare functions that don’t violate the metrics? Maybe you want to track or visualize this data over time. This script exports all of the info you need, using the Understand Python API. We also have C, Java, and Perl APIs providing the same power. Learn about usage, customization, and download it here.

How does it work? The script accesses your project’s .und folder, which gives it each file. Each file has a collection of all the references and entities. The main function loops through all the function entities and runs the 12 checks on each entity. The entity gives us the kind, contents, references, and more. With this info, a check analyzes comment density, call complexity, recursion, etc.

How was it made? The Understand API gives us information about each entity, in this case, each function. An entity gives all references to itself, such as when it’s used, passed, or defined. The entity and reference API informs us of each occurrence of anything we want to look for, such as when a member method is defined in C++. The other tool in our toolbelt is the lexer. With it, we step through all the symbols and we know about the type of each symbol. Each of the 12 HIS checks uses these tools and report how well a function follows the metric. Add new metrics yourself by using the API.

The output is a CSV file with data for each function. Each line contains the parent, file name, line number, function name, and the 12 HIS metrics. Here’s an example of scanning the Linux kernel.