I’ve been using Actionscript 3 as part of my game programming class for almost a month now. Coming from a C++ background, there were a lot of things I didn’t like about it the first day. Some of those things I’ve grown to like; others…not so much.
Things I like about Actionscript:
The import system
In AS3, libraries are built on the “one class, one file” paradigm. Each class must reside by itself in a file with the same name as the class. At first I thought that this was just a needless imposition; coming from C++ where every class can be in one file, it seemed silly to me that a language would restrict the freedom of the programmer in that way. As it turns out, this is possibly the thing I like best about the language.
AS3 classes are organized in “packages”, in a similar way that C++ uses namespaces. A package is a folder that contains class files, and is usually named according to Java naming conventions; for example since my website is www.jacobalbano.com, my classes are in the folder hierachy com/jacobalbano/, resulting in the package com.jacobalbano.
The beauty of this system is that the source is always exactly where you expect it to be, as opposed to in C+, where hunting down a class definition in a source tree can be a pain. Additionally, there is no concept of definition and implementation, since each class is contained in its own file.
Getters and Setters
In C++ classes, modifying or accessing private member variables requires two functions, usually defined as SetVariable() and GetVariable(). Actionscript streamlines this pattern with getter and setter functions, which take this form:
public function set ID(param:uint):void
this._ID = param;
public function get ID():uint
This allows access to the private variable _ID like this:
var Foo:uint = myClass.ID;
myClass.ID = 42;
The beauty of this system is that it allows the convenience of a public variable without the problem that setting a variable is often not enough. For example, to start an animation with a class in C++, you’d have to use the following style:
The PlayAnimation() function in the above case might stop the current animation and start the one specified in the function’s parameters. In Actionscript, the procedure would be the same, but the interface is different:
myClass.animation = "walking";
In this case, the setter function would perform the same actions as PlayAnimation(), but in a cleaner way, in my opinion.
In addition, member variables can be made read-only or write-only by eliminating one of the functions.
Super() and function overrides
When extending a class, functions of the base class can be overridden with a function implemented in the derived class. In addition, the base functions can be accessed with the super keyword. For example:
public override function update():void
The base function update() is overridden by the derived class, but it is preserved so we can still access it.
I’ve tended to shy away from using function pointers in C++, even going as far as to embed a Lua interpreter to bind actions to a GUI. Actionscript makes it easy to store and use function pointers with the Function type.
Things I dislike about Actionscript:
This is a big one. Actionscript’s syntax (and that of any other ECMAscript-derived language) is close enough to C that I didn’t have a hard time picking it up, but some of the changes are clumsy. I would say the biggest offender is variable declaration.
var variableName:variableType = new variableType;
This introduces another gripe I have…
Actionscript is a GC language.
I know it isn’t cool to manage memory yourself anymore, but I like the control it gives me over the program. Actionscript is very picky about what may and may not be deleted.
Actionscript doesn’t have Enums
Enums are a fundamental part of my programming workflow. I can’t think why they wouldn’t exist within the language, but they don’t.
Actionscript has limited support for class templates
The only class that has template support is the Vector class, and templates aren’t exposed for implementation into custom classes. I managed to fake it a bit, but it’s far from preferable.
Actionscript is platform-specific
I wouldn’t even consider learning a language if it didn’t run on Linux. Fortunately, Actionscript can be developed, compiled and run on Windows, Mac and Linux natively. In this case I refer to the fact that Actionscript only really runs on the Flash or Air VMs, one of which has been discontinued on Linux, and neither of which has particularly good performance.
Actionscript allows developers to be lazy
The Flex compiler won’t complain if you miss a semicolon. Forgot to define a return type for a function? Don’t worry, the compiler doesn’t care. I compile my C++ with every possible warning enabled, so this is another thing that bothers me a lot.
Actionscript doesn’t support overloading
In Actionscript you can only have one function per function name, even if the parameters are different between declarations. This means that you can only have one type of constructor, which gets awkward. In addition, operator overloading is not allowed, so if you want to trace your custom class you’re out of luck.
There’s a lot to like about Actionscript, but it has its fair share of ugly features and anti-features. If not for Flashpunk and FlashDevelop I would have given up on it by now. Ironically, Flashpunk is the best framework I’ve ever used, period, and Flashdevelop is the best IDE I’ve ever used, period. At least they make the language bearable while I have to use it.