Maybe we can add a check for internal/private class to not apply the
rule if there is never a call to ToString (or parameter of
string.Format, Console.WriteLine).
The same way, if a call to ToString is detected, we can increase the
severity.
> Maybe we can add a check for internal/private class to not apply the > rule if there is never a call to ToString (or parameter of > string.Format, Console.WriteLine). > The same way, if a call to ToString is detected, we can increase the > severity.
I like the idea of a rule that promotes classes that "play well" in the debugger.
However, many of my own classes use the attribute [DebuggerDisplay] to control the string that is displayed by the debugger.
I think the rule has potential since I had a similar idea a while
ago ;-) What stopped me earlier was a feeling that it would bring a
lot of defects, quite a few of them not very important. For example:
a) in Gendarme most rules do not have any state so the best "ToString"
is still, IMO, the type name. That could be fixed easily by skipping
types without any fields (since most interesting properties will have
a, manual or automatic, backing field).
b) Abstract types do not really need a ToString since it won't be used
(assuming all inheritors provide their own). Now this does not play
well with (a) because the abstract type could have fields where the
concrete have none (not a very big issue and a parent-check could be
done too);
c) Bevan's comment about [DebuggerDisplay] is another case (but again
not perfect considering non-VS.NET users ;-)
I don't have more ideas right now but I'd be happy if those suggestion
(more feedback welcome) could be added to the rule. I think that would
make it something I like to fix for Gendarme.
Sebastien
p.s. one last thing let's move the rule from .Design
to .Maintainability
On Nov 18, 8:27 am, Lex Li <lextu...@gmail.com> wrote:
On Nov 18, 3:20 pm, Antoine V <ant.v...@gmail.com> wrote:
> Hello,
> Maybe we can add a check for internal/private class to not apply the
> rule if there is never a call to ToString (or parameter of
> string.Format, Console.WriteLine).
> The same way, if a call to ToString is detected, we can increase the
> severity.
That's a bit complicated and would miss some "honest" cases ;-)
e.g. list.Add (obj); foreach (object o in list) Console.WriteLine (o);
ToString are also very useful when debugging (in or out a debugger)
where the extra debugging code won't be present at analysis time.