Flash on Tap -- Now in May →← Custom filters (sort of) without Pixel Blender

Variable Declaration, aka Iterative Sanity

I was just talking to Tai U, a friend of the Spaceship, about this one. We were discussing how all these new young hooligan coders out there are committing the sin of looping using reserved variable names for iterators. Something sorta like this:


for(var x:int=0;x<foo;x++) {
// x?! what are you doing?!!?
};

Young friends, that may fly for other languages, but in ActionScript you are playing with fire. I have labored for many moons over a better solution for this and the most sensible in my head is described as follows:


/* code below assumes some multidimensional array named grid was created before we got to this loop.*/

for(var i:int=0;i<grid.length;i++)
{
var a:Array = grid[i];
for(var ii:int=0;ii<a.length;ii++)
{
var b:Array = a[ii];
for(var iii:int=0;iii<b.length;iii++)
{
var mc:MovieClip = b[iii];
mc.gotoAndPlay("IN");
}
}

I use roman numerals instead of i,j,k because I find that easier to track back to how many loops I’ve gotten myself into… plus, if I start digging into ix territory, I’m probably writing WAY too many loops. :)

At the start of each loop I declare a variable that allows for easy reference for the item in whatever nested array I’m pointing at. These variables are organized alphabetically, so grid[i] will always be referred to as “a”, grid[i][ii] as “b” and so on. I realize this isn’t exactly the FASTEST way to loop through something, but I can always remove these pointers later if I really need a performance boost.

I doubt it’s the best convention out there. I’m sure the i,j,k camp will have something to say, and I’d love to hear it… I’m open to changing back to it if the argument is compelling enough. What I know we can all agree on is that declaring your variables “x” and “y”, or any other reserved keyword is a no no. You’re simply setting yourself up for complex irregular bugs and behavior. So to all you new coders out there: Quit it! And take off your shoes before you come in the house! Geesh.

September 15th, 2008  by Jamie  /  15 Comments
actionscript


