About Me

Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Sunday, March 16, 2014

TypeScript

Concerning last couple of years JavaScript has gone through an explosive growth and with HTML5 it delivers a rich user environment compared to any other server side scripting models.
But what is wrong with the JavaScript;
even though it is easier to learn basics of JavaScript; writing a application scale JavaScript is hard. And most importantly it is meant to be a scripting language not a programming language, so it lacks proper structuring mechanism which larger application model necessarily look for. 
But people still writing large scale applications using JavaScript with the help of tools like Google’s GWT and scriptsharp (a C# => JavaScript code transformer ).
TypeScript helps to strengthen JavaScript with what is lacks features like classes, module and static typing.

TypeScript is a language for application scale JavaScript development

Ultimately all TypeScript codes will be compiled to a plain JavaScript code. And most importantly the compiled code and the compiler works on Any browser, Any host and Any OS. Simply all JavaScript codes are TypeScript and just copying and pasting will work, optionally TypeScript can contain Static types Classes and Modules where all of those disappear at runtime.
Compiler and all libraries related are Open Source.

Install TypeScript

image
Once you installed there will be a new project type as follows;
image
Once the project created solution explorer will looks like,
image
Simply I’m not going to do any modifications to the code it generated as an example. Lets just go through the app.ts file
class Greeter {
    element: HTMLElement;
    span: HTMLElement;
    timerToken: number;

    constructor(element: HTMLElement) {
        this.element = element;
        this.element.innerHTML += "The time is: ";
        this.span = document.createElement('span');
        this.element.appendChild(this.span);
        this.span.innerText = new Date().toUTCString();
    }

    start() {
        this.timerToken = setInterval(() => this.span.innerHTML = new Date().toUTCString(), 500);
    }

    stop() {
        clearTimeout(this.timerToken);
    }

}

window.onload = () => {
    var el = document.getElementById('content');
    var greeter = new Greeter(el);
    greeter.start();
};

For the moment forget about all the matters with JavaScript, code above is very simple (even though I’m not a JavaScript expert). 

window has “onload” event which is subscribed with an anonymous method contains a new Greeter object with parameterized constructor (taking an HTMLElement as argument)…. Once start method called time should be displayed with 500 milliseconds interval.

Lets just see the output

image

While its running lets peek in to debugger generated code,

image

it has this app.js (Which is JavaScript) mapped to app.ts (which is TypeScript) and with in app.js we can see pure JavaScript code rendered.
var Greeter = (function () {
    function Greeter(element) {
        this.element = element;
        this.element.innerHTML += "The time is: ";
        this.span = document.createElement('span');
        this.element.appendChild(this.span);
        this.span.innerText = new Date().toUTCString();
    }
    Greeter.prototype.start = function () {
        var _this = this;
        this.timerToken = setInterval(function () {
            return _this.span.innerHTML = new Date().toUTCString();
        }, 500);
    };

    Greeter.prototype.stop = function () {
        clearTimeout(this.timerToken);
    };
    return Greeter;
})();

window.onload = function () {
    var el = document.getElementById('content');
    var greeter = new Greeter(el);
    greeter.start();
};//# sourceMappingURL=app.js.map

And most importantly check the index.html, it refers the app.ts as
 <script src="app.js"></script>
 

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.