Sign in to follow this  

[web] [Flash/Flex] ActionScript 3.0 Constructor Etiquette

This topic is 3740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently getting my feet wet in Flash development by learning the basics of ActionScript programming. At present I'm using Foundation ActionScript 3.0 Animation - Making Things Move! by Keith Peters as one of my main resources, along with the Programming ActionScript 3.0 PDF file available from Adobe's site and various searches on the Internet when I get stuck. I notice in Keith Peters' book that he always farms off the constructor to an initialisation method, in this fashion:
package {
    import flash.display.Sprite;
    public class Example extends Sprite {
        public function Example() {
            init();       // construction is passed off to another method...
        }
        private function init():void {
            // ...such as this one here
        }
    }
}
Peters states in his book that while "it would certainly be fine to put all your code into the constructor, ... it is usually considered a best practice to limit the amount of code in a constructor, so I shuffle it off to another method." However, he doesn't include why it is considered a best practice to do it this way. I don't have much of an idea of how the Flash Player interprets ActionScript, and I haven't had any experience with the earlier ActionScript versions, but I can't see how just sticking all your construction details in another method could make any sort of difference. I've searching through Adobe's docs and done a few Google searches but haven't come up with any justification for this. Is there something I'm missing?

Share this post


Link to post
Share on other sites
Quote:
Original post by johnhattan
My guess is that it's a holdover from old pre-constructor versions of ActionScript. Until Flash/Flex has an optimizing compiler that can fold function calls together, slap that code into the constructor.

That was my guess too, although I don't have the prior knowledge of ActionScript 1 and 2 to base it on any more that intution. Given that this is the second edition of a book that was originally written for ActionScript 2, it makes sense that there's a few holdovers.

Thanks!

Share this post


Link to post
Share on other sites
I also have that book, its very well written and has taught me alot more than just AS3. I also wondered why he liked to call init() in his constructors, however when coding in AS3 I like to adopt that style if I'm doing more than just initialising a few variables.

I would like to know the actual reason behind it, I suspect JohnHattan is correct.

Share this post


Link to post
Share on other sites

This topic is 3740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this