View Full Version : Where best to put utility functions
QuantumTiger
08-08-2008, 08:32 AM
Hi
In the AS3 project I'm working on at present I can see the need for a few (small) utility functions which several classes need access to. What's the best place to declare them?
The options I've currently thought of are 1) to extend Sprite (eg to mySprite), place the utilities there and then make all the dependant classes extend mySprite or 2) to create a small utility class, create an instance in my main class and pass a reference to it to the objects which require it.
Any options I've missed? Any thoughts on which method would be most efficient? Many thanks...
pj-co
08-08-2008, 10:15 AM
I've always thought of utility classes as mostly static personally. Though I guess it kind of depends on what you mean by utilities.
yell0wdart
08-09-2008, 12:52 AM
Personally, I'd create static classes within their own namespace or package to house all my utility methods (TextTools, EnumHelper, XmlTools, etc). That way you're not bloating your objects with a bunch of methods that they may or may not need. No need to inheritance-crazy just to extend functionality. ;)
Flash Gordon
08-09-2008, 01:36 AM
My utilitlies are all in a folder called utils. I have
XMLUtil
ArrayUtil
StringUtil
quickCopy(DisplayObject)
etc .......
QuantumTiger
08-11-2008, 10:50 AM
Thanks guys - that's very helpful.
swivelmaster
08-26-2008, 08:22 PM
Please tell me what quickCopy(DisplayObject) does. There's a whole debate on Adobe's bug database about the lack of ability to do a real copy of any DisplayObject data.
In regards to the original question, static classes are where it's at.
I also have some helpers that require instances to be made, but exist completely separately. One is for calculating file sizes, and includes instance variables for bytesLoaded and bytesTotal, and at any point you can get those values back converted to any size unit (byes, k, m, g, t, and p for the far future!). The conversion functions are still static though.
pj-co
11-15-2008, 01:49 AM
Please tell me what quickCopy(DisplayObject) does. There's a whole debate on Adobe's bug database about the lack of ability to do a real copy of any DisplayObject data.
In regards to the original question, static classes are where it's at.
I also have some helpers that require instances to be made, but exist completely separately. One is for calculating file sizes, and includes instance variables for bytesLoaded and bytesTotal, and at any point you can get those values back converted to any size unit (byes, k, m, g, t, and p for the far future!). The conversion functions are still static though.
$20 says FG has a class that grabs all the known properties of the DisplayObject and creates a new one with those properties :)
pj-co
11-16-2008, 04:50 AM
$20 says FG has a class that grabs all the known properties of the DisplayObject and creates a new one with those properties :)
except that doesn't work for the graphics object
*smacks self on head *
I see what you mean swivelmaster. Interesting talk on adobe's JIRA site.
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.