|
| 07 Jan 2014 09:19 PM |
I've been noticing myself doing this a lot lately and I was wondering if it's something that everyone else does too
Say I have five parts, instead of writing out five lines (and then maybe having to edit five lines later on) I'll do something like this
for partNumber, part in pairs{partA, partB, partC, partD, partE} do part.Transparency = 0.5 end
Is there a better way of doing this? It looks very readable to me and it's easy to edit, so I'm always inclined to go straight to it even if I only have two parts. |
|
|
| Report Abuse |
|
|
HaxHelper
|
  |
| Joined: 19 Dec 2009 |
| Total Posts: 1208 |
|
|
| 07 Jan 2014 09:21 PM |
| I do that for anything more than two parts |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 07 Jan 2014 09:24 PM |
| I never had a case where I wanted to change only some parts in a model, it was always all, one, or nothing for me. So not really |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 09:31 PM |
@cnt; This is specifically for parts. I most recently used this to cycle through nodes on a grid. I have to cycle through three at a time, so I was using
for nodeIndex, node in pairs{group.A, group.B, group.C} do -- end |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 09:31 PM |
*this isn't
Of course I mis-type the most important word in the post. |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 09:35 PM |
I use next, but I do do that.
Lazy ftw! |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 09:35 PM |
I also iterate through a table of tables to create interface buttons instead of making them one at a time :P
Again, lazy ftw! |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 07 Jan 2014 09:37 PM |
That works I guess, I would probably do something weird like:
function makeTransparency(trans, ...) local parts = {...} for i, v in next, parts do v.Transparency=trans end end |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 09:38 PM |
Why not just skip out local parts = {...}?
for i, v in next, {...} do v.Transparency=trans end |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 07 Jan 2014 09:38 PM |
| sometimes I return the table from the function, and it seems better to just create a variable as a table then to do {...} twice |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 09:46 PM |
| I meant for that specific example, but forgot to mention it. |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 11:19 PM |
I never learned how to use next. Is it
for [index], [value] in next, [list] do ...
? |
|
|
| Report Abuse |
|
|
| |
|
suremark
|
  |
| Joined: 13 Nov 2007 |
| Total Posts: 6315 |
|
|
| 07 Jan 2014 11:24 PM |
For varargs, I like to do
for _, arg in pairs{...} do -- do stuff end |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 11:28 PM |
"for partNumber, part"
"for i,v"
"for _"
The readability of the code in this thread is declining at an alarming rate. |
|
|
| Report Abuse |
|
|
suremark
|
  |
| Joined: 13 Nov 2007 |
| Total Posts: 6315 |
|
|
| 07 Jan 2014 11:29 PM |
| Underscores are a perfectly readable and acceptable convention for unused variables. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 07 Jan 2014 11:30 PM |
| I never use i, v. I usually use key, value or index, value but oh well |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 11:35 PM |
| If a variable is unused it doesn't exist. That fact that _ exists in the environment means it's being used, even if its value has no affect on the code itself. Using _ can result in forgetting how your table is indexed in larger projects, and it can help push you into the trap of using one-letter shorthand variables like cnt was doing with his i,v. Of course this is small code and we all know what it means, but I know for a fact that difficult-to-read code can and will result in dead projects when they last longer than your short-term memory. I don't want to see many people giving up on or restarting their projects just because they can't read their own code. |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 11:38 PM |
| If you yourself are not using a variable the convention is to have _ in its place. |
|
|
| Report Abuse |
|
|
suremark
|
  |
| Joined: 13 Nov 2007 |
| Total Posts: 6315 |
|
|
| 07 Jan 2014 11:39 PM |
| What you say is true, but in the case of {...} the indices are not necessary. Plus, the variables used in a 'for' loop are local to that stack. They won't interfere with the rest of your code. |
|
|
| Report Abuse |
|
|
|
| 07 Jan 2014 11:39 PM |
| Although I do not often use it unless I have for loops within for loops and do not use the i's. |
|
|
| Report Abuse |
|
|
HaxHelper
|
  |
| Joined: 19 Dec 2009 |
| Total Posts: 1208 |
|
|
| 07 Jan 2014 11:40 PM |
| it's usually not a good idea to use single-letter variable names in any case |
|
|
| Report Abuse |
|
|
MettaurSp
|
  |
| Joined: 20 Mar 2010 |
| Total Posts: 3179 |
|
|
| 07 Jan 2014 11:44 PM |
| I use '_' as a variable name rarely, but it is always for an unused variable so I know it is unused. |
|
|
| Report Abuse |
|
|
|
| 08 Jan 2014 03:09 AM |
In Lua "_" doesn't mean anything. It's just a 1-letter variable.
So since it doesn't do anything, you might as well just have the name "Index" instead in case you end up wanting to use it.
But still, I always just use an underscore, I rarely need the index and if I do it's not going to take ages to undo or anything |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|