Hack 66. Generate Statistics on Your C# Code![]() Visual Studio. Now, learn about a free tool that lets you view assorted statistics and information about your C# Visual Studio Projects.Have you ever wondered how many files, classes, or lines of code exist in one or more of your Visual Studio Projects? While you can certainly count these metrics manually, there is a free Visual Studio add-in that will provide these statistics (and more!) in a flash. Once installed, this add-in, called devMetrics, adds an Analyze menu option to the Visual Studio Tools menu. When selected, the projects in the currently loaded Visual Studio Solution are examined, and their statistics are presented in an easy-to-read report. (Unfortunately, devMetrics generates reports for only C# Projects.)The devMetrics reports provide useful information at the click of a mouse. The report's statistics include information that can be used to measure the progress of a code project, show what classes are undercommented, and point out what classes and methods are exceedingly complex and in need of refactoring. 7.9.1. Download and Install devMetricsBefore you can generate statistical reports on your code, you'll first need to download and install the devMetrics add-in, which is a free product from Anticipating Minds (http://www.anticipatingminds.com) and is available at http://www.anticipatingminds.com/Content/Products/devMetrics/devMetrics.aspx. Download the devMetrics installer to your computer, and then double-click the file to begin the installation process.Once installed, devMetrics can be run in two ways: To view the statistics for a C# Project through Visual Studio, open Visual Studio and load the solution or project to analyze. Then, from the Tools menu, choose the devMetrics submenu, and select the Analyze menu option, as shown in Figure 7-36. Once the project(s) have been analyzed, the statistical report will be automatically displayed in Visual Studio. To run the devMetrics analyzer from the command line, navigate to the Program Files\Anticipating Minds\devMetrics directory and run the devMetrics.exe executable, specifying the directory that contains the C# Projects that you want to analyze, such as: devMetrics "C:\My Projects\Some Visual Studio Project Folder". (devMetrics will recursively iterate through the directory's subdirectories and include reports on all C# Projects found.) After completing, devMetrics will automatically launch Internet Explorer, displaying the report. Figure 7-36. devMetrics Analyze menu option![]() 7.9.2. Study the ReportAfter analyzing either a Visual Studio Project or Solution, devMetrics displays the resulting project report (see Figure 7-37). This report lists each C Project analyzed, along with the following metrics: The number of C# files in the project. The number of classes, interfaces, and structures in the project. The number of fields, properties, events, methods, and constructors. The total number of lines of code in the project, including blank lines and comments. The total number of statements in the project. devMetrics uses the cyclomatic code complexity algorithm as defined by the Software Engineering Institute (see http://www.sei.cmu.edu/str/descriptions/cyclomatic_bodyl). The complexity is a measure of linearly independent paths through a program module and is calculated for each member that contains statements. The code complexity measurement results in a numerical evaluation, with lower values meaning less complex code. The maximum code complexity is the highest complexity value for all members in the project; the average code complexity is the average complexity of all members. In addition to showing these metrics for each project, the project report also displays a summary row. Figure 7-37. devMetrics project report![]() the report is displayed as a hyperlink. Clicking on the link will expand the project node and show you a list of the types in that particular project. For each type in the project, the number of statements and members are shown along with the cyclomatic complexity. Figure 7-38 shows the report with the project node expanded. Figure 7-38. devMetrics member report![]() and view a list of the type members, including the number of statements and complexity of each member.The devMetrics add-in can provide quick summary data about your C# code on a project-by-project level as well as a member-by-member level. The cyclomatic code complexity, statements/member, and members/class metrics can assess what portions of your code are unduly verbose and what portions would benefit from being refactored. Scott Mitchell |