Kerry Thompson wrote:
Jim McIntyre wrote:

I got the impression from the AS2 documentation that one *should* set an
initial value for a variable in the class body, as long as the initial
value could be defined at compile time.

However, I've seen many folks leave the value undefined by not assigning
anything in the class body, like your example below.

What's the real deal? Is there a reason it's better (or worse) to favor
one practice over the other?

I'm not  sure what you mean by "class body", but in the example you cited,
the variable is initialized in the constructor. That guarantees that the
variable will be initialized.

As for initializing it in the variable declaration vs. in the constructor,
it's purely a matter of style and circumstance. Sometimes you can't
initialize a variable until the class is constructed--for example, a path.

It's crucial to initialize your variables--uninitialized variables are the
source of more bugs than any other single thing I can think of. It doesn't
matter a lot where you initialize the variables, though.


Sorry for my sloppy terminology! By saying "class body" I meant initializing it here, which is what the documentation recommends:

class myClass
{
  public var myVar:Number = 10;
}

as opposed to here:

class myClass
{
  public var myVar:Number;
  public function myClass()
  {
    this.myVar = 10;
  }
}


Thanks for the input, though. The documentation I cited is in the section dealing with coding practices and style, after all, so I guess there's no real technical reason to do it one way or the other... as long as it's done.

-Jim
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to