views:

223

answers:

4

I'd like to have the nightly build check for how many NotImplementedExeptions there are in my .NET code so hopefully we can remove them all before releasing. My first thought is that FxCop might be a good tool to do this. Does anyone have a custom FxCop rule for this? How would I go about creating one myself?

+1  A: 

There's tons of resources on the net about how to make your own FxCop rule, but since 'NotImplementedException' is such a unique name, you could probably just do a count of its occurence in the text of the files. Create an ItemGroup for all the .cs files and then use an appropriate DOS command (such as "find /c") and get the count as the output, via the EXEC task.

DarkwingDuck
A: 

There are times when a NotImplementedExeption should be thrown -- one example would be if you've a partial implementation of IBindingList.

There are plenty of resources out there for creating custom fxCop rules. However, a "find in solution" might be a suitable approach (on the assumption that you're not needing this in an automated build)

Rowland Shaw
For a partially implemented Interface, that you don't plan to implement, you should use NotSupportedException. Thanks for the links, I'll check them out. I do want this for an automated build, just to let me know how many are left in code. Tests might be better though.
Lance Fisher
Good comment; I don't think you'd want any NotImplementedExceptions in production ready released code, so a daily build step to count these as asked might very good sense.
peSHIr
@Lance -- good point. Seems I need to top my coffee up :)
Rowland Shaw
+3  A: 

Unit test like this will fail if more than 10 methods create NotImplementedException. On failing it will report all methods that create this exception.

var throwingMethods = codebase.Methods
  .Where(m => m
         .GetInstructions()
         .Exists(i => i.Creates<NotImplementedException>()))
  .ToArray();

if (throwingMethods.Length > 10)
  CollectionAssert.IsEmpty(throwingMethods);

Where codebase is created like this:

var codebase = new Codebase("Assembly1.dll","Assembly2.dll");

Snippet uses Lokad.Quality.dll from the Lokad Shared Libraries.

Rinat Abdullin
That's really cool! I haven't heard much of Lokad before, but the Codebase object looks really cool. Thanks.
Lance Fisher
+2  A: 

I've actually implemented one and shown the code in this answer.

Martinho Fernandes