Comments on “Variable Declaration, aka Iterative Sanity”

  1. This is a terrible idea. i, ii, iii, iv, iiv etc. Are you mad?

    Al on September 15th, 2008 at 7:38 pm
  2. Well I wouldn’t say I am of “the i, j, k camp”, but I just rarely find myself going too far in loops, thus making the use of mainly i and j, and sometimes k perfectly reasonable. If things go beyond that, something is probably wrong and I need to put code in another function.

    Rigard Kruger on September 16th, 2008 at 1:54 am
  3. I think it’s very difficult to understand if you work in a team - cause your typing is really not easy to read - and understand if you have some line of code.

    why don’t you declare the vars out of the loops ?
    then you can refer and remove these pointers too.

    Andre on September 16th, 2008 at 4:19 am
  4. I like the roman nested style. One way I make sure I never encroach on reserved keyword territory is to begin local variables (or function specific variables) with a ‘$’. These are always variables I expect to be garbage collected after the function block executes. It also means in eclipse I get to just type ‘$’->ctrl + space for a list of that function blocks variables. Great for times when my short term memory fails me.

    Liam Walsh on September 16th, 2008 at 5:10 am
  5. When I learnt Java in school the idea was to start with i and then continue through the alphabet (j,k,l, and so on).

    Snorre Berge on September 16th, 2008 at 7:18 am
  6. @Al: I don’t think there’s any question that I am out of my mind. :) Seriously, why do you oppose the roman numerals?

    @Andre: Sometimes I do, especially if I reuse i for a different counter somewhere else. Nobody here seems to mind and we’re not big on imposing standards upon one another, so it’s just a style I like.

    @Liam: Ah the dollar sign variable. Best used very carefully — Check this out for more info. Although we do use it here for arguments…

    @Snorre: Yah, that seems to be the popular convention. I wonder if there’s a roman numeral camp out there, beyond myself? Maybe I’ll start using a “binary” convention. var oooo:int, var oooi:int, var ooio:int, etc…

    Jamie Kosoy on September 16th, 2008 at 8:37 am
  7. If you wanna track back your loops, why don’t you use something like c1, c2 or c3.
    Then it’s not so difficult to count your i’s and j’s etc.

    Andre on September 16th, 2008 at 9:02 am
  8. I think the site you were trying to link to about the $ dollar sign is incorrect. I think you are trying to link to here. http://www.senocular.com/?id=2.41

    Tim Kindberg on September 16th, 2008 at 9:39 am
  9. @Jamie, I can’t wait to see the binary convention gain popularity!

    Justen on September 16th, 2008 at 10:46 am
  10. @Tim: You’re right. I updated my link.

    @Andre: That’s not a bad solution. I forgot one of my rationalles for using roman numerals: It’s easy to refactor variable names if you need to insert a loop in between existing loops — just add another i! If you have a ton of nested loops going on (and I pray you don’t), it might be a bit irritating to correct your v’s and x’s, but I loathe seeing i,k,j in that order. Maybe I’m just ocd. i,ii,iii is way easier to refactor into a nice tidy order. :)

    Jamie Kosoy on September 16th, 2008 at 10:49 am
  11. @Jamie. Cheers for the link, I dollars have been a no-no in member variables for sometime now (AS2 had ‘$$’ at the start of all the hidden assetPropFlagBlah whatever variables when you poked under the hood.) It never fails to annoy people who look at my code for the first time, until they try it. I have converted a few people in my time. Similarly people really really shouldn’t have a public method named ‘init’ in AS3.

    :)

    Liam on September 17th, 2008 at 3:32 pm
  12. Hey Jamie.
    Yeah, I’ve always declared var i:uint; at the beginning and then used i for several loops, i usually go into j, and rarely k, so that works fine for me.. but i agree with you (this is such a small issue), that as long as you aren’t using key words like x and y, its all good. :)
    Christian

    Christian Kragh on September 18th, 2008 at 9:07 pm
  13. I have to iterate over a ten-dimensional data set. What’s the Roman numeral for ten? ;-)

    Frankly, the biggest problem I face when writing code that iterates is when you need to find and replace your iterator variable. Have you ever tried to find and replace “i”?

    So I formed a gang of thugs who help me iterate. Meet ike, jen, ken, lee, mel, nel, and obi(-wan). I don’t see obi too often. But unless I’m iterating over a list of bikes, these iterators’ names save me time.

    Rezmason on September 21st, 2008 at 4:00 am
  14. Ok.. a few things wrong with your reasoning:

    first, for readability and ease of maintenance/debugging variables should always reflect their intended purpose, so if you’re looping through rows and columns of a grid it would be more appropriate to use “row” and “col” for your variable names (or “r” and “c” if you’re concerned about variable name length).

    second, assuming your loops are defined inside a function, the variables are locally scoped so it doesn’t technically matter if you use x, y, alpha, or whatever you want. if it makes sense with regard to the first point above, use it! … OK, granted it could cause another developer confusion if they don’t see the variable declaration and therefore assumes x really means the DisplayObject’s x (assuming the code lies in a DisplayObjects subclass)… but thats another story because if your developers don’t see variable declarations in for loops, or worse - modifying code for which they don’t understand the purpose - you should probably find new developers.

    third, saying you could pull out the variable references/pointers to the objects you’re iterating through for a performance boost is completely wrong. in fact you’re boosting performance by having those references because it prevents flash from having to look up the object via object/array notation for each loop. In fact, your biggest flaw for performance is that you’re requesting the length of each array in each loop…. now that’s terrible my friend. at the same time you store each pointer in each loop, you should store the length of the next loops array and use that in your loop definition (unless of course you expect the length of the loop to change as a result of the code within the loop).

    finally, if you’re really concerned about performance, shorter variable names process faster in flash so since j.length is shorter than ii.length …

    Todd on October 2nd, 2008 at 10:22 am
  15. I just double up the iterator characters. ii, zz, xx. I typically aim for pairs that won’t appear too often in my code.

    I find it easier to read than a single i, which tends to get lost between []’s, and if I need to replace them, there are a few less instances of the match.

    Alex on October 16th, 2008 at 10:14 am