Dec
2

Useful FDT Templates Paul

Actionscript, Flash

One of the most useful features in FDT is templates. It’s something that FlashBuilder is still missing, and while templates are pretty simple in concept, they are one of the key features that keep me using FDT over its free counterpart.

Templates are basically code completion shortcuts that allow you to generate boilerplate code quickly and easily. This can be anything from generating a for loop up to generating entire classes. They are especially useful when you’re under that tight deadline before your presentation on Mobile Broadband deals or trying to realize your next great game idea. In this article I’ll share with you some of the more useful ones I’ve written that speed up my dev time significantly. Not only do they speed things up, they can help enforce consistency and reduce the little typos that introduce errors and bugs.

A Better Trace

One of the pre-packaged templates in FDT is systrace, which just generates a call to the trace method and puts your cursor in the message body. Surely we can do better than that!

mt:

 trace("${enclosing_type}.${enclosing_method}(",[${enclosing_method_arguments}],")");

This template will generate traces that look like:

trace("MyClass.saveUser(",[userid, userdata],")");

By wrapping the method arguments in an array, the contents of the array will each have their toString methods called and automatically be concatenated in to one string. This is a huge time saver, and allows you to do a quick-and-dirty check of what’s flowing in to your methods.

Generating Event Listeners

cl:

addEventListener( Event.COMPLETE, ${cursor} );

ml:

addEventListener( MouseEvent.CLICK, ${cursor} );

After typing in the event handler method’s name, I follow up with CTRL+1, which will auto generate the method and automatically create the correct event parameter based on what type of event listener you added. You can add similar templates to take care of all the common events you work with.

Declaring Event Names

More than once I’ve been bitten by copying and pasting an event name declaration and ending up with code that looks like this:

public static const ADD_USER : String = "addUser";
public static const REMOVE_USER : String = "addUser";

Any time a user is added, the handlers for both ADD_USER and REMOVE_USER will get fired!
et:

public static const ${param} : String = "${param}";

Using this event name template makes sure duplicate names will never happen by ensuring that the string constant behind your event parameter is unique, since it shares its name with its compile-time checked constant

Public/Private/Protected Functions

puf:

public function ${cursor}() : void
{
}

ptf:

protected function ${cursor}() : void
{
}

prf:

private function ${cursor}() : void
{
}

These are pretty obvious, and it’s strange that they don’t ship with FDT by default.

Singletons

This generates a singleton based on a technique described over at Daniel Rinehart’s blog.
singleton:

package ${enclosing_package}
{
	public class ${enclosing_type}
	{
		public static var instance :  ${enclosing_type};
		public static function getInstance() :  ${enclosing_type}
		{
			if( instance == null ) instance = new  ${enclosing_type}( new SingletonEnforcer( ) );
			return instance;
		}

		public function ${enclosing_type}( pvt : SingletonEnforcer )
		{
			if (pvt == null)
			{
				throw new Error("${enclosing_type} is a singleton class, use getInstance() instead");
			}
			${cursor}
		}
	}
}
internal class SingletonEnforcer {}

These are just a few ways to use templates to speed up your day-to-day workflow. If you have any of your own, please, leave them in a comment!

You can download the templates here.

To install on Windows open FDT and go to: Window > Preferences
In the preferences window: FDT > Editor > Templates > Import

On a Mac open FDT and go to Application Menu (FDT 3 or Eclipse) > Preferences
In the preferences window: FDT > Editor > Templates > Import

This entry was posted on Wednesday, December 2nd, 2009 at 9:20 pm and is filed under Actionscript, Flash. 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.