Wednesday, August 12, 2015

Overview of Stylecop With Code Analyzer in Visual Studio 2015

This article explains an upcoming code review tool StyleCop. We will understand the basics and do a small sample of code review practically to understand how StyleCop works.

The StyleCop tool provides warnings that indicate style and consistency rule violations in C# code. By default, the tool integrates into Visual Studio 2008 and Visual Studio 2010, and it can also be integrated into MSBuild-based build systems. It is also possible to author custom rules to run within the tool, and to create a wrapper host to integrate the tool into a custom build environment.

StyleCop provides value by enforcing a common set of style rules for C# code. StyleCop will continue to ship with a single, consistent set of rules, with minimal rule configuration allowed. Developers can implement their own rules if they so choose. StyleCop ships with the ability to seamlessly integrate with Visual Studio, MSBuild, TFS, etc. Developers are free to implement custom code to integrate StyleCop into other development and tooling environments, as described in the SDK documentation.

Installing and Download StyleCop 

As per the styplecop project the support for Visual Studio 2015 is not yet ready. Not sure if there will be any support available in the future because Visual Studio 2015 now has it's built-in code analyzer that can adapt to any provided code analyzer and run the ruleset accordingly against the project code files.

Getting the style cop running with Visual Studio 2015

Installing StyleCop.Analyzers (currently in pre-release version) Nuget package in Visual Studio 2015 project. Be sure to select “include prerelease” checkbox before searching.

After the installation of Stylecop.Analyzers Nuget package, expand the Reference tree and look for the Analyzers.
Now right-click on it and select “Open Active Ruleset”. This will open the list of Analyzers and their rule sets.

Now you can select and customize the Ruleset to run for your code. Now when you press save, this will add a new file “<your projectname>.ruleset” within the project. This customized Ruleset file can be used across the projects.

Managing the Rulesets across Projects

To manage the Rulesets across the projects you can configure the mapping of the Ruleset. Here is the procedure:

  •     Click on Analyze from Menu and select Configure Code Analysis for solution
This will open a window and provide the option to choose which ruleset to run against each project. You can choose the same file that you created for all projects or you can use separate ruleset files for individual projects.

Now that you have created a custom rule set file and setup the Ruleset for projects in a solution, you can launch the code analysis on the Solution level.

Launching the Code analysis

Analyze: Run Code Analysis for Solution (Allt + F11)

This should gives results in the Error window.

Now you have the code analysis result from your old friend styleCop.

Comparing FXCOP with StyleCop
It runs on compiled DLLs. 
       It runs on actual source code.

As it runs on compiled IL code it can be used for C#, 
in short any language which compiles to IL code.
       Currently it runs only on C#.



Post a Comment