5

This question is related to python, but it is actually common for many languages. Take a look at that code:

import sys
people = ['John', 'Jack', 'Charles']
cities = ['London', 'Liverpool', 'Manchester']
if (len(cities) != len(people)):
    print "Error! Length of cities list \
    (%s) differs from length of people list (%s)" % (len(cities), len(people))
    sys.exit(1)
for i in xrange (len(cities)):
    print "Hello, %s from %s" %(people[i], cities[i])

Seems correct. But at this small fragment len is called 3 to 5 times (3 times if lists' lengths are equal, 5 times if lists' lengths differ). Do we need to rewrite code like this?

import sys
people = ['John', 'Jack', 'Charles']
cities = ['London', 'Liverpool', 'Manchester']
people_count = len(people)
cities_count = len(cities)
if (cities_count != people_count):
    print "Error! Length of cities list \
    (%s) differs from length of people list (%s)" % (cities_count, people_count)
    sys.exit(1)
for i in xrange (cities_count):
    print "Hello, %s from %s" % (people[i], cities[i])

In the other words: in which cases do we need temporary variable for storing list length, and in which cases it is not needed?

1
  • 2
    This is mostly a matter of whether you want to keep reaching for the parenthesis keys. (Also, don't loop over xrange(cities_count). Loop over zip(people, cities).) Dec 19, 2013 at 20:37

4 Answers 4

3

I think that this falls within Do Not Repeat Yourself: if you wanted to change away from using that length, you'd have to change about five things, but if you used a variable, it would probably just be that one.

5
  • Even with the variable, if you change the meaning of it then you quite likely need to change the text "Length of cities list". So it's probably a false hope that using a variable avoids repetition, but that doesn't mean it's necessarily a bad idea. It might be possible to think of text that wouldn't need to change. Dec 19, 2013 at 20:53
  • @SteveJessop Right, but that's still two changes rather than six.
    – Marcin
    Dec 19, 2013 at 20:54
  • The rule is "Don't Repeat Yourself" and not "Repeat Yourself Only Once" for a reason. It doesn't follow that repeating yourself once is significantly better than repeating yourself five times. Especially when four of them are in a single line of code. Dec 19, 2013 at 20:58
  • @SteveJessop By that logic, repeating yourself fifty times is no worse than repeating yourself once.
    – Marcin
    Dec 19, 2013 at 20:59
  • That's not what I said. I said the rule doesn't say it's worse. More relevant to your answer, though, I said it's not the case that "if you used a variable, it would probably just be that one". Dec 19, 2013 at 21:00
2

If you need to count up to the length at all, there's a good chance you're doing it wrong. In your specific case, I would use this instead:

for p, c in zip(people, cities):
    print "Hello, %s from %s" % (p, c)

But in general, as soon as len(somelist) appears twice in your code for the same list, I would stick it into a variable.

1

it will not give you any significant performance increase if that's what you're asking for. len(list) is O(1) meaning it will be the same speed as accessing the variable.

from a clean code prespective I beleive that repeating len(cities) is cleaner.

  1. has less characters than cities_count
  2. it's immediately visible what youre doing.
1
  • Calling a function takes more time than accessing a variable.
    – dansalmo
    Dec 19, 2013 at 20:57
0

This sounds like premature optimization. If the len() calls happen frequently then it's worth putting them in a separate variable. Otherwise you could leave the len() calls there for easy of coding.

On the other hand there's nothing wrong with a coding style (or coding policy) that prefers additional variables for storing work that needs to be computed only once. In this case there wouldn't be noticeable performance gains by doing so but that's a different discussion.

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