Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lint for C# ?

895 views
Skip to first unread message

Davej

unread,
May 19, 2012, 12:08:13 PM5/19/12
to
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

unread,
May 19, 2012, 12:24:02 PM5/19/12
to
On Sat, 19 May 2012 09:08:13 -0700 (PDT), Davej wrote:

> 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?

I'm not aware of any tools that would warn about that. I don't even recall
using any version of lint that would warn about that (but it's been quite
awhile, so that's not really saying much), and probably wouldn't use that
feature if I did. There's too much code running around with that pattern
where the code is correct (i.e. someone just didn't bother to put any blank
lines in their code) and dealing with all the false-positives would be IMHO
more trouble than it's worth.

Of course, YMMV. I adhere to the philosophy that the code's not done until
you've 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're thinking about writing such a tool, the new
"compiler-as-a-service" features in C# 5 (coming out with .NET 4.5) may
make it somewhat easier to implement such a thing.

Pete

Arne Vajhøj

unread,
May 19, 2012, 9:30:34 PM5/19/12
to
StyleCop does give warning/error on this.

<quote>
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.
</quote>

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
0 new messages