计算代码度量

有没有可用的工具来计算项目的代码度量(例如代码行数、循环复杂度、耦合度、内聚力) ,并随着时间的推移生成一个显示趋势的图表?

68318 次浏览

NDepend for .net

On my latest project I used SourceMonitor. It's a nice free tool for code metrics analysis.
Here is an excerpt from SourceMonitor official site:

  • Collects metrics in a fast, single pass through source files.
  • Measures metrics for source code written in C++, C, C#, VB.NET, Java, Delphi, Visual Basic (VB6) or HTML.
  • Includes method and function level metrics for C++, C, C#, VB.NET, Java, and Delphi.
  • Saves metrics in checkpoints for comparison during software development projects.
  • Displays and prints metrics in tables and charts.
  • Operates within a standard Windows GUI or inside your scripts using XML command files.
  • Exports metrics to XML or CSV (comma-separated-value) files for further processing with other tools.

For .NET beside NDepend which is simply the best tool, I can recommend vil.

Following tools can perform trend analysis:

If you're in the .NET space, Developer Express' CodeRush provides LOC, Cyclomatic Complexity and the (rather excellent, IMHO) Maintenance Complexity analysis of code in real-time.

(Sorry about the Maintenance Complexity link; it's going to Google's cache. The original seems to be offline ATM).

I was also looking for a code metrics tool/plugin for my IDE but as far as I know there are none (for eclipse that is) that also show a graph of the complexity over a specified time period. However, I did find the eclipse metrics plugin, it can handle:

  • McCabe's Cyclomatic Complexity
  • Efferent Couplings
  • Lack of Cohesion in Methods
  • Lines Of Code in Method
  • Number Of Fields
  • Number Of Levels
  • Number Of Locals In Scope
  • Number Of Parameters
  • Number Of Statements
  • Weighted Methods Per Class

And while using it, I didn't miss the graphing option you are seeking as well.
I think that, if you don't find any plugins/tools that can handle the graphing over time, you should look at the tool that suits you most and offers you all the information you need; even if the given information is only for the current build of your project.

As a side note, the eclipse metrics plugin allows you to export the data to an external file (link goes to an example), so if you use a source control tool, and you should!, you can always export the data for the specific build and store the file along with the source code, that way you still have a (basic) way to go back in time and check the differences.

For Python, pylint can provide some code quality metrics.

On the PHP front, I believe for example phpUnderControl includes metrics through phpUnit (if I am not mistaken).

Keep in mind that metrics are often flawed. For example, a coder who's working on trivial problems will produce more code and there for look better on your graphs, than a coder who's cracking the complex issues.

There's also a code metrics plugin for reflector, in case you are using .NET.

If you're after some trend analysis, does it really mean anything to measure beyond SLOC?

Even if you just doing a grep for trailing semi-colons and counting the number of lines returned, what you are after is consistency in the SLOC measurement technique. In this way today's measurement can be compared with last month's measurement in a meaningful way.

I can't really see what would a trend of McCabe Cyclometric Complexity give? I think that CC should be used more for a snapshot of quality to provide feedback to the developers.

Edit: Ooh. Just thought of a couple of other measurements that might be useful. Comments as a percentage of SLOC and test coverage. Neither of which you want to let slip. Coming back to retrofit either of these is never as god as doing them "in the heat of the moment!"

HTH.

cheers,

Rob

Code Analyzer is simple tool which generates this kind of metrics.

alt text
(source: teel.ws)

keep in mind, What you measure is what you get. loc says nothing about productivity or efficency.

rate a programmer by lines of code and you will get.. lines of code. the same argument goes for other metrics.

otoh.. http://www.crap4j.org/ is a very conservative and useful metric. it sets complexity in relation with coverage.

Concerning the tool NDepend it comes with 82 different code metric, from Number of Lines of Code, to Method Rank (popularity), Cyclomatic Complexity, Lack of Cohesion of Methods, Percentage Coverage (extracted from NCover or VSTS), Depth of Inheritance...

With its rule system, NDepend can also find issues and estimates technical debt which is an interesting code metric (amount of dev-effort to fix problems vs. amount of dev-time spoiled per year to let problems unfixed).

All these metrics are detailled here.

Scitools' Understand does have the capability to generate a lot of code metrics for you. I don't have a lot of experience with the code metrics features, but the static analysis features in general were nice and the price was very reasonable. The support was excellent.

Atlassian FishEye is another excellent tool for the job. It integrates with your source control system (currently supports CVS, SVN and Perforce), and analyzes all your files that way. The analysis is rather basic though, and the product itself is commercial (but very reasonably priced, IMO).

You can also get an add-on for it called Crucible that facilitates peer code reviews.

NDepend, I am using it and its best for this purpose. Check this : http://www.codeproject.com/KB/dotnet/NDepend.aspx

Sonar is definitively a tool that you must consider, especially for Java projects. However it will also handle PHP or C/C++, Flex and Cobol code.

Here is a screenshot that show some metrics on a project:

alt text http://sonar.codehaus.org/wp-content/uploads/2009/05/squid-metrics.png

Note that you can try the tool by using their demo site at http://nemo.sonarsource.org

For Visual Studio .NET (at least C# and VB.NET) I find the free StudioTools to be extremely useful for metrics. It also adds a number of features found in commercial tools such as ReSharper.

Project Code Meter gives a differential development history report (in Excel format) which shows your coding progress metrics in SLOC, time and productivity percentage (it's time estimation is based on cyclomatic complexity and other metrics). Then in Excel you can easily produce the graph you want.

see this article which describes it step by step: http://www.projectcodemeter.com/cost_estimation/help/FN_monsizing.htm

I would recommend Code Metrics Viewer Exention for visual studio.

It is very easy to analyze solution at once, also do comparison if you made progress ;-)

Read more here about the features

For Java you can try our tool, QualityGate that computes more than 60 source code metrics, tracks all changes through time and also provides an overall rating for the maintainability of the source code.