Exercise 1: Introduction to Code Analysis
In this exercise, you will learn about new Code Analysis features in Visual Studio 2010 by configuring the rule set used, performing code analysis on a sample project, and addressing some of the warnings that are raised.
- Log in as Abu Obeida Bakhach (Dev) if you have not already done so. The password is P2ssw0rd (capital letter P, the number two, the letter s, the letter s, the letter w, the number zero, the letter r, and the letter d). Please see “Working with the Visual Studio 2010 RTM Virtual Machine” for instructions on how to log into the VM.
- Open Microsoft Visual Studio from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
- In Source Control Explorer (View | Other Windows | Source Control Explorer), navigate to Tailspin Toys | Development | Iteration 2 and double-click on the TailspinToys.sln file to open the Tailspin Toys solution.
- Rebuild the solution (Build | Rebuild Solution from the main menu). This step may take a few minutes to complete.
In Solution Explorer, left-click on the Tailspin.Model project node and select Analyze | Configure Code Analysis for Tailspin.Model from the main menu, which will load the project properties and go to the Code Analysis tab.
Figure 1
Configuring Code Analysis for a project
The Code Analysis tab is new to Visual Studio 2010, and it allows you to choose from sets of rules rather than picking and choosing from one flat list of rules, as was the case in previous versions.
Select the “Microsoft All Rules” option for the Rule Set to select the comprehensive set of rules.
Figure 2
Configuring Code Analysis rule set to use
In Solution Explorer, right-click on the Tailspin.Model project node and select Run Code Analysis from the context menu that appears.
Figure 3
Location of contextual Run Code Analysis command
The Code Analysis feature runs through static code analysis rules as defined by Microsoft and displays the results as warnings. Scroll through the list of warnings and read a few of them.
Figure 4
Code Analysis warnings
Depending on the version of Tailspin Toys that you are running, you may see more or less warnings than depicted in screenshots.
Code Analysis rules can also be configured to show up as errors if desired.
- The warnings produced by Code Analysis provide a wealth of information including a unique category ID (such as CA1062 in screenshot above), a group name (Microsoft.Design in screenshot above), and a brief description of the problem or suggested fix, and the file location of the offending code.
Pressing F1 while selecting one of the warnings will take you more descriptive help online. This may not be available to you within the virtual environment.
Find a warning that looks like it will be easy to fix quickly, such as the warning for CA1062 in the screenshot above, and double-click on it to load the location in code.
Figure 5
Code Analysis errors are linked to source code
Perform the code fix necessary to resolve the warning. For CA1062, we are told that we should validate parameter ‘item’ before using it. One potential fix can be seen in the following screenshot.
Figure 6
Validating input parameter before use resolves CA1062
- Select Analyze | Run Code Analysis on Tailspin.Model from the main menu and verify that the warning disappears. There should be one less warning than before.
Next Step
Exercise 2: Suppressing Code Analysis Warnings