Tuesday, December 12, 2006

Writing FxCop Rules Is Really Hard Without Any Documentation

So here at work I'm currently doing lots of .NET stuff. Recently I was tasked with writing some custom rules for FxCop so we can enforce our coding standards before check-ins and builds. Unfortunately, there seems to be a dearth of FxCop Custom rules, and an even greater dearth of information about how to write them. Apparently a developer named Raymond Lewallen came right out and asked "where are the custom FxCop rules?" in Feb 05, and it seems there's still not much of an answer. If you go to the GotDotNet UserSamples area and look for FxCop samples, the results are pretty grim; fewer than 20 results, and almost nothing for the newest versions.

That wouldn't really bother me -- experience has taught me to take online samples with a grain of salt, anyway -- except for that fact that there also doesn't seem to be thorough API documentation or an SDK, and according to the FxCop FAQ on MSDN, this will continue to be the case for the foreseeable future.

So I continue to slug it out on my own. Things take a little longer for me than it probably does for most programmers, but I've managed to get a solution put together and a rule or two (all very basic and borrowing heavily from Microsoft samples) written that are working against a really simple "Hello World" console assembly. I figured out the whole where-do-I-put-my-xml-file-and-what-do-I-call-it issue through some painful experimentation and forum scouring, but when I write something like...

LocalList locals = method.Instructions[0].Value as LocalList;
InstructionList instructions = method.Instructions;


...which I got from one of the sample rules, I really have no idea what I'm doing. I can make certain inferences based on usage, but I don't know what a "local" is, or what "instructions" are, really. Is a local like what's in the "Locals" window in VS? Are instructions lines of code, or literally instructions, not to include declarations? I suppose the best way to find out, lacking an SDK (or whatever), would be to download the source code, but I don't think it's publicly available right now, or at least it doesn't appear to be. Hm. Back to trial-and-error, I guess. Maybe this is why I'm yet to work at a shop that uses any custom rules; all anyone seems to do is turn off a few of the default checks and run it like that.

No comments: