Visual Studio Code Analysis vs. FxCop vs. StyleCop… what’s the diff?

Since I started using the StyleCop for ReSharper add-on I’ve been delving a bit further into the other tools out there for doing code analysis: StyleCop, FxCop, and Visual Studio Code Analysis. Even only considering those three, it’s a bit confusing. This post is my attempt to sort out, for my own benefit if nothing else, the differences between them.

Visual Studio Code Analysis

This code analysis tool is built into Visual Studio. It is performed as an after-build step (not to be confused with an actual post-build step). The description MSDN gives is this:

[The] code analysis for managed code tool analyzes managed assemblies and reports information about the assemblies, such as violations of the programming and design rules set forth in the Microsoft .NET Framework Design Guidelines.

Also, in a C++ project, we get this little bit of useful description information when enabling VS Code Analysis:

clip_image002

You enable VS Code Analysis for each project in the project’s properties:

image

Enabling Code Analysis yields such warnings during build-time as:

image

Visual Studio Code Analysis is very similar to FxCop. In fact, the two share some common assemblies. There was some timing differences on when each product was released, and for some reason the two have grown-up next to each other rather than being merged. Code Analysis is not built into all versions of Visual Studio, which is probably why. In any case, the rule sets are very similar, though from what I can tell only FxCop allows you to customize your own rule sets.

FxCop

Here’s the official description from MSDN for FxCop:

FxCop is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements. Many of the issues concern violations of the programming and design rules set forth in the Design Guidelines for Class Library Developers, which are the Microsoft guidelines for writing robust and easily maintainable code by using the .NET Framework.

FxCop is very similar to Visual Studio Code Analysis, though it might be described as containing a super-set of the latter’s rules. A little more about the difference in rule sets can be found here.

FxCop can be run as either a Windows application or at the command line. Use the latter as a post-build step for better integration with your build.

StyleCop

StyleCop is similar to FxCop, but it provides a different function and is, in fact, complementary to either FxCop or Code Analysis. (Note that it only works on C# source files.) It is mostly concerned with coding style and formatting. As such, it is run against source files, not assemblies like the other two analysis tools. It can be run from inside Visual Studio:

image

Or as a step in your build process. I haven’t looked into CI integration yet, but I’ll update when I’ve got those steps down. For right now, I’m making great use of the ReSharper add-on, which provides real-time StyleCop analysis as you code. It’s good stuff.