[Mono-dev] Mono enhancements

Sebastien Pouliot sebastien.pouliot at gmail.com
Fri Mar 30 09:24:14 EDT 2007


Hello Vladimir,

On Thu, 2007-03-29 at 09:42 -0400, Vladimir Giszpenc wrote:
> 
> To the Gendarme gurus,
> 
> I don't know if the GC is smart enough to Dispose of resources for us,
> but I will assume that it does not hurt to help it.  There are two
> patterns that Gendarme could look for. 

Side node: We don't lack ideas for Gendarme but time/people do implement
them ;-)
> 
> class Foo
> {
>   Foo()
>   {
>     IDisposable resource = new Resource();
>     throw new System.Exception();
>     resource.Dispose();
>   }
> }
> 
> This should either be remedied with a try finally or with the using
> construct which encapsulates try finally (I vote for the latter as a
> suggested fix).

In many cases it's hard to get a "perfect" rule (e.g. ensuring a Dispose
is reached in every cases) while keeping false negative to a minimum.
Note that in some cases the "public Dispose" can be named something else
(e.g. Close).

Here your test case is simple but, in real life, just about any code
between the ctor and the dispose can throw something (outside your
method). So a "perfect" rule would warn you just about every IDisposable
object you have outside a try/finally.

> Another related rule should be to check if resource.Dispose() is never
> called anywhere in scope.

However it's possible (and much better than nothing ;-) to have a rule
that catch most of them, with very few false positive. In this spirit it
shouldn't be hard to track, at least when the variable is local.
> 
> An unrelated pattern which is not too important to me:
> 
> class Foo
> {
>   Foo()
>   {
>     System.Console.WriteLine(string.Format("hello {9}", "world"));
>   }
> }
> 
> Note that I missed the zero and typed nine. 

It's a good idea.

I'll clean up my notes and (sometime soon ;-) add a new Gendarme Ideas
page to the wiki. I'll add your suggestions there.

> Is it possible to promote Gendarme rules to compiler errors/warnings
> or would that somehow break compatibility with csc?  

Possible ? yes
Likely ? no

> Is it worth it if it means better code is produced?  

Generally you'll compile *much* more often than you'll be use Gendarme.
IMO it's not worth the extra time at every compilation.

> I realize that it won't matter as much once there is a nice Gendarme
> Plugin for MonoDevelop 

amen :)

> but even then, we could ask for severe flaws to be compiler warnings
> at least.

You could make a "csc" script that calls [g]mcs and, if successful, then
call gendarme on the output assembly.

> Many thanks,
> 
> Vlad Giszpenc
> 
> 
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
-- 
Sebastien Pouliot  <sebastien at ximian.com>
Blog: http://pages.infinit.net/ctech/




More information about the Mono-devel-list mailing list