dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Non-necessary return values: help or hindrance?

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Posts
    17

    Non-necessary return values: help or hindrance?

    Hey guys,

    Long time. I've a question about architecting methods.

    As an example, think about how addChild() (or certain other display functions) returns the child DisplayObject that was added. It was really more a choice than a necessity, from Adobe's point of view, to architect it like that, because it isn't necessary for addChild() to have a return value in order to do what it does, or if it did, it could return a Boolean to indicate success or failure. Their choice to architect the display functions this way, however, allows you to write more compact client code, eg.
    code:
    addToHashTable(addChild(new Sprite));



    For some time, I have been questioning whether to write methods this way in my games. For most applications it's unimportant, but for games, I tend to wonder what the time overhead is of adding these "friendly" return values to my methods. If it isn't a primitive return value, then it's just a reference that is passed, and I guess then that overhead only really occurs on the actual line indicating the return, and in the client code there will presumably be overhead only if the return value is actually passed into a property or method to be used.

    Ideas? Suggestions? Gifts?

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Do you worry about impact on speed or memory? Dont think these affect the speed much so I would use whatever way you feel more comfortable with. Personally, I like to write the code out clearly so I can later understand whats happening, making it compact like you example, confuses me. But if it works for you then go for it.

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Posts
    17

    Thumbs up

    If I get only one response, it better be from someone who *knows*.. and it was.

    Thanks, tonypa!

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    If you are interested optimizing actionscript 3 code, I recommend this series of extremely good articles (any blogs or tests that does not include standard deviation are not trustworthy):
    http://www.stephencalenderblog.com/?p=7

    Very shortly: "Dont bother unless it really has an effect".

  5. #5
    Senior Member ozmic66's Avatar
    Join Date
    Oct 2005
    Posts
    472
    Quote Originally Posted by NickZA View Post
    For some time, I have been questioning whether to write methods this way in my games.
    Hey Nick. I haven't tested any of this, but it's a well known fact that function callback has a slight overhead. The thing is that unless you're adding hundreds of children per frame, this is probably going to be less than noticeable. So yea, whatever will be easier to understand when you come back to your code 6 months from now is a better priority.
    Sometimes I get really caught up in optimizing but I've realized that at some point nice looking code > less cpu cycles

    @tonypa great link!
    Last edited by ozmic66; 06-29-2009 at 10:30 AM.
    Pixelwave Flash-based iPhone framework
    iPhone Games: Flyloop | Freedom Run

    Twitter: Oztune

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