The title pretty much speaks for itself, but I'll provide the current decision I am facing.
I am migrating python code towards the use of generators. The current code looks like this:
...
l = returns_a_list(args)
log.debug('examining {} entries', len(l))
for e in l:
do_stuff(e)
...
Outside of the debug log, l
fits the use case of a generator very well, and its length is not needed anywhere else. However, due to the debug log, using a generator would look something like this:
...
log.debug('examining {} entries', sum(1 for _ in returns_a_generator(args))
for e in returns_a_generator(args):
do_stuff(e)
...
This one is less readable and calls the generator twice. However, the production code is straightforward. Another option would be:
...
count = 0
for e in returns_a_generator(args):
do_stuff(e)
count += 1
log.debug('examining {} entries', count)
...
This one does not call the generator twice, which is not a big deal since we do not really care about performance in debug mode. In my opinion it looks a bit more straightforward in terms of counting elements compared to sum(1 for _ in generator)
, which does not convey the intentions as clearly (however, the preceding debug message should hint towards what that snippet is doing). However I am still not certain if moving the log is acceptable (what if do_stuff
fails? what if for some reason the generator yields way to many values? I'd like to have that debug line before the program crashes or starts computing until the universe's heat death.). Moreover, the counting is still done even in with no debug log.
So, what is your opinion on this general issue? What would you think when stumbling onto one of those three options? After having understood them, would you care about the debug log being convoluted and file an issue/patch or would you think "Ah, got it, it's reasonable like that"?
EDIT: This is the programmers community. I am not that interested in concrete solutions to this particular problem, but more in opinions on the first, larger question (unless of course there is a good argument that such a choice should never happen).
returns_a_generator
twice. But again, not really the point of the question.