Posted by: rcosic | 24/03/2009

Code Analysis in Visual Studio 2008 (new features)

*I should clarify from the start that the Code Analysis is available only in the Team System editions of Visual Studio 2005/2008.

1. Analyze Menu

A first thing you can notice inside the Visual Studio 2008 when you open it, is the new Analyze menu:


This menu make us aware of the important role which Code Analysis plays in the development process, together with Code Metrics and Performance Analysis. As testing is becoming more and more integrated in Microsoft Visual Studio (especially in the forth-coming edition of Visual Studio: 2010 or Rosario) and more popular to developer masses, so is the Code Analysis and Metrics.

The menu has been initially generically named Developer, but it is (luckily) renamed to more specific Analyze, and some of the subitems have been rearranged to be more practical to use. So, if you have an opened solution, you can easily click on this menu and choose Run Code Analysis on Solution name option, or maybe Calculate Code Metrics for Solution/Project name to perform the code metrics calculation.

2. GeneratedCodeAttribute

If you followed my recent posts regarding CodeDOM, you will find attractive when I say that the CA guys have thought a way of working around the problem when the code has been generated (for example, X.Designer.cs class when a custom designer was used) and CA warnings just kept reappearing when the code was regenerated. What is new to VS 2008 is that by the usage of GeneratedCodeAttribute, those warnings are hidden from the user and do not appear again. Check this code to see how you can make it if you need it:

CodeAttributeDeclaration generatedCodeAttribute =
new CodeAttributeDeclaration( typeof(System.CodeDom.Compiler.GeneratedCodeAttribute).FullName,
new CodeAttributeArgument(
new CodePrimitiveExpression(“TeslaGenerator” )),
new CodeAttributeArgument(
new CodePrimitiveExpression(“”)));typeDeclaration.CustomAttributes.Add(generatedCodeAttribute);

This code snippet creates the attribute (which then could be added in the correspondent code definition) specifying the name and version of the tool that generated the code (in this case, a TeslaGenerator v1.2).

3. New CA Options in Project Properties

Next, a new design of Code Analysis tab is added inside the Project Properties window. Namely, CA engineers have been polite and, along with that we can have CA warnings enabled or disabled when building a solution (CODE_ANALYSIS constant from Visual Studio 2005), we now have the opportunity to suppress results from generated code, and make those annoying warnings (as described in previous paragraph) dissapear.

Also, we have an option to treat every single warning as error, thereby making our analysis more strict on points we consider more important.

New Code Analysis tab inside Project Properties

New Code Analysis tab inside Project Properties

4. Suppressing CA warnings in Code

Next thing that really makes the life easier is the possibility to suppress warnings directly in the source code. Typical scenario for this is baselining the code analysis project policy. Consider including zero-warnings CA policy in your solution : you want that, from now own (and you have a project which have been developed by couple of developers in several years), your .NET project (or more of them), does not produce any more warnings. You will find it difficult to manage to solve them in a short time (let’s say, you see them 300 in the Error List window). What you can do? The solution can be to clear them from the list by creating the GlobalSuppressions.cs (or .vb) file to store ‘commented out’ warnings in your code:


First option: In Source (you can see in the screenshot above) is to let the Studio generate attributes above the content to suppress its warning, or you can use Project Suppression File which the Studio automatically adds to your solution with the advantage of storing your suppression in one central location. Just to say, the file can be named differently, but this topic is out of scope of this post.

Adding Project Suppression File

Adding Project Suppression File

I will continue with new features of CA in VS 2008 in my next post. Until then…



  1. Analyze menu is available only in Team editions of Visual Studio 2008 and is not available in lesser packages, such as Professional. Nice post…

    • Yes, this is so. Maybe I should point that more clearly.
      Thanks for feedback, Mario!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: