Book Image

Instant StyleCop Code Analysis How-to

By : Franck Leveque
Book Image

Instant StyleCop Code Analysis How-to

By: Franck Leveque

Overview of this book

In medium-sized and big projects, coding conventions are defined in order to improve readability and maintainability for all the developers of the team. Stylecop analyzes your code and detects coding rule violations during all the phases of your project lifecycle. Instant StyleCop Code Analysis How-to allows you to take advantage of the features of Stylecop by guiding you through how to configure it, how to integrate it in your project environment, and finally how to personalize it in order to fit your needs. Instant StyleCop Code Analysis How-to teaches you how to configure and integrate Stylecop in your programming environment. The book will do this by showing you how to configure Stylecop on the developers IDE to the continuous integration server. You will also learn how to customize Stylecop to fit your coding style by creating new rules as well as learning how to personalize your headers file. You will also see how to embed it in your own tools, using as an example the creation of a real time analysis add-on for Monodevelop. With Instant StyleCop Code Analysis How-to, you will have an overview of all the required steps to successfully integrate your programming team and enforce your own coding rules.
Table of Contents (7 chapters)

Understanding the Resharper add-in (Simple)


In this recipe, we will discover the StyleCop add-in for Resharper. We will see its real-time analysis and how to easily fix most of the StyleCop violations.

Getting ready

For this recipe, you will need to have:

  • StyleCop 4.7 installed.

  • Resharper 7.1 installed. Evaluation version available at http://devnet.jetbrains.com/docs/DOC-280.

  • Visual Studio Professional (2008, 2010, or 2012).

  • A sample C# project to modify.

How to do it...

Note

Before starting to see the benefit of Resharper with StyleCop, I have to say that the installation is not an easy one. First, each version of StyleCop seems to be tightly built on a specific version of Resharper. At the time of writing, StyleCop Version 4.7 is compatible with Resharper v7.1.1000.900. You can find the compatibility matrix at http://stylecop.codeplex.com/.

Then you need to install them in a specific order to be able to use them. The exact order to install them is to begin with Resharper, and installing StyleCop after. If you didn't do it this way, you will have to remove both products and reinstall them in the right order.

  1. When you open Visual Studio for the first time after having installed Resharper, you will be asked if you want to reset the default settings of Resharper to comply with StyleCop rules. Click on Yes to do so.

  2. Now let's open our sample project. The first thing visible is a new column at the right-hand side of the code screen as shown in the following screenshot:

    This column updates in real time and gives you all the errors or warnings you have in your document. If you go over one of the warnings displayed in the file, you will be able to see a description of the warning.

  3. If you click on it, a light bulb icon will appear on the left-hand side of your code and provide you options for dealing with the error. Generally the options are of three kinds for each error:

    • You can either autofix the rule, which is also marked by a light bulb icon

    • You can explicitly suppress the error by automatically adding a suppress message in your code. This is marked by a hammer icon

    • And you can change the level of this kind of error for Resharper. This is marked as a barred light bulb

    The following screenshot shows you the options available for dealing with the StyleCop violation:

  4. As this is quite a long task, we can fix in one go most of the violations. To do so, use the Clean up command. This command is accessible in three places:

    • From the Resharper | Tools menu.

    • In the Explorer solution, from the context menu.

    • And from the coding panel's context menu.

How it works...

Whereas the auto clean up feature of Resharper helps to quickly fix a lot of violations, it won't fix all of them, and you will have to check the remaining violations manually or with the help of the fixing feature of Resharper.

If you want to know what the autofixing capabilities of the Resharper add-in are, you can refer to the following link:

http://stylecop.codeplex.com/wikipage?title=ReSharper%20Fixes&referringTitle=Documentation

You can manage the behavior of the Resharper addin in the Resharper | Option… menu. You have two menus that address StyleCop. The first is the Code Inspection | Inspection Severity menu, which allows you to change the way each StyleCop violation is displayed in Resharper.

The second one allows you to manage how StyleCop is running under Resharper, as shown in the following screenshot:

The most important part in this screen is the Analysis Performance as it allows you to control the resources allocated to the StyleCop analysis.

There's more...

Though Resharper is certainly the most complete tool to autofix your violations and has the advantage of being directly supported by the StyleCop team, it's not the only program that is able to automatically correct violations. Other tools exist, and can help with StyleCop violation fixing.

Dev Express – Code Rush

This is a direct competitor of Resharper. Through plugins, it can also have some StyleCop compliant violation fixing features. However, at the time of writing, they seem to cover only a small part of StyleCop violations.

You can download Code Rush at the following address:

http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/index.xml

The plugin used to include StyleCop violation fixing is CR_StyleNinja and is available at the following site:

http://code.google.com/p/dxcorecommunityplugins/wiki/CR_StyleNinja

Code Maid

Code Maid is a free Visual Studio add-in allowing you to reformat your code. While it doesn't have a plugin for StyleCop, it allows you to reformat your code in order to remove layout and ordering violations.

You can find the tool at the following address:

http://www.codemaid.net/

NArrange

Narrange is another code beautifier, but unlike Code Maid, it's not a Visual Studio add-in. All configurations are made in an XML file and you can set up NArrange launch from the Visual Studio external tools menu. The configuration has a tool to simplify its edition.

One of the advantages of this tool is the fact it is not dependant on Visual Studio. You can integrate it with other developing tools such as SharpDevelop or MonoDevelop.

You can download it at the following site:

http://www.narrange.net/