is this valid code?

Hi

I want to cycle through an array table and display some of the contents (it’s actually a page of scores).

This is the gist of what I have:

for i = 1, #myTable do         local myText = display.newText(myGroup, myTable[i].myField, 100, 100 \* i, FONT\_NAME, 30 )  ... end

and then later in another function:

myGroup:removeSelf() 

The question I have around this is that I wind up with 8 text objects on the screen, but there has only ever been one variable name. It works just fine on my test devices (iOS 7 iPads and Asus tablet) and the simulator but I’m wondering whether it’s sloppy code. Should I be dynamically generating a new variable name each time through the loop, based on the value of “i” ?

          

thanks,

David

This is one of those areas that creates some memory management confusion.  If you were not inserting them into the group, then you would certainly be creating a memory leak since you’re overwriting the pointer to the previous display object.

But since you’re inserting it into a group, the group is holding a reference to each display.newText object and when the you do the removeSelf() on the group, it cleans up it’s children for you, so you’re not leaking memory. 

Personally I would put them in an array, though in this case, you would actually end up using a little more Lua memory than the way you’re doing it.

Rob

Nothing sloppy about using a temp variable to load an array of objects in a loop - it’s actually very efficient / clean. But, if you’re looking for some criticism on sloppiness / neatness, I’d change basically all of your variable names.

The variable “i” might be renamed numRadarBlips… The var myText might be renamed blipLabel, myGroup --> radarGroup, FONT_NAME --> radarFont, etc etc

Of course the naming depends on how the vars are actually used. But naming then closer to what the objects are / do will greatly help other coders pick up your code (or you, a year or two later).

Hi

Thanks for the tips. I actually just used those variable names for simplicity’s sake in this example. In my code I use very descriptive (and consequently sometimes very long) names for my functions and variables.

cheers,

david

This is one of those areas that creates some memory management confusion.  If you were not inserting them into the group, then you would certainly be creating a memory leak since you’re overwriting the pointer to the previous display object.

But since you’re inserting it into a group, the group is holding a reference to each display.newText object and when the you do the removeSelf() on the group, it cleans up it’s children for you, so you’re not leaking memory. 

Personally I would put them in an array, though in this case, you would actually end up using a little more Lua memory than the way you’re doing it.

Rob

Nothing sloppy about using a temp variable to load an array of objects in a loop - it’s actually very efficient / clean. But, if you’re looking for some criticism on sloppiness / neatness, I’d change basically all of your variable names.

The variable “i” might be renamed numRadarBlips… The var myText might be renamed blipLabel, myGroup --> radarGroup, FONT_NAME --> radarFont, etc etc

Of course the naming depends on how the vars are actually used. But naming then closer to what the objects are / do will greatly help other coders pick up your code (or you, a year or two later).

Hi

Thanks for the tips. I actually just used those variable names for simplicity’s sake in this example. In my code I use very descriptive (and consequently sometimes very long) names for my functions and variables.

cheers,

david