Reputation: 1541
say there is a class
package
{
public MyClass
{
var myA:Number = 10 ; //<< initializing here
public function MyClass()
{
myA = 20; //<< initializing here
}
}
}
Which one of the above is the right way to follow ?
Upvotes: 0
Views: 72
Reputation: 462
At a conference I attended, I was advised, during a session about optimization, that it is unwise to do many assignments and operations in the constructor, or when declaring variables outside functions; for, the compiler does not put those sections through any vigorous optimization.
This would leave me to believe that it is best to declare your variables outside of your functions, and then assign them in an initialization function, unless these were variables passed along to the constructor as parameters and you want to avoid passing them along again.
package{
public MyClass {
private var myA1:Number; //declare here
public var myA2:Number; //declare here
public function MyClass(arg1:Number = 10):void{
myA1 = arg1;//assigns myA1 a Number passed into the constructor, or 10
init();
}
public init():void{
myA2 = 20; //assigns myA2 a value of 20
}
}
}
As well, after a search on google I found this article that seems to agree.
http://voices.yahoo.com/flash-actionscript-3-optimization-guide-part-1-4793274.html
Keep in mind, that you may just want to do things a certain way that makes things easier for you, and then optimize later; for, just getting it done is more important than style imho.
Hope that helps.
Upvotes: 3