ActionScript Programming Efficency and Best Practices
A key to good programming is getting the most out of
the smallest/most efficient amount of code.
The ActionScript language contains many tools and techniques that help minimize
the amount of code needed in a project.
As you look through the examples on this site,
it is important to note that they where not built with efficency in mind, but rather
"understandability"
The animated
"Hello World!" example uses a long switch statement to break each interval
of the Timer into separate visible parts.
This helps to express the point of the example; the Timer class can be used much like a timeline with frames.
However, a more efficient way to think about that code would be to
also take into account a situation where the words "Hello World!" would need/or want to be changed:
...
private var myWord:String = "Hello World!";
...
theTimeline = new Timer(500, myWord.length);
...
private function doTheAnimation(event:TimerEvent):void
{
theShowTyping.appendText(myWord.substring(Number(event.target.currentCount),
Number(Number(event.target.currentCount)+1)));
}
...
Using the above code is more efficient because the phrase "Hello World!" would be easier for anyone to change.
It also takes fewer lines of code than the "understandable" way.
As you get deeper into programming with ActionScript 3.0,
efficiency is one of the things you will need to be mindful of.
The other concept that is good to master is Object Oriented Programming.
I like to think of it as sort of creating your own little language.
You use Classes and sets of Classes, Interfaces and Namespaces to signalize and organize common behaviors and functions.
To Upper Case or not
camelCase is the term for the industry standard technique of Capatalizing the start of new words in a variable:
(for example: myText mouseDownHandler)
Using camelCase helps in phrase recognition.
But it can also get confusing keeping up with what is supposed to be capatalized,
especially when the programming language is using camelCase as well.
For my own personal applications I tend to give things with more importance camelCase and the lesser variables of my own, lowercase.
without camelCase:
package
{
import flash.display.Sprite;
public class myclass extends Sprite
{
public function myclass()
{
var myvar:Sprite = new Sprite();
addChild(myvar);
}
}
}
with camelCase:
package
{
import flash.display.Sprite;
public class myClass extends Sprite
{
public function myClass()
{
var myVar:Sprite = new Sprite();
addChild(myVar);
}
}
}
These kind of details are really just a matter of opinion.
However, most people prefer camelCase simply because it is the standard.
back to top