Skip Navigation LinksHome > View Post

Generated Code and Code Analysis Tools

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 etc.

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 and NDepend. FxCop is even integrated into the Developer edition of VS.NET.

NDepend screenshot

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:

    FieldName_EventHandler()

rather than

    fieldName_EventHandler()

or as many will experience:

    _memberVariable_EventHandler()

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.

[GeneratedCode] attribute

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.

But why?

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:

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]

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.

Tags: Other

 
Bruusi Post By Bruusi
4:42 AM
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.

Posted by Mike Gale @ 24 May 2006 7:08 PM
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!!

Posted by lb @ 26 May 2006 7:26 AM
>Why should generated code have to be
>excluded from code analysis?

top question!

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.

© 2005 - 2014 Josh Twist - All Rights Reserved.