Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

How do you name loop variables when the list item is named after something without a plural? For instance (in python): [x for x in sheep]. x is not a great name, but sheep have/has no plural that distinguishes it from the singular.

Is there a general best-practice for naming such variables, to emphasise clarity, and avoid confusion?

share|improve this question

closed as primarily opinion-based by Snowman, MetaFight, Pureferret, Blrfl, gnat May 18 at 16:55

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
To the downvoters and closevoters, I'm asking if there is a best practice: "best practice (noun):commercial or professional procedures that are accepted or prescribed as being correct or most effective." This is not something that should be opinion based. – Pureferret May 18 at 16:23
    
my go-to variable name for items in a collection is: item! Kind of obvious, no? Alternatively, keep the singular variable name and rename the collection. [sheep for sheep in sheepCollection]. – MetaFight May 18 at 16:23
3  
1  
@Pureferret, unfortunately, best practices questions are also usually offtopic here. And, as far as I know, there is no widely recognized best practice for this, so it does far squarely under "primarily opinion based". – MetaFight May 18 at 16:25
3  
Don't feel bad. Our ways are foreign and strange to some long-timers too. – Karl Bielefeldt May 18 at 16:59

Names should be expressive in direct proportion to their scope. A loop variable should have a really small scope, so it's perfectly alright to just call it s. If this makes the code unreadable, you should refactor the loop code into a method, not find a longer variable name.

share|improve this answer
    
I had not considered it that way. Thanks – Pureferret May 18 at 16:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.