Are there any tools which can report on commented-out .NET code?
Asked Answered
K

6

20

Has anyone come across a tool to report on commented-out code in a .NET app? I'm talking about patterns like:

//var foo = "This is dead";

And

/*
var foo = "This is dead";
*/

This won't be found by tools like ReSharper or FxCop which look for unreferenced code. There are obvious implications around distinguishing commented code from commented text but it doesn't seem like too great a task.

Is there any existing tool out there which can pick this up? Even if it was just reporting of occurrences by file rather than full IDE integration.

Edit 1: I've also logged this as a StyleCop feature request. Seems like a good fit for the tool.

Edit 2: Yes, there's a good reason why I'd like to do this and it relates to code quality. See my comment below.

Kampala answered 11/4, 2011 at 21:1 Comment(7)
Out of curiousity, why does this matter? What's the end-game?Elburt
Good question! I'm looking at it as a code quality metric. Sometimes I'll come across an app (i.e. built by a vendor to their own exacting standards!) with serious amounts of commented dead code and I'd like a way of quantifying it. Similar to reports from tools like NDepend on long methods and too many parameters. While it never compiles and executes, from a maintainability and code elegance perspective, it's not pretty. It's bad code smell at best.Kampala
Gotcha. I think that the best way to do this is to find all comment blocks and commented lines of code, abstract them into their own files, so to speak, and then see which of those look like code to the compiler. I imagine it would have to be able to determine that a line didn't fit and then remove that line as is. The problem is that there may be a segment like this: // if we use 'x = y.Get(z);' here then we have to deal with ... which may or may not meet your guidelines of code smell. Those can't be parsed automatically.Elburt
@drachenstern: If you had comments like the one you suggested, I don't think it would fall in the "code smell" category; it is actually telling you something useful. In particular, it isn't dead code.Prenatal
@Ira, if we take scenarios where throughout the project there are hundreds of lines (maybe even thousands of lines), of commented code, often in blocks of several dozen lines with equivalent but slightly different "live" blocks right next to it (i.e. someone is making backups), what would you classify it as? Certainly that's telling me something and it ain't useful!Kampala
@Troy Hunt: I'm agreeing with you, but objecting to @drachenstern's example. Nobody writes "If we use <hundreds of lines of code> here then we have to deal with..."Prenatal
@Ira no but if they do what I suggested hundreds of times in one class then it's probably no longer useful commenting. I didn't push my example far enough to distinguish that that's what I had in mind. I agree there's a difference between "engrained knowledge" and "wtf is he doing here" so I'm totally on your side.Elburt
P
5

You can get an approximate answer by using a regexp that recognizes comments, that end with ";" or "}".

For a more precise scheme, see this answer: Tool to find commented out VHDL code

Prenatal answered 11/4, 2011 at 21:12 Comment(0)
P
1

I've been done this road for the same reason. I more or less did was Ira Baxter suggested (though I focused on variable_type variable = value and specifically looked for lines that consisted of 0 or more whitespace at beginning followed by // followed by code (and to handle /* */, I wrote a preprocessor that converted it into //'s. I tweaked the reg exp to cut down on false positives and also did a manual inspection just to be safe; fortunately, there were very few cases where the comment was doing pseudo-code like things as drachenstern suggests above; YMMV. I'd love to find a tool that could do this but some false positives from valid but possibly overly detailed pseudo code are going to be really to rule out, especially if they're using literate programming techniques to make the code as "readable" as pseudo code.

(I also did this for VB6 code; on the one hand, the lack of ;'s made it harder to right an "easy" reg exp, on the other hand the code used a lot less classes so it was easier to match on variable types which tend not to be in pseudo code)

Another option I looked at but didn't have available was to look at the revision control logs for lines that were code in version X and then //same line in version Y... this of courses assumes that A) they are using revision control B) you can view it, and C) they didn't write code and comment it out in the same revision. (And gets a little trickier if they use /* */ comments

Penrose answered 24/5, 2011 at 1:31 Comment(0)
R
1

There is another option for this, Sonar. It is actually a Java centric app but there is a plugin that can handle C# code. Currently it can report on:

  1. StyleCop errors
  2. FxCop errors
  3. Gendarme errors
  4. Duplicated code
  5. Commented code
  6. Unit test results
  7. Code coverage (using NCover, OpenCover)

It does take a while for it to scan mostly due to the duplication checks (AFAIK uses text matching rather than C# syntax trees) and if you use the internal default derby database it can fail on large code bases. However it is very useful to for the code-base metrics you gain and it has snapshot features that enable you to see how things have changed and (hopefully) got better over time.

Rodriquez answered 17/2, 2012 at 12:0 Comment(0)
J
1

Since StyleCop is not actively maintained (see https://github.com/StyleCop/StyleCop#considerations), I decided to roll out my own, dead-csharp:

https://github.com/mristin/dead-csharp

Dead-csharp uses heuristics to find code patterns in the comments. The comments starting with /// are intentionally ignored (so that you can write code in structured comments).

Jarred answered 19/6, 2020 at 13:37 Comment(0)
D
0

StyleCop will catch the first pattern. It suggests you use //// as a comment for code so that it will ignore the rule.

Desexualize answered 11/4, 2011 at 21:13 Comment(1)
Yeah, there are a bunch of other syntactically correct commenting patterns I didn't reference. The trick is identifying commented text versus commented code which StyleCop won't do (yet).Kampala
L
0

Seeing as you mentioned NDepends it also can tell you Percentage commented http://www.ndepend.com/Metrics.aspx#PercentageComment. This is defined for application, assemblies, namespaces, types and methods.

Linebacker answered 12/4, 2011 at 4:21 Comment(1)
Yes, but it can't tell you when a comment is not a comment! By this I mean it can't identify code which has simply been excluded from compilation / execution as opposed to comments of an instructional or descriptive nature.Kampala

© 2022 - 2024 — McMap. All rights reserved.