Why global variables are evil in JavaScript

First what are global variables ?

Global variables are variables that are accessible from every scope in your JavaScript, even in different files referenced in the same page.

When i started to learn JavaScript, i thought that using global variables is a great thing. But that was totally wrong, because it’s easy to overwrite the values of it unintentionally as they are accessible from anywhere in your code, and if you didn’t notice that two variables have been overwritten. you might be in a big trouble, your code will be throwing errors, and it will be hard to figure out the reason of these errors.


the solution is to encapsulate your code, And because JavaScript has ‘function level scope‘ so you could just write all your code within a self-executing, anonymous function:

   (function () {
        var x = 10, 
            y = 20;
        // code goes here 

Continue reading

Using Microsoft Translator API with Windows Store App HTML 5 & Javascript


is a translation portal by Microsoft as part of Bing services that can provide a machine-translation of texts or entire web pages. Microsoft Translator also provides a set of web service APIs (that can be called via an HTTP REST service, an AJAX callable service, or a SOAP service) for developers who wish to use Microsoft Translator in their applications.

In this article we will develop a Windows Store application that uses the AJAX callable service of Microsoft Translator to translate text between languages.

First you have subscribe to the Microsoft Translator API on windows azure from here. Follow this link will help you to subscribe. Then register your application on Azure Data Market to get the clientId and clientSecret that we will use to obtain the access token.
The access token is passed with each API call and is used to authenticate your access to the Microsoft Translator API. You must obtain an access token to use the Microsoft Translator API.

NOTE:  Bing AppID mechanism is deprecated and is no longer supported. As mentioned above, you must obtain an access token to use the Microsoft Translator API. The access token is more secure and more flexible.

Now we will create a Windows RunTime component (WinRT Component) project that will have two classes we will use to obtain our access token .

The first class is AdmAccessToken that will have Token Request Output Properties :

using System.Runtime.Serialization;

namespace MicrosoftTranslatorRT
  public sealed class AdmAccessToken
    public string access_token { get; set; }
    public string token_type { get; set; }
    public string expires_in { get; set; }
    public string scope { get; set; }

Continue reading

SOLID – The Dependency Inversion Principle


It states that high-level classes should not depend on low-level classes. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.

low level classes: are those which implement basic and primary operations.

high level classes: are those which encapsulate complex logic and rely on the low level classes.


The Dependency Inversion principle (DIP) helps to loosely couple your code by ensuring that your high-level modules depend on abstractions rather than concrete implementations of lower-level modules.

The Dependency Injection pattern is an application/ implementation of this principle.


suppose that we have a banking system software. As a part of that software it is necessary to transfer money between accounts. So there is a bank account class which contains 2 properties account number and the balance. also 2 methods addFunds and removeFunds. Continue reading

SOLID – Interface Segregation Principle

Definition :

Clients should not be forced to depend upon interfaces that they do not use. Or as Uncle Bob says: Make fine grained interfaces that are client specific.


The idea in this picture that we have a very small dependency which is the USB cable in the upper left but instead of being able to plug that into a USB port in a computer or something, we are forced to use that device here in the right which contains buttons , switches and lights and a lot of things that we don’t need.

When we have non-cohesive interfaces, the ISP guides us to create multiple, smaller, cohesive interfaces. The original class implements each such interface. Client code can then refer to the class using the smaller interface without knowing that other members exist.


We have a fat Interface called IBird that contains these four methods or behaviors ( Walk , Eat , Sleep, Fly )

3-6-2013 2-19-02 PM

Continue reading

SOLID – Liskov Substitution Principle

Definition :

it states that derived class must be substitutable for their base classes. which means that the subtypes must be replaceable for their super type references without affecting the program execution.

This principle was early described by Barbra Liskov in 1988.


When we were learning about object oriented programming, inheritance is usually described as an “is a” relationship, like If a penguin “is a” bird, then the Penguin class should inherit from the Bird class. The “is a” technique of describing inheritance relationship is simple and useful, but occasionally results in bad use of inheritance.

I’ll use the square and rectangle example to demonstrate the principle:

we have a Rectangle base Class that have two properties width and height , and subclass called Square that have the same two properties Height and Width and a method called SideLength that we use to set the values of height and width properties to make them equal.

28-5-2013 4-10-02 PM

Continue reading

SOLID – Open/Closed Principle


it states that the software entities (classes, modules, functions,..etc.) should be open for extension but closed for modification.

in this principle we want to be able to add a new behavior ‘open to extension’. without the need to change our source or binary code ‘closed to modification’. or in other words we should strive to write code that doesn’t have to be changed every time the requirements change.

huh! change behavior without changing the code. !!

Yes we can achieve this by relying on abstraction (Interfaces, Abstract Base Classes) because there is no limit to number of different ways we can implement that abstraction .

An Example :

Suppose that there is a class called Rectangle that has a width and height properties within it.


Continue reading

SOLID – Single Responsibility Principle


Every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.
In this principle the class should be only responsible for one thing or one task to do. responsible here means a reason to change, there must be never more than one reason to change your code ‘class’.

 The more responsibilities we have the more likelihood of change and the more changes affects the more errors will be introduced.

 In the SRP we want to strive for low coupling but high cohesion

 cohesion: Is the degree to which the elements of a module or class belong together.

 coupling: Is the degree to which each program module or class relies on each one of the other modules.

So Coupling talks about the inter-dependencies between the various modules while cohesion describes how related functions within a module are.

Continue reading