Hack 66. Generate Statistics on Your C# Code
You'll write a lot of code in
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 devMetrics
Before 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:
- From Visual Studio
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.
- From the command line
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.
7.9.2. Study the Report
After
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:
- Files
The number of C# files in the project.
- Types
The number of classes, interfaces, and structures in the project.
- Members
The number of
fields, properties,
events, methods, and constructors.
- Lines
The total number of lines of code in the project, including
blank lines and comments.
- Statements
The total number of statements in the project.
- Average and maximum code complexity
devMetrics uses the cyclomatic code
complexity algorithm as defined by the
Software Engineering Institute (see
http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html).
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.
As Figure 7-37 shows, the name of each project in
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.
From the Type Declaration list, you can expand the node of each type
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
|
No comments:
Post a Comment