.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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube