The way I ensure complete code coverage (got this idea from Gordon Letwin many many years ago) is to place a trap in my code at the start of every new section of code, in every if & else, after a while & for, and anywhere else appropiate.
Then when the trap hits, it drops me in the debugger and I single step through the code until I get to code I've already walked through. This way I end up single stepping through every line of code I write. And by doing a grep for trap I can find code that has never been executed.
Here's the code to make this work in C#. To call it you just call Trap.trap();
using System;
namespace WindwardBear.utils
{
/// <summary>
/// This code will force a break in to the debugger for debug builds. For release builds they do nothing.
/// </summary>
public sealed class Trap
{
// can't create this
private Trap()
{
}
/// <summary>Will break in to the debugger (debug builds only).</summary>
public static void trap()
{
#if DEBUG
System.Diagnostics.Debugger.Break();
#endif
}
/// <summary>Will break in to the debugger if breakOn is true (debug builds only).</summary>
/// <param name="breakOn">Will break if this boolean value is true.</param>
public static void trap(Boolean breakOn)
{
#if DEBUG
if (breakOn)
System.Diagnostics.Debugger.Break();
#endif
}
}
}


what about using a code coverage tool? NCover, PartCover, and any of the VS Team editions all support code coverage of .NET code.
Posted by: Stephen | 11/12/2009 at 10:19 PM
Those are good tools too, but they serve a different purpose. With the TRAP I am dropped in to the debugger at that point in the code to step through it. And it gives me an easy way to walk each part as I first hit it, which can be days apart throughout a module.
Posted by: David Thielen | 11/12/2009 at 10:24 PM