Recipe: How to make use of intellisense feature when coding JavaScript in Visual Studio 2010

Motivation

You have included a JavaScript library and you want to make use of intellisense while coding JavaScript in Visual Studio but you couldn’t find the correct way for it.

Recipe

Ingredients

  • 1 JavaScript library to include (i.e. jslinq.js)
  • 1 -vsdoc.js file of library you want to include (i.e. jslinq-vsdoc.js)

Directions

  • Include the -vsdoc.js file. You don’t need to include it in the same folder where your actual .js file is located.  However, it is more practical to keep them as close as possible for the ease of maintenance, code navigation and delightful presentation 🙂

image

  • Put the following code at the top of the JavaScript file (i.e. map.js) where you call functions of the included library (i.e. JSLINQ.js).
/// <reference path="~/Scripts/jslinq/JSLINQ-vsdoc.js" />

Tip of the recipe

    • This “<reference … /> codes” must be the first code in you calling JavaScript file (i.e. map.js)
    • The referenced path must be the path of –vsdoc.js file. Not the path of the actual js file !

 

  • Create an object of the library.

image

Tip of the recipe

  • If you create in the wrong way, although you can still use the object as expected, the intellisense won’t work.
  • Now you can make use of intellisense while you code JavaScript.

image

  • That’s it enjoy your code 🙂
Advertisements

Swaping two variables without using any temporary variable

It is a common belief that two variables can only be swapped by means of using a temporary variable. Interestingly this is not true !!!

There is a smart way of exchanging two variable without using any other temporary variable.  Here comes the explanation.

Given X and Y as two variables to be swapped, the following sequence of assignments swaps the X and Y at the end.

X = X XOR Y

Y = X XOR Y

X = X XOR Y

Proof:

First of all, let’s recall the truth table of XOR.

XOR Truth Table
INPUT OUTPUT
A B
0 0 0
0 1 1
1 0 1
1 1 0

As is shown on the table, the result of XOR operator is 1 unless the two operands are equal.

This clearly leads us to the following formulas.

X XOR 0 = X

X XOR X = 0

where X represent not only a boolean type variable but also integral type variables.  So let’s analyze the sequence of assignments.

Step 1) X = X XOR Y

After the first equation X holds the result of X XOR Y.

Step 2) Y = X XOR Y

The second equation can be broken down to the following assignments.

Y = (X XOR Y) XOR Y

or alternatively

Y = X XOR (Y XOR Y)

which is identical to

Y = X XOR 0

which is is identical to

Y = X

So at the end of the second equation Y holds the original value of X.

Step 3) X = X XOR Y

The last equation can be broken down to the following assignments.

X = (X XOR Y) XOR X

which is identical to

X = Y XOR (X XOR X)

which is identical to

X = Y XOR 0

which is identical to

X = Y

 At the end of the last assignment the variable X holds the original value of Y. That’s it !

How to maintain multiple configuration files for multiple deployment environment?

This used to be one of the most challenging issue that the majority of the developers come across when to deploy their application into a variety of environments such as development, test(or staging) and production.  The problem increases linearly if the application is to be deployed to multiple customers since each of them may be expected to have their own development, test and production environments.

Visual Studio 2010 provides a lean solution , namely Web.Config Transformations, for this sticky problem.  Below are simple tutorials about how to apply the solution.

http://msdn.microsoft.com/en-us/gg454290

http://msdn.microsoft.com/en-us/vstudio/ff801895

Edit: It is possible now to apply configuration file transformation even for console applications with the following free extension that has recently been released.

http://visualstudiogallery.msdn.microsoft.com/579d3a78-3bdd-497c-bc21-aa6e6abbc859

image