Thursday, November 23, 2006

Roland Weigelt's GhostDoc - Just use it!

I've been trying this VS2005 add-in for the last couple of days, and it's safe to say that it ROCKS!

From the site:
GhostDoc is a free add-in for Visual Studio that automatically generates XML
documentation comments. Either by using existing documentation inherited
from base classes or implemented interfaces, or by deducing comments from
name and type of e.g. methods, properties or parameters.
In use, it's great for going back over hurriedly written code to write the in-code comments we're all SUPPOSED to write as we go - took me about a minute to completely document a complex ASP.Net server control, and I had to adjust the comments of only 3 methods.

Get Roland Weigelt's GhostDoc - you know it makes sense!

Wednesday, November 22, 2006

Quick Tip: Checking for a reference type null in C#

How do you test for a null reference type in C#?

You'd think that it'd be easy to check for a null, just use some code like this:

MyType thing = null;
...
if ( thing == null)
{
// do something
}

But what if you need to test for a null within an overloaded == operator?!
You can't use the simple code above, as this would cause a stack overflow.

The trick is in casting the entity to a different type and so that the compiler uses the == for that alternate type. I wrap this test into a static IsNull() method on the class.

public static bool IsNull(MyType thing)
{
object testThing = thing as object;

// uses the == operator from object, NOT MyType
return (testThing == null);
}

So now I can safely test for equality, including correctly testing for nulls like this

public static bool operator == (MyType thing1, MyType thing2)
{
// two nulls => equal
if (IsNull(thing1) && IsNull(thing2))
return true;

// one null => not equal
if (IsNull(thing1) || IsNull(thing2))
return false;

// do the REAL equality test here
...
}

Simple when you know how! And absolutely essential when you're implementing IEquatable.

Friday, November 17, 2006

Sometimes the simplest things are most effective

I've been playing with Joel Fjordén's Code Style Enforcer
recently, and reckon it's one of the smallest, simplest and most useful VS IDE plugins around.

It's basically a DXCore plugin that redlines (draws a hand-drawn red line underneath) any c# code that doesn't meet the basic style guidelines from the iDesign C# coding standard.

I have to admit that finding the right properties page for the plugin to let me customise the rules too a while (find it by selecting DevExpress -> Options -> Code Standard -> Name Rules from the VS menu), but now it's great - I get instant feedback when I'm not meeting the basic coding standards.

Find it here...
http://joel.fjorden.se/static.php?page=CodeStyleEnforcer
And go use it!