The other day I was speaking with a client and they asked me to describe the difference between StyleCop and FxCop, two useful tools for code analysis. This is a great question! I’ll clarify some details about each tool later in this post, but let’s begin with an overview of the two tools.
In many ways, StyleCop and FxCop are “cousins” that complement one another because each tool performs a somewhat different code analysis function. StyleCop is a source code analysis tool that provides developers with an effective way to follow C# coding standards. FxCop runs against the compiled binaries as a way to understand and enforce the.NET Framework Guidelines for managed code assemblies.
StyleCop runs against C# source code but cannot analyze VB.NET or another .NET language source code. FxCop runs against .NET compiled binaries but cannot analyze source code and aspects like the proper use of braces, whitespace, or comments.
Coding standards are intended to improve readability, consistency, and maintainability. StyleCop defines a widely-used C# coding standard. StyleCop is a static analysis tool that provides developers an effective way to follow the coding standard. It also gives projects a way to customize and report violations of the coding standard.
StyleCop is a free source code analysis tool for C# developers that was initially developed by Microsoft. The governance and coordination of the StyleCop project were turned over to the .NET community as a CodePlex project. StyleCop integrates well into Visual Studio and warns developers when coding standards are not followed. If you have the ReSharper productivity tool, StyleCop integrates and provides options for code cleanup and feedback while writing code, to further help adhere to the coding standard.
The C# coding standard that StyleCop defines is widely-used and many developers are happy to follow it. Since the standards enforced by StyleCop are not likely to exactly match your team’s style preferences, StyleCop provides an effective way to turn off, change, or customize the coding standards within an organization or on a project-by-project basis.
Coding standards are about establishing a coding style and writing code that adheres to that style. The intention of establishing and adhering to coding standards is to make the source code easier to read and straightforward to maintain
FxCop provides a way to understand the broad condition of the managed code assemblies and various ways to drill into the details of each rule violation. After .NET managed code assemblies are built, FxCop can inspect the assemblies to determine and report if any of the configured rules have been violated.
The latest version of FxCop is version 10.0. It is a free analysis tool that is part of the Microsoft Windows SDK for Windows 7 and .NET Framework 4 version 7.1. FxCop is both a desktop application and a command-line tool that can be used for analysis outside Visual Studio and as part of the automated build process.
The FxCop desktop application allows you to define a project and select the target assemblies to analyze. You can specify the rules that ought to be followed, specific to your project. You can run the analysis to reveal and explore the violations occurring within the project. You can evaluate the violations and report or suppress the violations based on their priority and significance to the project. The FxCop application allows you to analyze and improve the code.
The FxCop command-line application is called FxCopCmd.exe. With this tool, you can monitor the code within the build and continuous integration (CI) processes.
Also, FxCop provides both the tools to analyze the software and detailed guidance on where and how to improve the source code. It provides the means to monitor the situation with reports and metrics. Many CI servers provide the facilities to display FxCop output as HTML reports.
Source: Stephen Ritchie, Pro .NET Best Practices (New York: Apress, 2011). Excerpted with permission of the author.