Code Analysis is a great way to identify problems with your code and enforce standards. By default, Code Analysis in Visual Studio 2012 uses a rule set called Microsoft Managed Recommended Rules. It’s a set of rules that Microsoft has deemed most important, and they suggest following these rules in all projects. Here’s their description of the rule set:
You can use the Microsoft Managed Recommended Rules rule set to focus on the most critical problems in your managed code, including potential security holes, application crashes, and other important logic and design errors. You should include this rule set in any custom rule set that you create for your projects.
I like this rule set, but it’s not enough for me. I suggest using Microsoft’s Basic Design Guideline Rules rule set. The MSDN description suggests, “You should include this rule set if your project includes library code or if you want to enforce best practices for code that is easy to maintain.” Best practices for code that is easy to maintain? Who doesn’t want that!?
I can tell I’ve sold you on this rule set, and now you’re wondering how to actually use it. Don’t worry, it’s easy!
- Open the Code Analysis window in Visual Studio (View > Other Windows > Code Analysis)
- Click the ‘Settings’ button
- Click the rule set value next to the project(s) that you want to change
That’s it! Now, when you run Code Analysis, it will use the selected rule set.