[Mono-dev] Mono enhancements

2007-03-30 Thread Vladimir Giszpenc

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.  

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).
Another related rule should be to check if resource.Dispose() is never called 
anywhere in scope.

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.  

Is it possible to promote Gendarme rules to compiler errors/warnings or would 
that somehow break compatibility with csc?  Is it worth it if it means better 
code is produced?  I realize that it won't matter as much once there is a nice 
Gendarme Plugin for MonoDevelop but even then, we could ask for severe flaws to 
be compiler warnings at least.

Many thanks,

Vlad Giszpenc
___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


Re: [Mono-dev] Mono enhancements

2007-03-30 Thread Sebastien Pouliot
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@lists.ximian.com
 http://lists.ximian.com/mailman/listinfo/mono-devel-list
-- 
Sebastien Pouliot  [EMAIL PROTECTED]
Blog: http://pages.infinit.net/ctech/

___
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list