Re:Best Practices of Variable & Method Naming
by mattdeboard on Saturday February 25, 2012 @10:05AM
(#3633058)
"for node in nodes" is preferred, at least IMO, to "for i in nodes"
[ Parent
| on HN ]
Re:Best Practices of Variable & Method Naming
by mudetroit on Saturday February 25, 2012 @11:19AM
(#3633220)
"for node in nodes" implies you are returning the node itself and not simply a counter. This would seem to fall more under his 1 word for loop variables idea.
* NB: I am not certain whether I agree with such rules or not, but that is another discussion point.
Re:Best Practices of Variable & Method Naming
by mattdeboard on Sunday February 26, 2012 @01:10AM
(#3634973)
You must be a functional programmer :) My specific example was Python-related; but yes, if I'm using a functional language (or comprehensions in Python) I'm much more inclined to just use i/j/k/x as the counter. But for loops in Python don't return anything of course except in `break` cases.
Re:Best Practices of Variable & Method Naming
by inerte on Saturday February 25, 2012 @10:17AM
(#3633083)
"i" as in a counter is almost a standard for any code in any programming language. "i", "j" and "k" (comes from math). If you have the loop counter as "node", then you're doing it wrong, but if you have an actual node, then it's ok.
Re:Best Practices of Variable & Method Naming
by kstenerud on Saturday February 25, 2012 @10:41AM
(#3633140)
Or, if you want to be sure that a search/replace of a name won't go awry, you could use names such as customerIdx, nodeIdx, accountIdx. It also helps keep things clear in the few cases where you find yourself with nested iterations (so that you don't accidentally call customers[j] when it should have been customers[i]).
Re:Best Practices of Variable & Method Naming
by mattdeboard on Sunday February 26, 2012 @01:06AM
(#3634967)
If I've got an iterable of Foo object instances, I'll call it "foos". When I iterate, I'll write "for foo in foos" without exception. Calling it i, j or k is fine, too, but I try to be as obvious as possible, because I believe that is a virtue.