Aug
5

Default Values with Null Coalescing Paul

Actionscript

The null coalescing operator is a rarely used but useful operator for initializing your objects.

Often times you’ll have a method that accepts optional arguments. If your arguments are typed to non primitive objects (anything other than Strings, Numbers or Booleans), you’re usually stuck setting their default value of your parameter to null. This makes for a lot of messy code that looks like this:

private var __friends : Array;
public function initializeUser(friends : Array = null) : void
{
	if(friends == null)
	{
		__friends = new Array();
	}
	else
	{
		__friends = friends;
	}
} 

If our poor user doesn’t have any friends yet, we can call initializeUser() without specifying any parameters, and internally we dont need to worry about handling the null case every time we need to use the __friends variable because its initialize with an empty array.

These optional parameters are a big part of writing clean public interfaces to the rest of your program, but as you can see the code to handle them gets quite bloated. The Null Coalesing operator will help us fix that. Lets look at an example and then dissect how it works.

private var __friends : Array;
public function initializeUser(friends : Array = null) : void
{
	__friends = friends || new Array();
} 

Slick. But what is going on here?

In essence, the Null Coalescing operator in AS3 isn’t a different type of operator at all. Its a logical ‘or’ operator, just like you’d use in an if statement. The interpreter evaluates a series of OR clauses in the if statement one at a time, and as soon as it finds a clause that doesn’t evaluate to false (or null, or undefined, or NaN…), it returns the value of that clause. The Null Coalescing operator takes advantage of that fact.

In the example above, ‘friends’ evaluates to null if we don’t pass an array to initializeUser. The interpreter moves on to the second clause ‘new Array()’. Since this does return something other than false, we get back this value and store it in our __friends variable.

Because we can chain or statements, we can take this a step further:

private var __friends : Array;
public function initializeUser(friends : Array = null) : void
{
	__friends = friends || 
			__usersMom.friends || 
			FriendRepository.getDefaultFriends() || 
			new Array();
} 

In this example if you don’t have any friends, we try to use your mom’s friends. If she doesn’t have any friends, we get some default friends from a static class. If there aren’t any friends in the repository we finally fall back to the failsafe of a new empty Array.

Another great use for the Null Coalescing operator is in your UI controls. If you wish to have an optional sub-component like say an error highlight in a text input control you can ensure that you’ll always have a control to work with:

public var errorHighlight_mc : MovieClip;
public ValidatableInput()
{
	errorHighlight_mc = errorHighlight_mc || new MovieClip();
} 

Unfortunately, you do sacrifice a little readability by Null Coalesing. However, if you educate your team, comment your code and encapsulate properly this technique should help you cut down on keystrokes.

This entry was posted on Thursday, August 5th, 2010 at 11:31 am and is filed under Actionscript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.