-
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?
-
Senior Member
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.
-
If I get only one response, it better be from someone who *knows*.. and it was.
Thanks, tonypa!
-
Senior Member
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".
-
Senior Member
Originally Posted by NickZA
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|