Skip to main content

StyleCop Issues in C# Code - How to get rid of them real quick !!

Are you not able to checkin your code because of the Gated Checkin Policy due to StyleCop issues in your code ?
Do you have hundreds and hundreds of StyleCop issues to fix in short time ?
Are you looking for ways to quickly/automatically Fix StyleCop issues in your C# code ?

Then in that case, you have two options:
  • Use Resharper
    • Resharper is a great tool for fixing such issues, but it comes with a licensing cost.
  • Use Free extensions available in Visual Studio marketplace
Wouldn't it be nice if we could have a Visual Studio Extension that could fix all the StyleCop issues for us, and is FREE at the same time.

Well there is one, available as a Visual Studio Extension, its called "Code Formatter".

This tool in combination with StyleCop works very efficiently in resolving most of the StyleCop issues in code.

Below are some of the features of this tool:
Format the class file (.cs) to arrange members of class in the proper order.
Using statements are brought inside namespace block.
Unwanted namespaces are removed.
Unwanted space are removed.
Proper space introduced where ever required.
Adds header comments on members of the class.
Can group members by regions

Here are the simple steps that you can follow, to install and get started with this extension:
  • Install StyleCop & Code Formatter Extensions from Visual Studio => Tools => Extension & Updates.
  • Once installed, you can configure the Code Formatter tool from Tools => Options => Code Formatter => Update Settings


  • You will see three tabs in the Code Formatter Settings Popup:
    • Default - here you will see basic settings as to what all code you want to generate automatically
    • Type Layout
    • File Header - here you can specify the format of the header you want for your class files with auto generated code.




  • Once you have set up all of these, now its time for the real action. Open any class file, where you want to fix StyleCop violations. 


  • In my case, i am using UserBUS.cs class file, which has around 111 StyleCop Violations (File Header Missing, No Summary Tag on class, methods, No Proper Indentation, etc) as shown below:





  • Now we just need to right click on text editor, and from the context menu, select "Format Code" option.



  • You will notice that it completes adding the auto generated code within seconds. Next all you need to do is to save the class file, and Run StyleCop again on the same file.




  • And you will now see that all your 111 StyleCop Violations have vanished now, bringing the count to 0 StyleCop Violations.





There are ways to perform this code formatting in all .cs files present in your project, or solution. All you need to do is Right click on the Solution/Project node and go to "Code Formatter > Format Code" option on the menu.

When you do this, all the .cs files within the project or solution respectively will get formatted automatically.

Hope this is helpful. Cheers :)

PS: Code Formatter Extension manages to resolve most of the StyleCop violations, however there could be cases where you would have to do a fix manually like Method & variable naming, Using this qualifier, etc. But you will definitely end up saving a lot more amount of time that you would have spent on fixing most of these issues.

Comments

Post a Comment

Popular posts from this blog

Handling Concurrency in Azure Functions (HTTP Triggered)

Concurrency & Isolation Concurrency is one of the most common requirements when we don't want the resource to be accessed by multiple requests at the same time. Lets say we have a Database that holds products stock information, then it is very important that any read/write operation that happens on the Database must hold the property of Isolation . The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict – as opposed to relaxed – serializability), the effects of an incomplete transaction might not even be visible to another transaction. Options Available in Azure Functions Recently while working on Azure Functions, I discovered a really simple way of handling Concurrency via code. There are ways to con

Azure Functions - Retrieve data from Dynamics 365 CRM/CE Online using CRM OData Web API

Introduction: This blog describes how you can retrieve data from Dynamics 365 CRM/CE Online in an Azure Function, using CRM OData Web API. The CRM Web API has a couple of advantages over the traditional SOAP based approach to interact with CRM. These are rest based services, its easy to use and transport over http protocol , and there is no need to use additional web resources (XrmServiceToolkit ) of SDK (SDK.REST). Here is a detailed documentation on CRM OData Wbeb API. I will be using an Azure Function Application in Visual Studio 2017 to retrieve records from Dynamics 365 CRM/CE, by making use of OData API Endpoint provided by CRM. Prerequisites: Dynamics 365 CRM Online Trial https://trials.dynamics.com/ https://signup.microsoft.com/Signup?OfferId=bd569279-37f5-4f5c-99d0-425873bb9a4b&dl=DYN365_ENTERPRISE_PLAN1&Culture=en-us&Country=us&ali=1 Azure Subscription related to CRM Online instance https://portal.azure.com/ Visual Studio 2017 with Cl

Retrieve 5000+ Records in Dynamics 365 CE using CRM OData, Fetch XML and C#

If you want to retrieve 5000+ records in Dynamics 365 CE, you need to make use of Paging concept of CRM. By default 5000 records are retrieved in a single OData call in CRM. To get more, you need to make subsequent calls to OData Endpoint by passing a paging cookie in the request. I will be using a simple C# Console Application to retrieve 5000+ records from Dynamics 365 CE, by making use of OData API Endpoint provided by CRM and passing Fetch XML in OData Call . Concept: CRM OData Endpoints allows us to use Fetch XML to retrieve data from Microsoft Dynamics 365 CE.  If your resultset has more than 5000 records, you will be returned with first 5000 records ONLY in one OData call. To know if the resultset has more than 5000 records, we make use of the response from OData call. The response is added with a cookie value contained in a key - "@Microsoft.Dynamics.CRM.fetchxmlpagingcookie" If you wish to fetch the next set of resultset data, you will have to pass this c