A lot of code is automatically generated each day when you use VS.NET. There are also other tools which generate code such as WSDL.exe and tools like CodeSmith
Here, I want to share my views on generated code vs Microsoft's own design guidelines
. The reason for this is the arrival of code analysis tools like FxCop
. FxCop is even integrated into the Developer edition of VS.NET.
Whenever I run code analysis I get annoyed by constantly have to sift through warnings about generated code. If you have a rule that says "Method Names = PascalCase" then how difficult can it be to generate an event which looks like this:
or as many will experience:
And this is only talking about the designer of WinForms or ASP.NET forms. I guess the largest purpetrator of these crimes is WSDL.exe, which created public fields. Since you can't use databinding with public fields, this is significantly less than ideal! With all the code that is generated nowadays, I believe it places an extra responsability of the "code generators" to follow best practices.
Don't get me wrong, I am not against generated code at all, I just don't think it can be treated as a 'second class citizen' when it comes to standards adherence.
A number of tools now mark their code by the [GeneratedCode] attribute which instructs FxCop and the like to ignore the code because it was generated.
Why should generated code have to be excluded from code analysis? Surely, if you are generating code you only have to get it right once?
Obviously, it's not fair to expect all generated code to pass all code analysis, because sometimes there are valid reasons to catch all exceptions (e.g. for logging or in SSIS Components) but these rules can be explicitly excluded in your code using the SuppressMessage attribute:
Furthermore, code generated by a third party is unlikely to pass any custom rules that you have defined yourself, but adhering to the very public Microsoft standards would be a great start.
24 May 2006
» Next Post:
SSIS Custom Logging Task for Event Handlers
« Previous Post:
SSIS Custom Log Provider Samples
Comments are closed for this post.
24 May 2006
You're dead right.
Generated code should be held to the standards of humans and I think it should be easily able to look better.
If we don't go down that road we'll end with a disaster like web pages. In this area incompetent code churns out a lot of content and does a horrible job of it!!
26 May 2006
>Why should generated code have to be
>excluded from code analysis?
generated code still has to be maintained.
Still if a template is wrong in one place it might cause five hundred warnings. Better to have the warning point to the troublesome template just one times (with a great big exclamation mark!) than clutter up your view with 500 redundant issues.