Skip to content


Which code layout do you like more?

I can lay out the same function in two ways. Which one is better?

One return statement and a mutable temporary variable


def f(a, b):
    temp = g(a)
    if b:
        temp = h(temp)
    return temp

Two return statements and everything uses single assignment


def f(a, b):
    if b:
        return h(g(a)
    else:
        return g(a)

I prefer the second approach. Ever since I learned how prolog requires single assignment, I avoid updating variables. I’m not adamant about it, but in general, I try not to. It’s just a style thing though. I’ve met a lot of programmers (older ones, usually) that like only having one return statement in a function. I don’t know why they like that, but I’ve heard it from numerous people.

Posted in Programming, Python, Uncategorized.

Viewing 36 Comments

 
close Reblog this comment
blog comments powered by Disqus