.NET Framework - Lint for C# ?

Asked By Davej on 19-May-12 12:08 PM
http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis#.NET

Is anyone here using a lint-like program for C# ? I just found a bug
that would be easy to search for: I'd like to see a warning for an if
that follows another if with no space between them if the second if is
followed by one or more else if's.

Would Lint or these other style tools have warned about this?

if (...)
{
...
}
if (...)
{
...
}
else if (...)
{
...
}
else if (...)
{
...
}
else if (...)
{
...
}




Peter Duniho replied to Davej on 19-May-12 12:24 PM
I am not aware of any tools that would warn about that.  I do not even recall
using any version of lint that would warn about that (but it is been quite
awhile, so that is not really saying much), and probably would not use that
feature if I did.  There is too much code running around with that pattern
where the code is correct (i.e. someone just did not bother to put any blank
lines in their code) and dealing with all the false-positives would be IMHO
more trouble than it is worth.

Of course, YMMV.  I adhere to the philosophy that the code's not done until
you have stepped through every line of it at least once, and doing so would
immediately reveal an error in code like that.  Additionally, a long
sequence of if/else if/else usually points to a situation that is better
handled either with a switch statement or polymorphism.  Others without
those philosophies might find such problems come up in their own code more
often and would find a lint warning more valuable.

All that said, if you are thinking about writing such a tool, the new
make it somewhat easier to implement such a thing.

Pete
Arne_Vajhøj replied to Davej on 19-May-12 09:30 PM
StyleCop does give warning/error on this.

SA1513: ClosingCurlyBracketMustBeFollowedByBlankLine

Cause
A closing curly bracket within a C# element, statement, or expression is
not followed by a blank line.

Rule Description
To improve the readability of the code, StyleCop requires blank lines in
certain situations, and prohibits blank lines in other situations. This
results in a consistent visual pattern across the code, which can
improve recognition and readability of unfamiliar code.

A violation of this rule occurs when a closing curly bracket is not
followed by a blank line.

StyleCop is available here:
http://stylecop.codeplex.com/

You may want to disable many of its rules as it is pretty strict
by default.

Arne