Pointless Code
We’ve all seen it, perhaps even in our own code. It’s something I think we do because we’re not really sure what would happen if we didn’t do it. Here are some little tidbits of pointless code I’ve been seeing recently:
For starters, here’s a quick review of the default values of class fields:
class Classy { public var i:int; // 0 public var u:uint; // 0 public var n:Number; // NaN public var s:String; // null public var o:Object; // null public var a:Array; // null public var b:Boolean; // false public var r:RegExp; // null public var v:Vector.<int>; // null }
Make sure you know the above. When you do, you realize how pointless and bloated this is:
class Classy { public var i:int = 0 public var u:uint = 0 public var n:Number = NaN public var s:String = null public var o:Object = null public var a:Array = null public var b:Boolean = false public var r:RegExp = null public var v:Vector.<int> = null }
All of these assignment statements will result in bytecode that gets executed to change each variable to its current value. This wastes CPU cycles and increases the size of your SWF. It should be noted though that constants do require initialization.
The second and last case I’ll talk about today is the case of conditionals. Any expression you put in an if or the like will essentially be passed to the global Boolean function. Consider these if statements:
// Best at everything except being explicit if (obj) { } // Explicit, more typing, slow if (obj != null) { } // Sort of explicit, even more typing, slowest if (Boolean(obj)) { }
Keep this in mind as you write your conditionals. As a programmer you’re likely to type a lot. Make sure your typing really counts!
#1 by jwo on July 4th, 2010 ·
Love the blog. I recently had to defend my usage of if (obj) over if (obj != null), glad to see I was vindicated.
Do you know if in simple situations are there any performance implications for using if..else statements of ternary operations?
e.g.
vs.
I have been trying to convince myself to run a test but been too lazy haha.
#2 by jackson on July 11th, 2010 ·
I haven’t tested this directly, but I’ll take a look at it and let you know (perhaps via an article) if I find anything. One would hope that MXMLC generates the same bytecode, but some recent examples seem to, unfortunately, show otherwise.
#3 by bubumachen on February 11th, 2011 ·
Sorry, i realise this is so old but surely, especially for the first example, the two should be compiled to the same code?
I mean doesn’t this say more about the AS3 compiler being rather rubbish? Surely the compiler should check whether you set a value for a var on its creation before setting the default value, rather than setting the default value and then setting the value you have chosen – particularly if the var is set and assigned a value all in the same line!
Again for the second one, the same seems to apply, as null is a primitive and If(obj) is logically equivalent to if (obj != null) – that is surely what if(obj) would be turned into anyway – or at least if the compiler was decent enough it could turn it into the same code? I can understand why it wouldn’t for if (Boolean(obj)) as this is forcing a cast but the other two surely the compiler should know better and spit out the same code?
Hopefully my ignorance can be explained cos that would be helpful.
If you ever somehow realise that i have commented then thank you for your time :)
#4 by jackson on February 11th, 2011 ·
Even in Flex 4.1, MXMLC compiles them differently. Consider these two classes:
Using nemo440, the difference is demonstrated in an AS3-style manner. Here’s
Duplicate
:And here’s
Unique
:Using Apparat, the difference is demonstrated more verbosely and less like AS3. Here’s
Duplicate
:And here’s
Unique
:So, as stated in the article, the pointless assignment to the default value will increase SWF size. It’ll also increase the amount of typing you have to do. As far as speed though, I don’t think it’ll be even a little noticeable. The JIT may even take out the extra assignment. Still, why do more typing and increase your SWF size for no reason? Every AS3 developer should know the default values and not need extra visual reinforcement. It’s not like the defaults are very hard to memorize:
0
,null
,NaN
andfalse
.#5 by Deril on January 10th, 2012 ·
your example class… it’s slower by 0.000006 ms per use… if you will type default values in.