Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred Python (or any language, really) style: Should else be used when if returns?

Very simple question:

Specifically in Python (since Python actually has "strongly recommended" style guidelines specified in PEP 8, but really this applies to any language), should a function with an if clause that always returns have the alternative code in an else clause or not? In other words, func_style_one() and func_style_two() in the following piece of code are (obviously) exactly equivalent:

def func_style_one():
    if some_conditional_function():
        do_something()
        return something()
    else:
        do_something_else()
        return something_else()

def func_style_two():
    if some_conditional_function():
        do_something()
        return something()
    do_something_else()
    return something_else()

Obviously, the best and most readable style depends on the situation, and opinions will vary greatly on which is better, but I'm asking which is specifically preferred by the core Python community. (e.g. Which is used more often in the standard library, all other things being equal?)

like image 258
Daisy Sophia Hollman Avatar asked Jun 20 '12 14:06

Daisy Sophia Hollman


People also ask

Is it necessary to use else after if in python?

So, The else is not necessary because with or without it, the execution continue normally.

What is pep in python?

PEP stands for Python Enhancement Proposal, and there are several of them. A PEP is a document that describes new features proposed for Python and documents aspects of Python, like design and style, for the community.

How long should a Python function be?

A function should be small because it is easier to know what the function does. How small is small? There should rarely be more than 20 lines of code in one function. It can be as small as below.

Why do we need a style guide for Python code?

One of Guido’s key insights is that code is read much more often than it is written. The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Python code. As PEP 20 says, “Readability counts”. A style guide is about consistency. Consistency with this style guide is important.

What is the Pythonic style guide?

Thankfully, the Python developer community has assembled a set of Pythonic style guides. These come in the form of “Python Enhancement Proposals”, or PEPs for short. The Zen of Python is listed as PEP 20. It’s a collection of 19 statements (number 20 is missing) that describe a certain philosophy to follow when coding in Python.

How to use the Python return statement effectively?

Watch it together with the written tutorial to deepen your understanding: Using the Python return Statement Effectively The Python return statement is a key component of functions and methods. You can use the return statement to make your functions send Python objects back to the caller code.

Why is Python code so readable?

One reason for the high readability of Python code is its relatively complete set of Code Style guidelines and “Pythonic” idioms.


1 Answers

As a rule of thumb, you should always avoid adding unneccessary complexity to your code, whatever the language. It also often is a good idea to try to split your code into semantically meaninful subsections.

Given these heuristics, there is no definitive answer. It really boils down to what you are trying to achieve.

I'll demonstrate this with examples.

If we have a function that checks for various error conditions before proceeding, it could make sense to write it without else:

def do_stuff():
    if error1():
        return cleanup_and_fail()
    return ok()

This is better as you often end up checking several errors in similar fashion in a sequence:

def do_stuff():
    if error1():
        return cleanup_and_fail()
    if error2():
        return do_different_cleanup_and_fail()
    return ok()

However, if your function instead branches to two equal branches, it could semantically make more sense to you else:

def do_stuff():
    if option1():
        return do_option1()
    else:
        return do_option2()

This is because you often end up adding several other options with elif:

def do_stuff():
    if option1():
        return do_option1()
    elif:
        return do_option2()
    else:
        return do_option3()

To summarize: think about the semantics of your code and choose syntax accordingly.

like image 140
jsalonen Avatar answered Nov 15 '22 08:11

jsalonen