MySQL Tip: Using Bitwise Operations for Multivalued Attributes

Recently, I was working on an Application when I decided to use bitwise operations on a multi-valued attribute. I haven’t done any performance benchmarks, but I would think this should improve the performance on making Queries.

Here’s an example: Suppose we want to store Users and their Interests. We should have the following 3 Tables:

  1. Users – Stores User Information
  2. Interests – Various Interests
  3. UserInterests – Stores the Relationship between Users and Interests

Relationship Tables in Database: How it is normally stored

Table 1: Users

Id Name Phone

Table 2: Interests

Id Name
1 Movies
2 Soccer
3 Swimming
4 Running

Table 3: UsersInterests

UserId InterestId

 

Making Queries:

Query 1: Find all users whose interests are either swimming, running, soccer (one or all of them)?

One would query them as:

SELECT * FROM
users WHERE
Id IN (
  SELECT UserId FROM
  UsersInterests WHERE 
  InterestId IN (2, 3, 4)
)

Query 2: Find all users whose interests are swimming AND running AND soccer (All of them)?

Ans: One could query them as:

SELECT * FROM USERS
WHERE ID IN
(
  SELECT USERID FROM UsersInterests
  WHERE InterestId IN  (2, 3, 4)
  GROUP BY UserId
  HAVING COUNT(UserId) = 3
)

Improvement: Storing Interests as Powers of 2

If we decide to store InterestId’s as Powers of 2, then we can easily apply bitwise AND, OR operations as each of the interests Ids would be mutually exclusive to each other.

So, we add another field in UsersTable (sort of Cached) called UserInterests as follows:

Table 1: Users

Id Name Phone UserInterests

Table 2: Interests [Id’s stored as Powers of 2], also I added another column in it’s bit representation

Id Name Bit Representation [not added in DB]
1 Movies 00001
2 Travelling 00010
4 Soccer 00100
8 Swimming 01000
16 Running 10000

Thus, for the following interest combinations, UsersInterests in Users Table could be stored as:

5  // = Movies and Soccer = Bitwise (1 | 4)
13 // = Movies and Swimming and  Soccer  = Bitwise (1 | 4 | 8)

Making the same Queries now:

Now that we have UserInterests already pre-cached/stored in Users Table, we can do the same queries easily

Query 1: Find all users whose interests are either swimming, running, soccer (one or all of them)?

Ans: One could query them as:

SELECT * FROM users 
WHERE (UserInterests & 28) != 0  // 28 = (4 | 8 | 16)

Query 2: Find all users whose interests are swimming AND running AND soccer (All of them)?

Ans: One could query them as:

SELECT * FROM USERS
WHERE UserInterests = 28

No more nested inner queries. It all happens in one shot!

Conclusions & Limitations

This approach has the major limitation of being able to store a maximum of 32 types of multi-valued attributes or 64 (if bigint) is used.

I still need to do benchmarking to see how much of a performance improvements it offers.

Android Volley Tutorial – Making HTTP GET, POST, PUT

Google released Android Volley Library around May/June 2013, which has been internally used by Google for some time. It is supposed to provide Fast Networking Operations and also takes care of Threads nicely. If you are unfamiliar with Volley, please see Google I/O 2013 Video

Unfortunately, there is almost no documentation on Volley. So, I put together code snippets on how to make Volley HTTP Requests (GET, POST, PUT, DELETE).

Setting up Android Volley Library

Setting up is straight-forward. Clone the Volley project from here and then import the Volley into project. A comprehensive tutorial on setting up can be found here.

Key Classes of Android Volley

The following are the Key classes of Volley:

  • – RequestQueue: A Queue containing the Network/HTTP Requests that needs to be made.
  • – Request: A Base Class which contains Network related information like HTTP Methods.
  • – StringRequest: HTTP Request where the response is parsed a String. View Source
  • – JsonObjectRequest: HTTP Request where the response is JSONObject. View Source

Getting Started with Android Volley

At first make a RequestQueue, which holds the HTTP Requests. View Source. Ideally, the RequestQueue should be made once and then referred to it across the Application. The Application is an ideal place to make it.

RequestQueue queue = Volley.newRequestQueue(this);  // this = context

Making GET Requests

Making GET Requests is simple. The example below uses JsonObjectRequest. It prepares a JsonObjectRequest and
passes and then adds it to RequestQueue. The JsonObject accepts 4 parameters (Http method, Url, Json values, Response Listener – Invoked on success, Error Listener – Invoked on failure).


final String url = "http://httpbin.org/get?param1=hello";

// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
	new Response.Listener<JSONObject>() 
	{
		@Override
		public void onResponse(JSONObject response) {	
                        // display response		
			Log.d("Response", response.toString());
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {			 
			Log.d("Error.Response", response);
	   }
	}
);

// add it to the RequestQueue	
queue.add(getRequest);

Making POST Requests

For a POST request, to add form parameters/values, the getParams() method needs to be overridden and a Map needs to be returned.

url = "http://httpbin.org/post";
StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
	new Response.Listener<String>() 
	{
		@Override
		public void onResponse(String response) {
			// response
			Log.d("Response", response);
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {
			 // error
			 Log.d("Error.Response", response);
	   }
	}
) {		
	@Override
	protected Map<String, String> getParams() 
	{  
			Map<String, String>  params = new HashMap<String, String>();  
			params.put("name", "Alif");  
			params.put("domain", "http://itsalif.info");
			
			return params;  
	}
};
queue.add(postRequest);

Making PUT Requests

Creating PUT Request is same as POST basically.

url = "http://httpbin.org/put";
StringRequest putRequest = new StringRequest(Request.Method.PUT, url, 
	new Response.Listener<String>() 
	{
		@Override
		public void onResponse(String response) {
			// response
			Log.d("Response", response);
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {
                         // error
			 Log.d("Error.Response", response);
	   }
	}
) {

	@Override
	protected Map<String, String> getParams() 
	{  
			Map<String, String>  params = new HashMap<String, String> ();  
			params.put("name", "Alif");  
			params.put("domain", "http://itsalif.info");
			
			return params;  
	}

};

queue.add(putRequest);

Making DELETE Requests

url = "http://httpbin.org/delete";
StringRequest dr = new StringRequest(Request.Method.DELETE, url, 
	new Response.Listener<String>() 
	{
		@Override
		public void onResponse(String response) {
			// response
			Toast.makeText($this, response, Toast.LENGTH_LONG).show();
		}
	}, 
	new Response.ErrorListener() 
	{
		 @Override
		 public void onErrorResponse(VolleyError error) {
			 // error.
			 
	   }
	}
);
queue.add(dr);

Hope it helps!

Android activity hide action bar

If the Action bar of an Android’s activity needs to be hidden through the manifest xml file (non Java Programming), then the easy way is to add the following attribute in Activity tag:

android:theme="@style/Theme.NoActionBar"

However, if a custom Theme is created then it is convenient to create another style extending the custom theme and then adding the following two items in it.

        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>

Suppose, the default theme is called MyTheme, then we create a new Theme called NoActionBar, and add the following two items.

<style name="NoActionBar" parent="MyTheme">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
</style>

Then, simply refer by using the NoActionBar Theme

android:theme="@style/NoActionBar"

Using OAuth 2.0 with Google API in Phonegap / ChildBrowser

This article explains how to implement OAuth 2 with Google API (gapi) on Phonegap using Childbrowser Plugin. We will authorize Google Tasks API from Phonegap and then retrieve a list of Tasks (of default list) from Google Server. Because the App is built using Phonegap, it can be deployed in iOS, Android, Blackberry, Windows Phone with no changes!

We have created 2 javascript files (liquid.js, liquid.helper.oauth.js) which facilitates the authorization process and I shall be using those 2 files in this example. However, once the process is understood, you can easily do it on your own.

1. The Output (on Android)

The output shall look like below. Upon initial launch, the user shall be required to authorize it. After the user authorizes, on next visit, the App shall automatically redirect to the Task List Page.

Working Demo: Download and install the app from Phonegap build and try on your devices!

2. Prerequisites: Before we begin

Basically, you shall need the following configured before proceeding:

  1. Phonegap/Cordova configured with
    ChildBrowser plugin
  2. jquery 1.7.2 or above
  3. jquery Mobile 1.1.1

  4. Google API Client

Here’s a screenshot of directory structure:



You won’t have the files underlined red. So, copy the following files from github:
liquid.js
helper/liquid.helper.oauth.js
model/liquid.model.tasks.js
As for, qtaskdemo.js, we will create it. So, create an empty file qtaskdemo.main.js (or anything) for storing js code for the index.html.

Now, register a OAuth access from your Google API Console. For the Client Application Type, choose ‘Installed Application’ and for Installed application type, choose ‘Other’. Here’s a screenshot:

OAuth 2.0 Access

Then, open up liquid.js and update the values for client_id and client_secret (line 65 onwards) received from Google API.

// The client_id obtained during application registration
client_id: "###",
				
// The client secret obtained during application registration
client_secret: "###", 	

3. Getting Started: Referencing the JS Files

I am assuming the html file to be used is index.html (from Activity). So, now add/reference the Javascript files. The head shall look like below. Also, add the qtaskdemo.main.js (or else) right before body ends

        < head >
        GTask Demo
        
                
        
        
        
        
        
                
        
        
        
        
        
        
    
    < /head >  
    < body >
     ....
     ....
    
   < /body >
  

On the section ‘JS to go here’, add the following lines:

$(document).bind("mobileinit", function() {            
            $.mobile.touchOverflowEnabled = true; // required
            $.mobile.defaultPageTransition = 'none'; // optional - for better performance on Android
            $.mobile.loadingMessageTextVisible = true; // optional - added to show text message
            $.mobile.buttonMarkup.hoverDelay = 20; // optional added to remove sluggishness - jqm default 200
});

4. Creating an Unauthorized Page

Now that JS has been referenced, lets create an unauthorized page (add the lines below inside your body):

   

GTask Demo

Getting Started

Authorize the App from your Google Account and you are ready to go!

Powered by Task

5. Creating a Tasklist Page

Lets create a simple Tasklist Page. Copy/Add the lines below inside your body:

	

Tasks Demo

Refresh Signout

6. Adding Authorization Process

So far, JS has been added, and 2 pages has been created. Add the following lines in qtaskdemo.main.js to prepare your app to start when device is ready.

$(document).ready(function() {
	
	/* startApp after device ready */
	document.addEventListener("deviceready", startApp, false);
});


/**
 * Start the App
 */
function startApp() {

}

Now, lets add an authorization feature. The Authorization process is completed in 3 steps.

  1. A New window is opened where user has to login to their google account and allow access to the App.
  2. After successful login, an authorization code is returned
  3. Using the authorization code, make another request to google’s server to get the refresh token and access token. The refresh token is permanent, however, the access Token expires in 1 hour. Thus, the refresh token needs to be stored in a local storage, as it can be used again to get new access token.

If its unclear, please spend some time on google’s oauth playground for a better understanding.

Now that OAuth process is clear, lets begin development. Initially, the App should show the Authorization page and upon clicking ‘Authorize &amp Go’, we want the authorization process to begin. Lets look at authorize function of liquid.helper.oauth.

/**
 * Attempts to authorize user using OAuth 
 * Opens up Another window where user allows access or denies it.
 *   
 * @param {function} callBack   A callback function which is invoked
 */
authorize: function(callBack) {
..
}

This function does the authorization process with google server and after success/error of the process, it invokes the callback function. liquid.helper.oauth also keeps track of the the error or success in its variable status, and also stores the authorization code (auth_code) in its local variable authCode. The status are:

/** 
 * Enum for Status values
 * 
 * @enum {number}
 *  
 * SUCCESS - Successfully data received from server
 * ERROR - Error occurred when trying to receive from server
 * NOT_DETERMINED - undetermined
 */
status: { 
	SUCESS: 1, 
	ERROR: -1, 
	NOT_DETERMINED: 0 
},

So, we add the following code in our qtaskdemo.main.js file

function startApp() 
{	
   var oAuth = liquid.helper.oauth;
	
    $("#access-code").click(function(event) {
        oAuth.authorize(authorizeWindowChange);
        event.preventDefault();
    });
}

Now, we need to define authorizeWindowChange function, so we add the following function:


function authorizeWindowChange(uriLocation) {
    //console.log("Location Changed: " + uriLocation); 
    var oAuth = liquid.helper.oauth;
   
   // check the status, oAuth process is successful!	
    if (oAuth.requestStatus == oAuth.status.SUCCESS) {
        var authCode = oAuth.authCode;
        // Got the authCode, now use it to get the refresh token ?
    }
    else if (oAuth.requestStatus == oAuth.status.ERROR) 
    {
    	console.log("Error >> oAuth Processing");
    } 
    else {
        // do nothing I guess!
    }


}

We received the authorization code (authCode), now we need to use it to get refresh token and an initial access token, and also store the refresh token. The function saveRefreshToken in liquid.helper.oauth does that for you. Here’s what it looks like:

/**
 * Saves the Refresh Token in a local database or localStorage
 * This method shall be invoked from externally only once after an 
 * authorization code is received from google's server. This method 
 * calls the other method (getRefreshToken) to get the refresh Token and
 * then saves it locally on database and invokes a callback function
 * 
 * @param tokenObj A Object containing authorization code
 * @param {String} tokenObj.auth_code The authorization code from google's server
 * 
 * @param {Function} callback The function to be invoked with parameters
 */
saveRefreshToken: function(tokenObj, callback) {

..
}

So, back to the authorizeWindowChange function, we invoke the function saveRefreshToken and give the function startPageTaskList as a callback. startPageTaskList simply changes the current Page to a Task List Page as shown below.

function authorizeWindowChange(uriLocation) {
    //console.log("Location Changed: " + uriLocation); 
	var oAuth = liquid.helper.oauth;
	
	// oAuth process is successful!	
    if (oAuth.requestStatus == oAuth.status.SUCCESS) {
        var authCode = oAuth.authCode;

        // have the authCode, now save the refreshToken and start Page Task List
        oAuth.saveRefreshToken({ 
        	  	auth_code: oAuth.authCode
        	  }, startPageTaskList);
        
    } 
    else if (oAuth.requestStatus == oAuth.status.ERROR) 
    {
    	console.log("Error: oAuth Processing");
    } 
    else {
        // do nothing I guess!
    }
}

function startPageTaskList() {
    $.mobile.changePage("#page-tasklist", {
        transition : "none",
    });
}

So far we have completed the authorization process. However, what if the user authorizes the App, closes it, and then relaunches it. With our current approach, the user would have to reauthorize the app again. Fortunately liquid.helper.oauth has a method isAuthorized() which returns true if the App is already authorized, false otherwise. So, we simply add that logic in startApp():

/**
 * Start the App
 */
function startApp() {
   var oAuth = liquid.helper.oauth;
	
    $("#access-code").click(function(event) {
        liquid.helper.oauth.authorize(authorizeWindowChange);
        event.preventDefault();
    });

    
    if (oAuth.isAuthorized()) {
    	/* Start Page TaskList */
    	startPageTaskList();
    }
}

7. Setting up the Task List Page (optional)

At this point, authorization is complete. I simply decided to add another extra page which lists all the tasks of the user. We
would like to check on the start of page if the user is Authorized is not. If the user is not authorized, we simply direct to the Home (Unauthorized page). Also add listeners for the 2 menu buttons (refresh and signout). Also, we want to populate the page with the Tasks as well (which is achieved by populateTaskList). So, we add the lines in gtaskdemo.main.js.

(function() {

	$('#page-tasklist').live('pageshow', function(event) {
		
		if (!liquid.helper.oauth.isAuthorized()) {
			goHome();
			return;
		}
		
		$('#btn-refresh').click(function(event) {
			populateTaskList();
			event.preventDefault();
		});
		
		$('#btn-hide-error').click(function(event) {
			
			$('#qt-listview-error').hide();			
			event.preventDefault();
		});
		
		/* populateTaskList on page init */
		populateTaskList();
	
		/**
		 * Add the listeners for each of the components
		 * Listeners are for:
		 * - Title bar refresh btn (#head-menu-refresh)
		 */
		$('#head-menu-refresh').click(function(event) {
		    populateTaskList();
		    event.preventDefault();
		});
		
		
		$('#head-menu-signout').click(function(event) {
		    liquid.helper.oauth.unAuthorize();
		    goHome();
		    event.preventDefault();
		});
	
	});
})();

8. Putting it all together

So the entire code for the qtaskdemo.main.js looks like:


$(document).ready(function() {
	
	/* startApp after device ready */
	document.addEventListener("deviceready", startApp, false);
});


/**
 * Start the App
 */
function startApp() {
	
	var oAuth = liquid.helper.oauth;
	
    $("#access-code").click(function(event) {
        liquid.helper.oauth.authorize(authorizeWindowChange);
        event.preventDefault();
    });

    
    if (oAuth.isAuthorized()) {
    	/* Start Page TaskList */
    	startPageTaskList();
    }
}


function startPageTaskList() {
    $.mobile.changePage("#page-tasklist", {
        transition : "none",
    });
}



function authorizeWindowChange(uriLocation) {
    //console.log("Location Changed: " + uriLocation); 
	var oAuth = liquid.helper.oauth;
	
	// oAuth process is successful!	
    if (oAuth.requestStatus == oAuth.status.SUCCESS) {
        var authCode = oAuth.authCode;

        // have the authCode, now save the refreshToken and start Page TaskList
        oAuth.saveRefreshToken({ 
        	  	auth_code: oAuth.authCode
        	  }, function() {
        		  startPageTaskList();
        	  });
        
    } 
    else if (oAuth.requestStatus == oAuth.status.ERROR) 
    {
    	console.log("Error >> oAuth Processing");
    } 
    else {
        // do nothing I guess!
    }
}


/**
 * Populates the list of Tasks
 */
function populateTaskList() {
	$.mobile.showPageLoadingMsg("a", "Loading Tasks...");
	
	/* hide all the request Info blocks/divs */
	$('.request-info').hide();
	
	liquid.model.tasks.getList(function(data) {
        $('#qt-listview-tasks').empty();
        
        console.log(JSON.stringify(data));
        
        /* check if there's an error from server, then display
         * error and retry
         */
        if (data.error) {
        	console.log('Unable to load Task List >> ' + data.error.message);
        	$.mobile.hidePageLoadingMsg();   
            return;        	
        }
        
        /* if there are no elements in it, then
         * display the info message, and return */
        if (!data.items) {
        	$('#qt-listview-info').show();
            $.mobile.hidePageLoadingMsg();        	
            return;
        }
        
        
        for (var i = 0; i < data.items.length; i++) {
        	
        	var item = data.items[i];
        	
        	$('#qt-listview-tasks')
        		.append('
  • ' + item.title + '
  • '); } $('#qt-listview-tasks').listview('refresh'); $.mobile.hidePageLoadingMsg(); }); } function goHome() { $.mobile.changePage("#page-unauthorized", { transition : "none", reverse: false, changeHash: false }); } (function() { $('#page-tasklist').live('pageshow', function(event) { if (!liquid.helper.oauth.isAuthorized()) { goHome(); return; } $('#btn-refresh').click(function(event) { populateTaskList(); event.preventDefault(); }); $('#btn-hide-error').click(function(event) { $('#qt-listview-error').hide(); event.preventDefault(); }); /* populateTaskList on page init */ populateTaskList(); /** * Add the listeners for each of the components * Listeners are for: * - Title bar refresh btn (#head-menu-refresh) */ $('#head-menu-refresh').click(function(event) { populateTaskList(); event.preventDefault(); }); $('#head-menu-signout').click(function(event) { liquid.helper.oauth.unAuthorize(); goHome(); event.preventDefault(); }); }); })();

    9. Final Thoughts

    The above was a rough demonstration of how to use google oauth on phonegap with childbrowser. For a production, it is better to combine/merge the Javascript file into 2 or 3 files, and also JS library such as backbone.js may be considered to avoid messy JS code

    Download from Github

    Download & Install App

    Check Database exist fails android sqlite3openv2

    I developed an android application where I shipped an sqlite database with the application. The Sqlite is initially stored at assets folder, and then copied to the proper directory (/data/data/[package]/databases/[db-name]) upon launch. I had the following piece of code for checking the existence of a database.

    	public boolean databaseExist()
    	{
    		SQLiteDatabase checkDB = null;
    		try {
    			checkDB = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
    			checkDB.close();
    			return true;
    		}
    		catch(SQLiteException e) {
    			// Log an info message stating database doesn't exist.
    		}
    		
    		return false;
    	}
    

    Even though I was catching an exception, however, I was still getting the error message (sqlite3_open_v2 failed):

    sqlite3_open_v2("...',  &handle, 6, NULL) open failed
    

    Doing some googling shows that its a popular problem.

    Finally, I tried an old school java code with java.io.File, and then it seemed to work fine. So I modified the code like below:

    public boolean databaseExist()
    {
    	File dbFile = new File(DB_PATH + DB_NAME);
    	return dbFile.exists();
    }
    

    And, it works very well, I also noticed this appears to be somewhat faster than the previous openDatabase approach

    I hope this helps someone out.

    DFA / NFA to Regular Expression (without using GNFA)

    The general approach to get a Regular expression (regex) from DFA/NFA is to convert a DFA to a GNFA by reducing states. Read more on wikipedia. However, for not too complicated DFA, it is often easier to get a regex from NFA instead of going through GNFA (which btw makes regex big).

    For regex, the patterns that contains a substring, it is easy to write a regex. Lets say you have to write a regex for all strings that contains the string 010, where: ∑ = {0, 1}. Such cases are straight-forward. A regex would be:

    (0 U 1)* 010 (0 U 1)*
    

    But consider the following:

    Write a regex for strings that does not contain substring 010. Where ∑ = {0, 1}.

    It is not straight-forward. For such case, it is often easier to draw a DFA, then make NFA, and then get regex from it (without using GNFA approach).

    Here’s one approach to solve such problem. The following steps are used:

    1. Create a DFA that contains the substring 010
    2. Complement the DFA and make the NFA from it (to get a NFA that does not contain 010)
    3. Get the Regex from it

    Step #1: Creating the DFA that contains 010


    Step #2: Complement the previous DFA (i.e. change the old accept state as reject state, and non-accept state as accept state). And, remove the unnecessary ‘d’ state to get the NFA.


    Step #3: Now, we write the regex. There are several ways to do it, however, I use the following technique:

    – From start state (a): You can either take 1 or 00*11 to come back to the start state (a), and repeat it.

    So the first part of regex looks like following:

    (1 U 00*11)*
    

    – After reading the previous input sequence, you are still in start state (a). You can either end it there (as a is an accepting state). OR you can read 00* to get to State b to end it there, OR you read 00*1 and reach State c and end it there.

    So the complete regex looks like the following:

    (1 U 00*11)* (ε U 00* U 00*1)
    

    Thats about it. I hope it helps out someone :).

    Good Javascript Practices

    I decided to share some of the best practices of Javascript which I have learned from various blogs and books over the years. Although, I used Javascript back in 2003, I didn’t learn Javascript properly and just happened to use it for some fun webpages. However, as I revisited Javascript from 2007 onwards, I have started to use Javascript as an OO language and try to follow the best practices.

    Below are a list of 7 such Best Practices of Javascript:

    #1. Making Scripts External

    Making Javascripts External keeps it separate from the HTML file and as such is easier to maintain. Also one can set an expiry date for the external js file (In Apache, expiry can be set using mod_expire), and thus the page loading would be much faster.

    
    

    #2. Placing Scripts at the bottom

    Javascript code should be placed at the bottom of the page right before the ending </body> tag. Script blocks parallel downloads, hence keeping them at the top of the page would block the downloading of the page until the Javascript has been completely downloaded. So, its best to put Javascript at the bottom. Read more on Yahoo Developer Network

    So, typically scripts code block should be like below:

    
    < /body>
    

    #3. Avoiding too many Global Variables

    Instead of declaring multiple variables like below:

    var name = "Alexander";
    var gender = "male";
    var phone  = "416-999-9999";
    
    function updatePerson(n,g,p) {
    name = n;
    gender=g;
    phone=p;
    }
    

    Its better to re-organize variable and declare it like below in JSON Format:

    var Person = { 
     name: "Alexander", 
     gender: "male", 
     phone: "416-999-9999"
     update: function(info) {
     // info is a object containing name, gender, phone. 
     // process accordingly
     }
    };
    

    #4. Using Shortcuts

    Instead of declaring arrays like:

    var nameList = new Array();
    nameList[0] = "Alif"; 
    nameList[1] = "Alexander";
    nameList[2] = "Totem";
    

    Its better to use:

    var nameList = ["Alif", "Alexander","Totem"];
    

    Using ternary operator instead of if-else, depending on circumstances. (Some developers I have worked with on projects, have asked me not to use ternary operator as it complicates the readability of coding, so this point is debatable, and hence its upto individuals to decide).

    Instead of using:

    var x;
    if(cond == true) {
    x = "A"; 
    } else {
    x = "C";
    }
    

    Use:

    var x = cond ? "A" : "C";
    

    #5. Unobtrusive Scripting

    Instead of using inline Javascript, the Javascript code should be separated out from the HTML codes. From the Javascript, a specific tag/node should be grabbed to attach an event or do something else. For example, instead of using:

    Show Users
    

    Grab the element/node from the Javascript separately and attach the function with it:

    document.getElementById("showUsers").onclick = function() {
    showUsersList();
    return false; // or event.preventDefault() for non-IE;
    };
    

    #6. Progressive Enhancement

    Progressive Enhancement technique starts with offering a minimal level of user experience that all browsers will be able to display. Then, on top that, more advanced functionality are built to enrich user experience.

    For example, when the following code block is used:

    Show Users
    

    It is assumed that the visitor/user has a Javascript enabled Browser.

    But, what if the user doesn’t have Javascript enabled? and even worse, what if the user doesn’t know or do not have permission to “enable/allow” javascript?. For that case, the above script will not work, and non-JS users will not be able to see the functionality of “Show Users”.
    So, the following technique should be used:

    Show Users
    

    From the Javascript, the element should be grabbed and onclick event should be attached as described on #5. And a functionality of ‘url.php?s=all’ should be added to the page to ensure non-JS users gets to see the same feature at a degraded user experience.

    #7. Minify Javascript

    Javascript should be minified whenever applicable (minify means to strip out comments and white spaces, carriage returns etc.). There are some great tools online to minify a Javascript.

    My favorite one is YUI Compressor, an online version is here. Minifying scripts reduces the file size significantly (depending on your code). As a rough example, jQuery 1.4 actual is 155kb, whilst minified version is ~74kb.

    Thats about it !.

    Please note that I am still learning as time progresses, as a result, if you find any of the above practices as ‘not the best practice of JS’, then notify me.

    For more reading, please visit the following resources:

    Opera Developers | Javascript Best Practices

    ezMark: jQuery Checkbox & Radiobutton Plugin

    ezMark is a jQuery Plugin that allows you to stylize Radio button and Checkbox easily. Its very small (minified version is ~1.5kb) compared to other similar scripts. It has been tested and works on all major browsers (IE 6/7/8, Firefox, Safari, Chrome) and it gracefully degrades.

    It has the following Method:

    $('selector').ezMark( [options] ); 
    

    Basic Usage:

    Include the CSS file:

    
    

    Then include the jquery.js file and the plugin file:

    
    
    

    Write the HTML tags normally, as you would type them

    
    

    Now, call the following method in your JS (to apply on all checkbox & radiobutton across the page):

    $('input').ezMark();
    

    Likewise, you can also use custom selectors for checkbox or radiobuttons:

    // to apply only to checkbox use:
    $('input[type="checkbox"]').ezMark();
    
    // for only radio buttons:
    $('input[type="radio"]').ezMark();
    

    Parameters:

    options parameter accepts the following JSON properties:

    Parameter’s (JSON) Properties: Explanation/Details of the Property
    checkboxCls The Checkbox Class as per declaration on CSS.
    checkedCls The Checkbox Class on Checked State
    radioCls The Radio button’s Class as per CSS
    selectedCls The Radio Button’s Class on selected State

    To customize the default checkbox/radiobutton image, change the background image (checkbox-black.png/radio-black.png) and CSS (ez-checkbox/ez-radio) and (ez-checked/ez-selected) accordingly.

    Customized Usage:

    After you have customized/created your own css class. Simply call the ezmark method like below:

    // to apply only to checkbox use:
    $('input[type="checkbox"]').ezMark({
     checkboxCls: 'your-default-checkbox-class-name' ,
     checkedCls: 'your-checkbox-class-in-checked-state'
    });
    
    // for only radio buttons:
    $('input[type="checkbox"]').ezMark({
     radioCls: 'your-default-radiobutton-class-name' ,
     selectedCls: 'your-radiobutton-class-in-selected-state'
    });
    
    

    View Demo

    Download from Github

    Download from jQuery Server

    Canada Postal Code & US Zip Code Regex

    I decided to write a simple Canadian Postal Code and US Zip Code Regex. There are already good ones on the web, like here for example. But, I decided to write my own to make it slightly more accurate.

    Canadian Postal Code Regex

    Canada’s Postal Code format is ‘A1A 1X1’ or ‘a1a1x1’. Its made up of two parts. Forward Sortation Area (FSA) and Local Delivery Unit (LDU). Read more on wikipedia. The letters D, F, I, O, Q, or U are not used on postal Code. So, the Postal Code would be roughly (note that when applying the regex, the case-insensitive (i) modifier should be used):

    US Zip Code Regex

    US Zip Code format seems to be straightforward. It needs to have 5 digits followed by an optional 4 digit. So, the Zip Code would be roughly:

    Merging US & Canadian Postal Code

    Now, both Postal Code and Zip Code could be merged, using an OR (|) operator, into one regex:

    Send Email from Terminal with file attachment using mutt

    Sometimes I run into situation where I need to email a file, and I am lazy to open a browser, login to my email account, and then attach file and send it.

    If you are like me, then mutt is a neat command for sending emails from terminal quickly =P.

    The code looks like below:

    echo "email body" | mutt emailaddress@gmail.com -s "email subject" -a "file-to-be-attached"
    

    So, if I need to email a file code.zip (in the same directory), I would use the following:

    echo "Code for Project" | mutt email@gmail.com -s "project c" -a "code.zip"
    

    Check out man mutt for more information. Hope it helps out.