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.

  1. 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.
  2. Open Microsoft Visual Studio from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
  3. 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.
  4. Rebuild the solution (Build | Rebuild Solution from the main menu). This step may take a few minutes to complete.
  5. 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

    Note:
    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.
  6. 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

  7. 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

  8. 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

    Note:
    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.
  9. 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.
    Note:
    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.
  10. 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

  11. 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

  12. 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