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?
-
Anyone know if NDepend would be a good tool for this? http://www.ndepend.com/ – Lance Fisher Jan 06 '09 at 21:50
4 Answers
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.

- 24,862
- 16
- 85
- 145

- 23,036
- 8
- 57
- 80
-
That's really cool! I haven't heard much of Lokad before, but the Codebase object looks really cool. Thanks. – Lance Fisher Jan 06 '09 at 20:46
I've actually implemented one and shown the code in this answer.

- 1
- 1

- 228,013
- 71
- 433
- 510
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.

- 2,686
- 24
- 29
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)

- 37,700
- 14
- 97
- 166
-
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 Jan 06 '09 at 09:22
-
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 Jan 06 '09 at 09:28