Showing posts with label Style Cop. Show all posts
Showing posts with label Style Cop. Show all posts

Tuesday, May 5, 2009

System.CodeDom.GeneratedCodeAttribute for suppressing the generated code warnings

For one of the utilities i am writing i am using the resx file to store various blocks of code and generating a csharp class file based on a xml file. Because of dynamic generation of blocks i am not able to indent the code properly and hence when i compile this code i obviously get style cop warnings in my project.
When i investigated further i saw that style cop warnings are not displayed in the designer files generated by Visual Studio for ASP.NET and WinForms application. One of the differences i found in the files generated by Visual Studio and files generated by my utility was that visual studio puts a tag <auto-generated> in the file header while i put a tag called <autogenerated/> in mine.
Any ways changing this didn't affect it at all and the warnings were still generated in the compilation log.
So i put a System.CodeDom.GeneratedCode attribute on my class and bingo all the warnings were happily ignored by the compiler.

So if you are writing any tool to auto generate the code use this attribute to suppress all the warnings from the auto generated code. Of course you need to be sure that warnings are not harmful and are ignorable warnings (In my case they were all stylecop warnings and no fxcop warnings)

~Abhishek

Style cop and helper utilities

One of the basic necessities when you are working on a project is that the style cop warnings in your code should be zero. If there is a single style cop warning in the code one can run into several issues like
1) I don't like it when there's a warning in the Visual Studio Compile log
2) I don't like it when my build log on the build server becomes several hundred kbs just because of style cop warnings and to find wh

For the records style cop is a visual studio plug in which ensures that the code is written in a consistent way i.e. tabs, indentation, regions etc. are defined very clearly in the code. It can even be integrated into msbuild so that the build points out the errors and warnings.It can be downloaded from msdn if you want it for your project.

Once you start working with Style Cop you'd find it difficult sometimes to take care of every warning on yourself. The trick is to do the hard work early on by fixing each and every warning manually and then slowly you'd become habitual of writing the style cop compliant code.

Here are a couple of utilities which can help in case the warnings are huge and there's a time constraint on delivering the code

1) NArrange :- Takes care of beautifying the code. It can be downloaded from here. Use it carefully for the existing files as it rearranges the whole file according to style cop rules and more. It clearly demarcates the regions for fields, methods etc. The catch is that if you already have a version of a file in your source control the tool will rearrange the code in such a way that diffing the file against the version in source control is not going to help much.

2) GhostDoc :- Its an intelligent documentation tool which generates the documentation for the methods based on parameters. It can be downloaded from here. Most of the time if you are using the proper naming conventions the documentation generated by this tool works pretty well. However some time you might want to review it. What's more it can even take your customizations done on a method in interface to the class automatically.

So use these cool utilities and write your code using style cop.
Happy coding....

~Abhishek