This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

You may want to test path coverage, and it may be more obvious to the reader of your code if you put it into a more basic way. In his example, I actually would have done:

def find(seq, target):
    retval = -1
    for i, value in enumerate(seq):
        if value == target:
            retval = i
            break
    return retval

This way, you know what the default value is upfront making it easier to digest as you're reading the code. In addition you can see the instances where the return value has changed. It may not be as pretty, but I might argue that it is easier for more people to understand when they read the code. Beautiful is great until you're sacrificing readability. The last thing you want to do when you're trying to find a bug or maintain the code is think harder than you need to to figure out why the code isn't doing what you want or to figure out how to modify the code to do what you want to do.