Click here to Skip to main content
15,880,891 members
Articles / Visual Studio / Visual Studio 2012

Running Code Analysis and StyleCop on build

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
22 Jul 2014CPOL3 min read 44.6K   11   3
How to have automatic code review on build in Visual Studio using Code Analysis and StyleCop.

Introduction

Code Review is a good thing.

Quote:

In a complex program, architectural guidelines give the program structural balance and construction guidelines provide low-level harmony… Without a unifying discipline, your creation will be jumble of sloppy variations in style. One key to successful programming is avoiding arbitrary variations so that your brain can be free to focus on the variations that are really needed. --  Steve McConell

If you are programming in .Net best choice is to follow .Net coding standard which MS uses internally. This coding standard is documented in a book Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries and later also on MSDN. Event better news is there are 2 tools that implement this standard:

  • StyleCop - checks source code files
  • Code Analysis (ex FxCop, now integrated in VS) - checks compiled code

Below are detailed steps on how to configure these tools so they run on solution build.

Solution StyleCop configuration

In order to define Style Cop for project first solution must have StyleCop rule settings and StyleCop executable files.

Image 1

 

Image 2

Below are the rules I disable from default rule set in StyleCop:

  • Documentation rules
    • SA 1600 – Elements must be documented
    • SA 1633 – File must have header
    • SA 1634 – File header must show copyright
    • SA 1635 – File header must have copyright text
    • SA 1637 – File header must contain file name
    • SA 1638 – File Header file name documentation must match file name
    • SA 1640 – File header must have valid Company text
  • Ordering rules
    • SA 1200 - Using directives must be placed within namespace
  • Spacing rules
    • SA 1027 – Tabs must not be used

Solution Code Analysis configuration

Add Code analysis rule set and dictionary to solution root.

Image 3

I use recommended rule set is “Microsoft All Rules” without rules:

  • CA2210 Assemblies should have valid strong names (Design)
  • CA1062: Validate arguments of public methods (Design)
  • CA1303: Do not pass literals as localized parameters (Globalization)
  • CA2233: Operations should not overflow (Usage)
  • CA2204: Literals should be spelled correctly (Naming) – useful but it doesn’t work correctly.

See more about Code Analysis Dictionary.

Project manual configuration

To integrate Code Analysis Dictionary and StyleCop in build, unload and edit project and add following tags. Note that paths might be different depending on Solution configuration.

XML
<ItemGroup>
    <CodeAnalysisDictionary Include="$(SolutionDir)\CodeAnalysisDictionary.xml" />
</ItemGroup>

<Import Project="$(SolutionDir)\ExternalDlls\StyleCop 4.7\StyleCop.targets" />

 

Image 4

Project build configuration

Configure compiler warning level on Project properties to 4 (most strict).

Check XML documentation files (unless it’s a test project and not documentation is needed because tests are self-documented).

Image 5

Project Code Analysis configuration

Configure project Debug configuration to use Code Analysis rules in Solution root. Do the same for the Release configuration. Only difference is that in debug mode Code Analysis should not be run because it slows down the build. We keep CA running in Release to get error report from continues integration and to allow easily turning CA on by altering solution mode from Debug to Release.

Image 6

 

Image 7

 

What about JS

I still havent figured out this one but I'm hoping WebEssentials with add possibility to easily run JSHint on build and configure which .js files not to scan.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) CallidusCloud
Serbia Serbia
I am a software developer at CallidusCloud currently working on software for Quoting and Product Configuration.

In past few years I have been working on development of multi-portal CMS and I was responsible for defining Coding standard and Code Review process. For three years, I have lead team of programmers that developed Soprex framework for enterprise applications development and I have also built Soprex Quotation Tool on that framework. My main points of interests are enterprise app architecture, Scrum and TDD.

I blogs about software development at www.Vukoje.NET.

Comments and Discussions

 
QuestionGood Understandable and readable contents.. Pin
Rajeev Kr. Singh30-Jul-14 19:26
professionalRajeev Kr. Singh30-Jul-14 19:26 
GeneralMy vote of 5 Pin
Member 1052531724-Jul-14 20:00
Member 1052531724-Jul-14 20:00 
GeneralRe: My vote of 5 Pin
MVukoje24-Jul-14 21:47
MVukoje24-Jul-14 21:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.