About Me

Sunday, February 16, 2014

Check-in SonarQube friendly code…!


Writing codes, even for a simplest program is not an easy task (only if you anticipate all movements of the end user ;)). Writing codes with higher readability is much harder. At the end of the day as a developer I check-in fairly readable (at least for me) code for everyone. But spacing, organizing of variables and commenting styles are somewhat different from developer to developer. Some people do not comment at all because their code exactly saying what’s happening throughout the code. These patterns and practices are debatable at the developer’s perspective, but at the industry level we have to go with a common pattern/practices which will be accepted by majority like SonarQube code quality measurement rules (most likely ethics). 

During the past couple of months we were able to maintain a good rule complacence with the help of few external tools. With the help of these we can easily clear most common simple issues like ordering and spacing across the project at once.


Once you download and install stylecop make sure all of your projects' properties are in following manner. 


You will find following [Run StyleCop] command in right click context menu, once click this will show following warnings dialog. So we can easily double click on each and edit quickly (no needs to visit Sonar Dashboard each time). 




Which is an an open source visual studio extension to cleanup, dig through and simplify our C#, C++, F#, VB, XAML, XML, ASP, HTML, CSS, LESS, JavaScript and TypeScript coding. Once installed you will see following commands in right click context menu.



Clean up - Cleanup random white space into a simple standard order. Add unspecified access modifiers. Utilize Visual Studio’s built-in formatting capabilities. Remove and sort using statements. And do it all automatically on save or on demand, from an individual file to the entire solution.

Reorganizing - Reorganize the layout of members in a C# file to follow Microsoft’s StyleCop convention, or your own preferences.

Finally make changes to your class templates.
Find visual studios class template location: default location C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class



Open the class file and paste following,

//----------------------------------------------------------------------- 
// <copyright file="$safeitemrootname$.cs" company="$registeredorganization$ i3"> 
//     Copyright $registeredorganization$ i3 $year$. All rights reserved. 
// </copyright>
// <summary>
// The $safeitemrootname$ class. target CLR version $clrversion$.
// Created by : $username$ on $machinename$.
// Created date time: $time$.
// </summary>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
 /// <summary>
 /// The $safeitemrootname$ class.
 /// </summary>
 class $safeitemrootname$
 {
 }
}

Once done just close and restart the Visual studio and try adding new class file which may looks like following.



No comments:

Post a Comment