.NET Tips and Tricks

Blog archive

Configure Code Analysis in Visual Studio

I like well-written code, and I especially like it when I've written it. But I don't always live up to my own expectations. So I've used various tools (FxCop, CodeRush, ReSharper) to give me some objective feedback on the quality of my code. Starting with Visual Studio 2010, Microsoft built the latest version of their tools into Visual Studio: Visual Studio 2012 Ultimate (and up) and Visual Studio 2012 Professional (and up). From Visual Studio's Analyze menu, you can select Run Code Analysis on individual projects or on your whole solution.

The code analysis tools will report on whether your code reflects best practices, based on a set of Microsoft's rules. You should realize, however, that just because Microsoft has a specific rule, it doesn't mean that you should write your code according to that rule.

For instance, Microsoft expects all of its projects to use the CLSCompliant attribute. This attribute causes the compiler to check for issues that would prevent your code's external API from being compatible with other .NET Framework code.

For instance, it's perfectly OK in C# to have two public methods with the same name but with different upper and lower case letters (e.g. Update() and uPDATE()). However, those method names would be indistinguishable in Visual Basic, which is why they would violate the CLSCompliant attribute.

However, you probably don't need the CLSComplaint attribute because (a) you're a single language shop, and (b) you probably wouldn't do anything that violates the CLSCompliant attribute anyway (I mean, really: Update and uPDATE?)

It's important, therefore, from the Analyze menu to select the Configure Code Analysis choice and pick the right rule set for you. The Microsoft All Rules choice is almost certainly not the choice you want. Instead, select the <Choose Multiple Rule Sets> option and check off the rule sets that appeal to you. This will let you, over time, remove the rule sets that flag issues you're not interested in.

If you're really committed, consider adding and removing rules from the rulesets themselves. To do that, from the File menu select Open | File and navigate to the folder that the ruleset files are kept in (For my installation of Visual Studio 2012, that's C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Static Analysis Tools\Rule Sets\). Opening a ruleset file in Visual Studio will allow you to check off which rules in the ruleset you want to keep and which you want to ignore.

Posted by Peter Vogel on 05/28/2013 at 1:16 PM

comments powered by Disqus


  • Green Motherboard Closeup Graphic

    How To Compute Transformer Architecture Model Accuracy

    Dr. James McCaffrey of Microsoft Research uses the Hugging Face library to simplify the implementation of NLP systems using Transformer Architecture (TA) models.

  • VS Code Snippets Leads Recent Open Source GA Announcements

    GitHub's "Release Radar" for November shows a Visual Studio Code snippets project leading a raft of open source offerings that reached general availability in November.

  • C# Language Server Revamps VS 2022 Razor Editor for ASP.NET Core

    Microsoft is touting new capabilities in ASP.NET Core web development in Visual Studio 2022 thanks to a new Razor editor powered by a Razor Language Server.

  • Windows Forms Lives On in .NET 6

    Windows Forms would like you to know that the reports of its death are greatly exaggerated.

Upcoming Events