.comment-link {margin-left:.6em;}
Name:
Location: San Diego, California, United States

Sunday, March 27, 2005

Alternative Dispose()

Visual Studio writes Dispose() methods for me that look like this:

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


I don't mind - much - it's not like I have to look at it much. But it's a whole lot of indentation to do not very much. I figured the one really important thing it's doing there is calling base.Dispose(), and the conditionals just add a lot of structure to protect the inner clause without interfering with that final line. So I refactored it into this:

protected override void Dispose(bool disposing)
{
try
{
if (!disposing) return;
if (components == null) return;
components.Dispose();
}
finally {base.Dispose(disposing);}
}


I'm pretty sure the behavior is identical, and it's just a whole lot easier on my eyes. The return/finally interactions are a little scary (finally clause code happens after return, which isn't true of normally structured code) - but I'll (grudgingly) trade that scariness for the reduction in apparent complexity.

0 Comments:

Post a Comment

<< Home