.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


comments powered by Disqus

Featured

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube