Home >Backend Development >Python Tutorial >Why I always assign intermediate values to local variables instead of passing them directly to function calls

Why I always assign intermediate values to local variables instead of passing them directly to function calls

Linda Hamilton
Linda HamiltonOriginal
2024-09-27 06:22:291139browse

Why I always assign intermediate values to local variables instead of passing them directly to function calls

Instead of

def do_something(a, b, c):
    return res_fn(
        fn(a, b),
        fn(b),
        c
    )

I do:

def do_something(a, b, c):
    inter_1 = fn(a, b)
    inter_2 = fn(b)

    result = res_fn(inter_1, inter_2, c)
    return result

The first version is much shorter, and when formatted properly, equally readable.

But the reason I prefer the second approach is because all intermediate steps are saved to local variables.

Exception tracking tools like Sentry, and even Django's error page that pops up when DEBUG=True is set, capture the local context. On top of that, if you ever have to step through the function with a debugger, you can see the exact return value before stepping out from the function. This is the reason why I even save the final result in a local variable, just before returning it.

At the performance cost of couple of extra variable assignments, and couple of extra lines of code, this makes debugging much easier.

The above is the detailed content of Why I always assign intermediate values to local variables instead of passing them directly to function calls. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn