10 Best Practices for Developing JavaScript Widgets

I’d like to offer updated, practical recommendations to developers who are building widgets. Widgets are essentially a mini-application within your web app that provides specific functionality, for example the map switcher widget included as a screenshot in this post. In May 2010, the OpenAjax Alliance published the Metadata 1.0 specification to define a set of standards for widgets. This was a great start, however in the last two years there have been some significant advancements which render some potions of this spec aged and in need of updating. And, since I’ve run across quite a few widgets in the last several years that seem to consistently have certain problems, this post attempts to provide guidelines for anyone looking to make their widgets significantly more re-usable.

State and Display Properties. All widgets need to make their State accessible via public setters and getters which, at a minimum, includes visible/not visible. Getter properties simply offer developers a way to determine whether the widget is visible or not. So, I add that there also needs to be a way to set the State as visible/not visible. Advanced widgets also need control over inline and block positioning.

Persistence. Widgets need the ability to maintain certain aspects of State and persist between app or browser restarts. For example, primary text fields should be persistent after an end user restarts the browser. The corollary is that there also needs to be functionality to clear persisted data.

View Transitions.Widgets must also persist fully and correctly through view transitions. This has to do with mobile web apps designed to mimic native app functionality that occurs when a user switches between different views within the same app.

Auto-resize. Widgets must auto-resize to their container. The sheer popularity of smartphones and tablets is driving this requirement. For example, when a phone is turned on its side, a widget should automatically fill the new dimensions of the container. If your widget doesn’t auto-resize it will significant limit it’s marketability for mobile devices.

Events. Widgets must have a clearly defined life cycle that is accessible via events. At a minimum this includes when the widget is initialized, after specific actions, and upon an errors. Events need to fire when they are expected to and not before or after. This is especially important for mobile apps where event bubbling, or even CPU/Memory overload, may cause delays in the callback. A properly wired event will fire as expected 100% of the time.

Namespace. Widgets must use name space separation so as to not cause variable and function conflicts. For example, don’t name your widget Class “AppWidget”, a best practice alternative is to call it “com.mycompany.AppWidget”. This also includes protecting your global variables to reduce “leakage” outside of your Classes.

Public Properties and Methods. Only properties and methods that are internal should be kept private. All other properties need to be public.

Documentation and Code Comments. This is an age-old problem. Here’s another way to look at it: in many cases, lack of documentation costs other developers significant lost time, effort and money. The one to two minutes a developer saves by not writing a comment could cost another developer hours, days or weeks of frustration.

Cross-platform compatibility. Clearly document which platforms the widget was tested on. If you haven’t been testing your widgets cross-platform then you need to be. If you only designed your widget for a specific browser, that’s okay as long as it’s documented. At a minimum, truly cross-platform widgets will have been tested on the latest production versions of Firefox, Chrome, iPad/iPhone (Safari), Android (native) and IE.

Debugging. If your widget uses an obfuscated JavaScript library, consider providing either a debug version, or a version that includes public (debuggable) methods alongside the private and obfuscated ones.

Common pitfalls with JavaScript scope

I’m a firm believer that JavaScript’s flexibility is great for small projects with a single developer. But, its flexibility can become a seriously liability in medium to large projects if not managed properly. The ‘scope’ of a variable can cause some of the largest headaches for developers. And, in big projects tracking down an improperly scoped variable will take time, and it will try your patience.

In most cases, variables in JavaScript have two scopes: global and private. Scope is the context in which a variable is used. A global variable is defined outside of any function() in a JavaScript text block, and it can be accessed from inside any function(). Any change to a global variable will be reflected where ever else that variable is used. A private variable is defined only inside a function() and private variables are not accessible by other function()’s by definition. So a change to a private variable only changes it’s own value and doesn’t affect any other variable outside the scope of the function(). If you are new to scope, you may want to re-read this paragraph twice.

If you’ve been using JavaScript for any amount of time you’ll discover that simply misplacing a “var” statement in front of a variable causes it become global in scope. And, if you happen to already have a global variable somewhere else by the same name then these values can overwrite each other. If you are working on a project with thousands of lines of code and multiple .js libraries then your problems can get larger. I’ve accidentally deleted a “var” keyword in several cases and then spent a considerable head banging tracking it down.

To demonstrate these pitfalls, I’d rather show you in code what the problems are. Hopefully by reading this, and understanding a bit more about scope and by using best practices, you’ll avoid the common pitfalls as much as possible.

Scenario 1– properly defined private variables. This scenario demonstrates best practices for defining local variables within a function(). You can have privately scoped variables with the same name as global variables because of JavaScript obeying adherence to scope. Click here to try out this scenario.

  //This sets a global variable scope
  var color = "blue";

  function init() {

	var me = new Person("Andy");
	alert(" Private scoped name: " + me.name +
	   "\r\n Private scoped color: " + me.favoriteColor +
	   ", \r\n Global scoped color: " + color
	);

  }

  function Person(myname)
  {
	  //This creates a privately scoped variable
	  //Does not affect or change the globally scoped
	  //variable of same name
	  var color = "red";

	  //myname exists within the private scope of the function
	  //color is privately scoped
	  this.name = myname;
	  this.favoriteColor = color;
  }

Scenario 2 – improper use of a global variable. This scenario demonstrates forgetting to set “var” on the variable color. The value of the global variable named color is changed. If you use this pattern for manipulating global variables you are asking for trouble as your project grows larger. Click here to try out this scenario.

  //This sets a global variable scope
  var color = "blue";

  function init() {

	var me = new Person("Andy");
	alert(" Private scoped name: " + me.name +
	   "\r\n Private scoped color: " + me.favoriteColor +
	   ", \r\n Global scoped color: " + color
	);

  }

  function Person(myname)
  {
	  //This changes the globally scoped
	  //variable of same name
	  color = "red";

	  //myname exists within the private scope of the function
	  //color exists within the global scope of the application
	  this.name = myname;
	  this.favoriteColor = color;
  }

Scenario 3 – this scenario shows the best practice for passing in a global variable to a function(). By passing the global variable into someColor you protect the scope of it within the function(). Click here to try this scenario.

  //This sets a global variable scope
  var color = "blue";

  function init() {

	var me = new Person("Andy", color);
	alert(" Private scoped name: " + me.name +
	   "\r\n Private scoped color: " + me.favoriteColor +
	   ", \r\n Global scoped color: " + color
	);

  }

  function Person(myname, someColor)
  {
	  //myname exists within the private scope of the function
	  //someColor is a private scope, but it inherits the value
	  //of the variable passed to it.
	  this.name = myname;
	  this.favoriteColor = someColor;
  }

References:

Scope in JavaScript by Mike West
Variable Scope for New Programmers by Jonathan Snook