Coding Advice

If you have been following along with the Flex 2 announcements, you have noticed that Flex 2 will be using ActionScript 3. Flex 1.5 uses ActionScript 2. The benefits of ActionScript 3 are too numerous to name here, but I thought I’d give a couple of pieces of advice for coding Flex 1.5 in anticpation of moving to Flex 2.

This advice should get you used to coding in AS 3 “mode” and reduce the number of changes you will have to make.

Data Type Everything

Make sure everything has a data type. For example, change:

var n = 4;
to
var n:Number = 4;

If you are unsure of what type something should be, make it an Object.

The downside is that not everything can be easily typed. For example, while there is an Event type, once you identify a variable as being of that type you cannot add your own properties to it. Flex 2 will overcome that by providing many more pre-defined classes.

If you run into a problem, then just leave the data type off for now and fix it if you port the code to Flex 2.

Events

In Flex 1.5 you can conveniently dispatch events by writing Objects inline:

dispatchEvent( {type:”select”, code:”Something” } );

This will not be allowed in Flex 2. Instead, start writing the code like this:

var event:Object = new Object();
event.type = “select”;
event.code = “Something;
dispatchEvent(event);

Public and Private

In Flex 2 you will be required to identify the scope of variables and functions. Right now you can get away with:

var dp;
function initApp() {
dp = new Array();
}

In Flex 2, this should become:

private var dp;
private function initApp() : Void {
dp = new Array();
}

Bindings

In Flex 1.5 you can bind to variables without any special syntax. For example:

var dp:Array = [{name:”George”,age:18},{name:”Holly”,age:23},…];
<mx:DataGrid dataProvider=”{dp}” />

In Flex 2 you need to identify those variables which will be used in data binding by using the [Binding] directive.

But you can use [Binding] now and just get used to making the identification:

[Binding]
var dp:Array = … ;

The [Binding] directive is harmless in Flex 1.5.

Conclusion

I know these seem minor, but if you get into the habit now, you’ll be better off if you try and port your application to Flex 2.