About Me

Thursday, February 20, 2014

Writing Traceable Code

Some times we have to work independently on a particular layer with in multi layered project, For example working on a typical business layer with out knowing how the service layer provides its features to a consumer.
image
Basically in such layered architecture a developer cannot expect each and every argument (parameter) received is up to the requirement. so each and every time we have to check all the boundary conditions to make sure my own code works perfectly with out running in to any trouble.
Let me take the following example;
image

Through out the code until last few lines this NewUserRegistration Method checks for any faults in in coming parameters even though this looks an overhead believe me its going to save lots of user time when the code beings to consume by other layers.

As a software developer we cannot guarantee what might happen between the layers; if the system scales up your layer might be running as an isolated tier which has been exposed to multiple other consumers. So as said in your method you must guarantee that inputs are properly validated before process them. This approach saved a lots of developer time when it comes to QA and Developer testing.

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.



Monday, February 10, 2014

Using Pre-Build, Post Build Events in C# Project

I have a Project which runs as a Windows service – which is a WCF service hosted under self-hosting. For each build I want to stop the service and copy build files to the output and restart the service. Normally this would be possible through Build Events as shown in below


But with the TFS CI build integrations I had to face a problem since TFS server does not have such service(s) installed. It gave me an error saying no such service exists and also stopped the CI build popping out an error. What I had to do was modify the pre build and post build command line as follows
Pre-Build Event



Post Build Event



This worked fine when no service found at the environment, finally this resolved my problem.