views:

1219

answers:

6

I'm working with C# and I was hoping to find some tools akin to those I'm used to in Ruby and Ruby on Rails for detecting code smells. I'm referring to things like Roodi, Flay, Flog, Reek, Rcov, and Saikuro. It would be nice if the tool(s) integrated with Visual Studio 2008.

I have ReSharper and it's nice for alerting me when I'm not following my own naming conventions, if I could use an object initializer, etc., but it doesn't tell me things like:

  • this code looks a lot like that code
  • [Duplication] ApplicationController#authenticate_admin calls session[user] more than once
  • [Nested Iterators] ApplicationController#remove_from_all_games has nested iterators
  • [Feature Envy] GameController#valid_set? refers to cards more than self
  • cyclomatic complexity

A tool akin to Railroad ("RailRoad is a class diagrams generator for Ruby on Rails applications") would also be nice.

I tried search StackOverflow for a question addressing this, but didn't have any luck. Sorry if this is a duplicate. Maybe the functionality I'm looking for is actually built into VS 2008 or ReSharper, and I'm just missing it.

Edit: I'm not sure whether ReSharper or Clone Detective added it, or perhaps it's VS 2008, but I was able to right-click on my Solution in Solution Explorer and choose Calculate Code Metrics. The metrics show up in a Code Metrics Results window and include columns Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Class Coupling, and Lines of Code. This is certainly a start to getting all the data I want!

+12  A: 

What you're searching for is probably a static analysis tool. Here is a Stackoverflow post that discusses this.

FxCop is one for .Net. I've never used it however.

Juri
I have used FxCop extensively and highly recommend it. FxCop was an internal MS tool which was used to make sure that all .NET Framework code follows certain design/coding standards. Later on, they made it available to public. You may also want to look at 'Framework Design Guidelines' book by Krzysztof Cwalina.
SolutionYogi
Also note Gendarme (http://www.mono-project.com/Gendarme which is the open source, mono equivalent
Nathan Koop
FxCop is now a part of VisualStudio as Static Code Analysis. Highly recommended. You can also add your own rules, if you seem to miss any.
peSHIr
+3  A: 

Not sure about Resharper, but I know CodeRush identifies Code Smells

Justin
+8  A: 

There are two tools that, based upon my experience, will really help you here:

  1. NDepend - Commercial tool (worth the $$$) that provides all sorts of quality metrics such as cyclomatic complexity, coupling, cohesion, nesting, etc. It also has it's own query language, CQL, that you can use to apply custom conditions and rules. Finally, it provides some awesome visual analysis of dependencies, code coverage, etc.

  2. Simian - Tool for detecting and highlighting similarities in source code. It's actually pretty language agnostic, handling C#, Java, Ruby, and XML, amongst other source types.

Both of these tools can be invoked from NAnt (not sure about MSBuild) and can be easily integrated into CruiseControl.net.

Thomas Beck
Simian link - http://www.redhillconsulting.com.au/products/simian/
Frank Schwieterman
+1 for NDepend - be sure to subscribe to Patrick Smacchia's blog at codebetter.com too
AakashM
+3  A: 

Have you seen Gendarme? It feels like it does more the FxCop and picks up a few of the things you've pointed out though I don't think its integrated.

Ian
+1  A: 

Semantic Designs (my company) offers code metrics for C#, including cyclomatic and Halstead measures on files, classess and methods. See http://www.semdesigns.com/Products/Metrics/CSharpMetrics.html. It also offers CloneDR, a tool for finding clones in a variety of langauages, including C#. A distinguishing property of CloneDR is that it finds clones that are similar, but not identical, shows the clone locations and text, and shows precisely how the clones are different by indicating how the differing parts ("parameters") are instantiated in each individual clone. See http://www.semdesigns.com/Products/Clone/index.html

Ira Baxter
+1  A: 

Concerning the tool NDepend and its Code Query language (CQL). CQL is dedicated to write code quality rules that can be verified live in Visual Studio, or that can be verified during build process and reported in a HTML report. Here are a few samples of CQL rules (designed to be highly customizable):

Code refactored recently should be 100% covered by test:

WARN IF Count > 0 IN SELECT METHODS WHERE CodeWasChanged AND PercentageCoverage < 100

Complex methods should be commented:

WARN IF Count > 0 IN SELECT METHODS WHERE CyclomaticComplexity > 15 AND PercentageComment < 10

I don’t want that my User Interface layer to depend directly on the DataBase layer:

WARN IF Count > 0 IN SELECT NAMESPACES WHERE IsDirectlyUsing "DataLayer" AND NameIs "UILayer"

Static fields should not be named m_XXX (Custom naming conventions):

WARN IF Count > 0 IN SELECT FIELDS WHERE NameLike "^m_" AND IsStatic

Methods out of MyAssembly and MyAssembly2 should not have more than 30 lines of code:

WARN IF Count > 0 IN SELECT METHODS OUT OF ASSEMBLIES "MyAssembly1", "MyAssembly2" WHERE NbLinesOfCode > 30

Public methods should not be removed to avoid API breaking changes:

WARN IF Count > 0 IN SELECT METHODS WHERE IsPublic AND IsInOlderBuild AND WasRemoved

Types tagged with the attribute MyNamespace.FullCoveredAttribute must be thoroughly covered by tests:

WARN IF Count > 0 IN SELECT TYPES WHERE HasAttribute "MyNamespace.FullCoveredAttribute" AND PercentageCoverage < 100

Patrick Smacchia - NDepend dev