You are on page 1of 11

StyleCop and FxCop

Installing StyleCop and FxCop

Page 2

Table of Contents
INITIAL STEPS................................................................................................................................................3 INSTALLING STYLECOP:............................................................................................................................3 INSTALLING FXCOP:....................................................................................................................................6

Page 3

Initial Steps
Copy the files from the below path:

\\skeltarnd1\RnD VM\Prasanth&Dibs\StyleCop & FxCop

Installing StyleCop:
Installation Steps: 1. Install the StyleCop-4.6.3.0.msi. It will automatically integrate with Visual Studio.

2. Replace the Settings.StyleCop file in your install directory with the file present in the path given above.

Steps to check your code:

1. 2. a) b)

Create a sample project with a class file and add sample code in the class file. Now, there are two options to verify your code: Right-click the project and select the Run StyleCop option. Right-click the classes file and select the Run StyleCop option.

Page 4

3. If you come across any warning right-click the warning and select the Show Error Help option to see the details of the error.

Note: It is recommended not to change any rule of StyleCop.

Example: I have defined a property before constructor of the class as given below:
public bool IsInteger { get { return isIntValue; } set { isIntValue = value; } }

Using StyleCop the following warnings appear: The property must have a documentation header. This means you must have a documentation header for each and every property. All properties must be placed after all constructors. This means you have to define property after all constructor of the class.

Page 5

The call to isIntValue must begin with the 'this.' prefix to indicate that the item is a member of the class. This means you have to use this keyword while using member variable. The call to isIntValue must begin with the 'this.' prefix to indicate that the item is a member of the class. This means you have to use this keyword while using member variable.

After adding the below code:


/// <summary> /// Check whether value is integer or not. /// </summary> public bool IsInteger { get { return this.isIntValue; } set { this.isIntValue = value; }

Added documentation. Added this while using member variable.

But you know what, I still see one warning. And that is: The property's documentation summary text must begin with: Gets or sets a value indicating whether

What does it mean? This means that if you are documenting property where you have Get and Set, you must start with Gets or Sets.
And if the property returns a Boolean value, an additional rule is applied. The summary text for Boolean properties must contain the words Gets a value indicating whether, Sets a value indicating whether, or Gets or sets a value indicating whether.

Now I changed to
/// <summary> /// Gets or sets a value indicating whether the item is integer or not /// </summary> public bool IsInteger { get { return this.isIntValue; }

Page 6

set { this.isIntValue = value; }

Guess what? Now I dont have any warning. This is how you have to fix all the warnings while writing the code.

Installing FxCop:
Installation Steps: 1. Copy winsdk_web.exe from the path given above and run it. (This will take several minutes to install). This is SKD for Windows 7 Framework 4. If you are using a different Operating System, download the appropriate SDK from Microsoft site. 2. Install SDK.

3. To install FxCop, run the FxCopSetup.exe file from the <Installed path>\Microsoft SDKs\Windows\v7.1\Bin\FXCop\FxCopSetup.exe.

Steps to check your assembly:

There are three different ways to check your assembly. They are: Shortcut from Start menu Add as external tool in your Visual Studio Add post-build event of your project

Using Start Menu: 1. 2. Open FxCop from Start > All Programs > Microsoft FxCop > Microsoft FxCop 1.36. Create a project and add your assembly using the Add Targets option.

Page 7

3. Click the Analyze option to analyze your assembly. All the errors and warnings will be displayed on the right-hand side of the window.

4.

Select an error or a warning to see the details of it in the lower window.

Page 8

Add as external tool in your Visual Studio: 1. 2. Open Visual Studio and go to Tools > External Tools Click Add and enter the data as given in the below screen shot:

Title: FxCop Command: C:\<Installed path>\Microsoft FxCop 1.36\FxCopCmd.exe Arguments: /c "/f:$(TargetPath)" "/d:$(TargetDir)\" /r:"C:\Program Files (x86)\Microsoft FxCop 1.36\Rules" Check Use Output Window so it will use Visual Studios output window to show the analyze result. 3. 4. Click Apply and OK. Open a project.

Page 9

5.

Select the project and click Tool > FxCop.

You can see the output as shown in the screen given below:

Page 10

6.

Add post-build event of your project.

You can also add the following command line code in the post-build event of project: "C:\<Installed path>\Microsoft FxCop 1.36\FxCopCmd.exe" /c "/f:$(TargetPath)" "/d:$(TargetDir)\" /r:"C:\Program Files (x86)\Microsoft FxCop 1.36\Rules"

If you add the command line code in the post-build event of the project, the warning message as shown below:

7.

Add the section given below in the header of each new file:

//-----------------------------------------------------------------------------// <copyright file="<file name>" company="Invensys Skelta"> // Copyright (C) 2010 Invensys Systems Inc. All rights reserved. // </copyright> // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. //------------------------------------------------------------------------------

/********************************************************************************************************** * Unit Name : <Class Name>

Page 11

* Created Date : 06/10/2011 * Created By : * Purpose * History * --------------------------------------------------------------------------------------------------------* Date Action Comments : Standard Class

* --------------------------------------------------------------------------------------------------------* *---------------------------------------------------------------------------------------------------------* * ********************************************************************************************************* */

You might also like