i, for loop, uint and int in ActionScript 3

Defining i:uint is not better than i:int since both of them take the same amount of memory. Besides, it’s risky to define i:unit because if i is accidentally assigned to a negative value it becomes a big positive int.

  1. Keith Peters

    I often use uints. Not because of any memory or performance reasons, but because it documents the intention of the variable. You are stating, “this variable is an integer, and should never be negative.” The problem is, like you said, when you make it negative, it flops over. I think it would be better if it threw an exception if you tried to make it negative..

  2. Derek Vadneau

    For a bit more detail on the subject, check out these entries:
    http://www.bit-101.com/blog/?p=908 (surprised kp didn’t mention it)
    http://tracethis.com/archives/2007/02/14/as3-tip-for-loop-index-should-not-be-uint/
    http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/

  3. Christian Cantrell

    I usually use uint, though I have had instances where I’ve had it become a huge positive number. I figure 4294967295 is just as big of a bug as -1, so I usually don’t worry about it.

    Christian

Leave a Reply

For spam filtering purposes, please copy the number 9633 to the field below: