×
Namespaces

Variants
Actions

How to improve your coding standards using StyleCop

From Nokia Developer Wiki
Jump to: navigation, search
Featured Article
14 Jul
2013

This article explains how to use StyleCop tool to improve C# coding standards and to maintain consistency throughout the project.

WP Metro Icon Bug.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
SignpostIcon Code 52.png
Article Metadata
Tested withCompatibilityArticle
Created: pooja_1650 (09 Jul 2013)
Last edited: hamishwillee (22 Jul 2013)

Contents

Introduction

It's always a good practice to keep a check on the coding standards used in the project irrespective of the number of developers involved in it. It not only helps in readability and understanding but saves lots of time and resources in the maintenance phase as well. StyleCop is such a FREE .NET tool which enforces a consistent style to be used in the project.

Note.pngNote: As mentioned here, StyleCop tool can be used to check C# development guidelines only. It can't be used to check XAML guidelines.

Prerequisites

  • Windows Phone development environment
  • StyleCop

Installing StyleCop

StyleCop can be downloaded from here after which you need to install it.

Writing sample code

To showcase the usage of StyleCop, we need some piece of written code. We are writing a simple function which will show a MessageBox to the user.

The code snippet is as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StyleCopUsage.Resources;
 
namespace StyleCopUsage
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
greetVisitor();
}
 
private void greetVisitor()
{
MessageBox.Show("Hello, Visitor!");
}
}
}

If we try to build/run this sample piece of code then it will compile/run successfully.

Running StyleCop

We will now run StyleCop to check if there are any C# development constraints we haven't met and if yes, then how many? To run StyleCop, simply right click on any page inside the application and choose Run StyleCop option or go through Tools>>Run StyleCop.

The results of the StyleCop tool can be viewed in the form of Warnings in the Error List window as shown below.

StyleCop Warnings

You can see, we have 19 StyleCop warnings in our project consisting of just one file and one very simple function. All these warnings are showing that we've not met the guidelines of C# development. Now you can imagine how many warnings would be there in our regular average projects and how much uneasy it would be for any other developer who used to follow C# guidelines immensely. The situation even gets worse when we(the developers), at times, need to deposit our source code to our clients as it puts a negative impression on them.

Removing StyleCop Warnings

The StyleCop warnings can be removed easily when read. So, after removing most of the StyleCop warnings our code looks like shown below.

// -----------------------------------------------------------------------
// <copyright file="MainPage.xaml.cs" company="Sample Company">
// sample rights
// </copyright>
// <author>Pooja Arora</author>
// -----------------------------------------------------------------------
 
namespace StyleCopUsage
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StyleCopUsage.Resources;
 
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
this.InitializeComponent();
this.GreetVisitor();
}
 
private void GreetVisitor()
{
MessageBox.Show("Hello, Visitor!");
}
}
}

The remaining warnings are shown below.

Remaining StyleCop warnings

StyleCop Settings

We have an option to remove some or all of the predefined settings set by StyleCop. These settings can be accessed by selecting the project in the Solution Explorer window, right clicking and choosing StyleCop Settings. By default all the settings are applied but we can modify them as per our project's/time's requirement.

Default view of StyleCop Settings

Assuming we don't want to handle below mentioned kind of warnings.

Remaining StyleCop warnings

For this, we can deselect the relevant check box(s) from the StyleCop Settings as shown below.

Modifying StyleCop Settings

After applying above settings, if we again try to re run StyleCop, we will see all the warnings have been removed.

No warnings

Summary

This way we can keep a check that all of us (developers) met with the C# development guidelines using StyleCop tool while making Windows Phone apps.

References

This page was last modified on 22 July 2013, at 03:03.
71 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×