A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Best Practices?

  1. #1
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63

    Best Practices?

    I'm still having trouble figuring out whether certain things are necessary, preferred, just best practice, etc. For example, when you're dynamically attaching everything at run-time, what's the difference between using stage.addChild and just addChild? I know that the main timeline is a child of the stage, but is there any real reason to use one over the other? When I'm writing an ActionScript file, why should I only import classes that I need instead of just importing everything? I assume it has something to do with memory management. What exactly is the point of adding :void to the end of a function that won't return anything?

    I feel like I should know these things by now but I'm still a bit lost.

  2. #2
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    I'm pretty new to AS3 and am currently reading books/teaching myself.
    Answered to the best of my current understanding:

    I guess it's mostly to do with making your intentions explicit.
    If you're working in a team, or if you come back to your project in months to come you might not know/remember the intention of something.

    It's best to put the exact classes you import so that when you glance at the top of your class you get a good indication of what is being used in the code. If you have a wildcard, then you don't know if only one Class, or all of the classes are being used. It could save you time when debugging the old you/someone else's code.

    It's best practice to put :void if you aren't returning anything, because then everyone knows that you don't intend to return anything. Also you'll get an error if you do try to return something. Also I think you'll get better performance because the compiler knows you won't be returning anything (am I right?).

    I'm not sure about the addChild thing, most examples I see just use addChild(); and not stage.addChild();
    I guess it just makes your intentions more explicit -- like many of these coding conventions.

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    The difference on stage.addChild and addChild (apart from the obvious case where you're writing code inside a child scope), becomes relevant if your swf ever needs to be loaded into another swf. Using addChild() inserts the displayObject into the local scope, while stage.addChild tries to insert at the top of the display tree. So if a loaded swf tries to add something to stage it's actually targeting outside of itself.

    As far as your other two questions - those are both best practices that help you out at the compiler level. The more verbose your script, the less Flash has to guess about what you're trying to do.

  4. #4
    Senior Member Gohloum's Avatar
    Join Date
    Aug 2002
    Location
    Florida
    Posts
    185
    It's also style-istic. for instance:

    addChild()
    this.addChild()

    does the same thing. Some people think the "this" keyword clutters the code. I on the other hand prefer using it because my brain interprets it better.

    Also there are things like:
    myArray:Array = new Array();
    myArray:Array = [];

    Again both the same but some people prefer one over the other. I happen to use both in specific circumstances. If I am initializing an array for the first time I will use the new keyword. If I am replacing an existing array with an empty one, I use [].

    Generally private variables prefix with underscore "_":

    _myVar

    but now you have private, protected, internal, and public. I have seen double underscores for private:
    __myvar

    and then just singles for protected and internal. There is some flexibility in style to be had here. What is important is that you define a convention early on and stick to it. Also if you work with a team, make sure everyone is on the same page with your naming conventions.
    The Early Bird may get the worm, but the second mouse to the trap gets the cheese...

  5. #5
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    The use of wildcard importing is discouraged because it imports all of th classes int the package whether used or not. This can bring in a lot of classes that you are not even using.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  6. #6
    Senior Member Gohloum's Avatar
    Join Date
    Aug 2002
    Location
    Florida
    Posts
    185
    Good point kortex, that's been kind of a bad habit of mine. FlashDevelop helps with this since it's autocomplete on datatyping will add the import statements with the specific class for you.
    The Early Bird may get the worm, but the second mouse to the trap gets the cheese...

  7. #7
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Ah, thanks everyone, good info. I figured most of this just came down to good habits vs. bad habits.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center