


These are the entries under the category » 2009 » April
When declaring variables like objects or arrays, there is more than 1 way to declare them. I’m not exactly sure which way is faster, but I have used both scenarios in a variety of occasions. I’m putting this here just to help clarify they mean the same thing.
//Object Declaration 1 var objData:Object = new Object(); objData.fname = "Mr."; objData.lname = "Krinkle"; //Object Declaration 2 var objData:Object = {fname:"Mr.", lname:"Krinkle"}; //Arrays Declaration 1 var arrData:Array = new Array(); arrData.push("Mr."); arrData.push("Krinkle"); //Arrays Declaration 2 var arrData:Array = ["Mr.","Krinkle"];
Everyone seems to know the feeling of being lost knee deep in a mess of code.
By using naming conventions you can help reduce the clutter….or at least organize your variables.
Here are some improvements that may help to keep your sanity.
1st Line: wrong way
2nd Line: right way
Numbers: use “num”
var thing:Number = 1; var numThing:Number = 1;
Strings: use “str”
var thing:String = "hello"; var strThing:String = "hello";
Alot of the time I spend developing flash, I’m always looking for ways to optimize my code and make it run faster. Take for instance the following code.
var arrStuff:Array = new Array(); arrStuff.push({label:"thing1"}); arrStuff.push({label:"thing2"}); for(var i:int=0; i<arrStuff.length; i++){ }
Should be written like so
var arrStuff:Array = new Array(); arrStuff.push({label:"thing1"}); arrStuff.push({label:"thing2"}); var numLen:int = arrStuff.length; for(var i:int=0; i<numLen; i++){ }
The reasoning for this is because it’s less work for flash to do. Instead of every single loop looking up the array’s length, it simply just compares it to a number (or int). It may not seem like much a difference, but later if your looping through thousands of objects it could definitely help.