Saturday, May 24, 2008

Beautify your [C#] code with Microsoft Source Analysis

Hear hear! Microsoft has just released Source Analysis for C# tool! This tool analyzes C# source files for compliance to set of rules in different categories, namely

  • Documentation category contains rules related to code documentation (such as "File must have formatted header" or "Elements must be documented")
  • Layout rules are rules related to text formatting (where curly brackets should be placed etc.)
  • Maintainability rules are the ones related to default specifiers and missing descriptions (f.e. Debug.Assert without text)
  • Naming rules are similar to such FxCop rules
  • Ordering rules check the order of declarations within namespace/class
  • Readability rules verify that the code does not contain not required extras (such as redundant base specifiers)
  • Spacing rules check for correct number of spaces in various situations

In all there around 200 rules provided!

The tool is installed as VS 2008 Integration Package providing tight integration with IDE. Once installed, two additional menus are available for C# projects in Solution Explorer – one to invoke Source Code Analysis, and another to configure analysis setting per project.

The configuration is somewhat similar to Static Code Analysis rules configuration (but you can look at hint window for rule detailed description)

"Run Source Analysis" menu is located right next to "Run Code Analysis"

Once you run it, Errors tool window will contain source analysis (SAxxxx) errors to handle

Additionally, there is MSBuild targets file (that may be optionally installed as part of the tool installation) that allows running Source Code Analysis as part of the build. Read the article detailing howtos on the development team's blog.

The application's first public release is 4.2 (since the tool was used internally at MS for quite a while) so it is full-featured and quite stable one. I am very excited by that release; in my opinion it may compare to first FxCop release by its importance (and it is free as is FxCop).

And according to the team's blog, the next version might also provide automatic fix options for some rules!

No comments: