Archive for the 'javascript' Category

Real-world example of why global variables are bad

We all know that global variables are bad. Sooner or later, someone is going to pick the same name for a variable as you do, and when that happens, *poof!* your code doesn’t work anymore.

Unfortunately, not every programmer understands why globals are bad. This is a write-up of a rather spectacular bug that I was asked to look into during my glory days at InternetBroadcasting. It involved tracking down a conflict between an advertisement on the same page as a Microsoft VirtualEarth map control. Needless to say, when we contacted Microsoft to make them aware of the problem, they were less than responsive… it was easier to talk to the small ad serving company to get them to fix their part, and that was ultimately the route that the project manager decided to take to fix the problem.

What the problem was

A bug was reported wherein a Microsoft VirtualEarth map would not display on certain web pages that contained a certain advertisement.

How it happened

In summary, the problem occurred because two third-party JavaScript files were included on the same web page, and both scripts contained code which did not follow the extremely important best practice of limiting the use of global variables.

JavaScript code included from Microsoft’s VirtualEarth library defined a global variable named “g.” JavaScript code from ContextWeb sourced in for an advertisement also defined a global variable named “g.”

There can only be one global variable named “g” on any given web page. In JavaScript, the last value assigned to a variable is always the current value. Here is the order of events as they happened on the websites that experienced this problem:

  1. A script tag loads Microsoft’s VirtualEarth library, wherein a global “g” is defined.
    /* This runs in the global space, i.e. outside of any function or object */
    function g(a){
            return new Gimme.object(Gimme.query(a))
    }
    

    (as found in http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2)

  2. A script tag loads in ContextWeb ad code, wherein the global “g” is overwritten to a new value.
    /* This runs in the global space, i.e. outside of any function or object */
    var g=''; /* Here is the initial overwrite... many follow below... */
    var k='';
    var j='';
    var tw=w;
    try {
    	k=d.location;
    	if(k==top.location){
    		g=k;
    	} else {
    		while(true){
    			g=tw.document.location;
    			.............
    

    (as found in http://tag.contextweb.com/TagPublish/getjs.aspx?action=VIEWAD&cwrun=200&cwadformat=728X90&cwpid=501657&cwwidth=728&cwheight=90&cwpnet=1&cwtagid=25722)

  3. A script initializes the VirtualEarth map, which attemps to use the variable “g.” Having been redefined, the variable does not contain what VE code expects it to, and the map cannot be created.

Why it happened

A typical problem when integrating two third-party JavaScripts into the context of a web page is a global variable conflict. A global variable is one that is available to all scripts running in a particular context. In a web browser, all global variables are members of the object called “window,” and can be referenced simply by typing the name of the variable. Because JavaScript allows one to rewrite or delete variables created in this space, one can define and rely on a global variable and have that variable overwritten by another script which naively uses the same variable name.

Many of today’s most respected JavaScript developers recommend against the usage of global variables (for example, see Douglas Crockford’s take on the subject). Unfortunately, the usage of globals is rampant. The reasons for this are many, including that:

  • global variables are convenient

    It is extremely easy to use a global variable, and many online tutorials unfortunately contain examples wherein globals are used. Many first-time programmers are taught to use a procedural programming style with JavaScript and to write just enough code to get the job done (without namespaceing or context-checking).

  • JavaScript programmers are relatively inexperienced

    JavaScript has only recently emerged as a programming language for web applications; in the past, it has been used for very simple tasks on web pages. Now that it is being used to write full-blown applications, problems such as global variable conflicts become extremely serious. Lacking understanding of best practices, even programmers who are fluent in other languages can make terrible mistakes in JavaScript.

  • the language has some confusing syntax that promotes the use of global variables
    • In the global space (i.e., outside of any function or object) any variable defined with the var or function keyword is global. This is what happened in the case of VirtualEarth and ContextWeb. For example:

      function f() { ... }
      var t = 4;
      alert(typeof window.f); /* "function" */
      alert(typeof window.t); /* "number" */
      
    • Inside a function, the declaration of a variable without the keyword var makes the variable global. Programmers new to JavaScript often are not aware of this. For example:

      
      function myFunc() {
      	/* LOCAL VARIABLES */
      	var myVariable = 3;
      	var myFunction = function() { return 3; }
      
      	/* WHOOPS - GLOBAL VARIABLES */
      	myOtherVariable = 4;
      	myOtherFunction = function() { return 4; }
      }
      myFunc();
      
      alert(myVariable); /* undefined */
      alert(myOtherVariable); /* 4 */
      alert(window.myOtherVariable); /* 4 */
      
      alert(myFunction()); /* error, myFunction is not defined */
      alert(myOtherFunction()); /* 4 */
      

How it could have been avoided

  • Namespacing

    In JavaScript, the practice of namespacing is simply creating one global variable that is unlikely to create a name conflict (such as the organization name of a company in capital letters). All variables are then stored within that object, and can only be accessed through the object. For example:

    var IBSYS = {};
    IBSYS.foo = 3;
    alert(foo); /* undefined */
    alert(window.foo); /* undefined */
    alert(IBSYS.foo); /* 3 */
    

  • Scoping

    Javascript allows the creation and immediate execution of unnamed (i.e., anonymous) functions. Functions have their own local scope, and thus any local variables defined in an anonymous function will not conflict with any globals or code in any other scope. For example:

    (function() {
    	var t = 3;
    	alert(t); /* 3 */
    })();
    alert(t); /* undefined */
    

PNG background-image support in IE6 – better late than never

My buddy Drew Diller (who is a freaking CSS genius) has come up with a clever solution to get CSS backgroun d-image support working in IE – including background positioning and repeating.  If you are tired of using AlphaImageLoader and want something easier, check this out:

http://www.dillerdesign.com/experiment/DD_belatedPNG/

It lets you use a PNG image as a CSS background-image property in IE6 without much fuss at all!

Enterprise JavaScript build system – Sysbliss

I’ll soon be working on a screencast for the JavaScript build module that Jonathan Doklovic wrote for his enterprise build system (which leverages Jira, Bamboo, Ivy, and Ant).  The JavaScript module takes advantage of JsUnit for unit testing, YUICompressor for compression and lint checking, and JsDoc Toolkit for source documentation.

I’ve also contributed a Flex build module to this system, and hopefully it will be put through the paces soon so that any major deficiencies can be exposed and worked out.

JSUnit XML to JUnit XML

We’re using JSUnit to run tests in our build system, and we’re using Bamboo to do continuous integration.  Unfortunately, the XML spit out by JSUnit is not recognizable as JUnit XML for the purposes of integrating into Bamboo. I wrote this XSL stylesheet to transform the JSUnit XML report into a JUnit XML report that Bamboo can parse.  It’s simple and inefficient, but hey, if you are looking at this, you probably just want to get the job done (like I did). 

View jsunit_to_junit.xsl

Type enforcement and argument validation in JavaScript saves time

Debugging JavaScript is hard enough – but it is especially annoying when you have spent a good amount of time tracking down a bug and it turns out to be a type error (i.e., your function expected a Boolean true but got a String “true”). Perhaps your value was implicitly cast to another type by some operation, or maybe you forgot to pass the right arguments to a method. In a statically-typed, compiled language (such as AS3 or Java), you would find out about bad arguments during compile time – and you’d probably get a nice, descriptive error message of what was going on.  Not so with JavaScript – in fact, because it is loosely and dynamically typed, you may not find out about your bad argument until you waste an hour or two tracking down a bug.

To avoid this sort of thing, you can throw your own errors when a method or constructor doesn’t get what it expects. The pattern is easy to get the hang of.  The very first time you get one of these errors, you will feel good about all this extra typing. 

/**
* A function that creates a new barf bag.
* @param {Boolean} zip          Whether this bag has a zipper or not.
* @param {String} bagType     Valid types: "paper"|"plastic"
* @param {String} vomitType  Valid types: "chunky"|"bile"|"watery"
*/
var VomitBag = function(zip, bagType, vomitType) {
   /* validate arguments */
   if(typeof zip != "boolean") throw new Error("VomitBag: expected zip to be Boolean");
   if(typeof bagType != "string" || !(/^paper$|^plastic$/.test(bagType)) throw new Error("VomitBag: expected bagType to be String and to be either 'paper' or 'plastic'");
   if(typeof vomitType != "string" || !(/^chunky$|^bile$|^watery$/.test(vomitType)) throw new Error("VomitBag: expected vomitType to be String and to be 'chunky', 'bile', or 'watery'");

   /* some code... */
}

This technique is not at all new to JavaScript. For example, Marijn Haverbeke talks about it briefly during a good writeup about error handling in Eloquent JavaScript: Error Handling – Eloquent JavaScript (that whole site is worth reading).

Next Page »