*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.
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 CodePrimitiveExpression(“TeslaGenerator” )),
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.
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.
I will continue with new features of CA in VS 2008 in my next post. Until then…