PY3.R0911

Too many return statements

Used when a function or method has too many return statement, making it hard to follow.

Noncompliant Code:

Copy
def foo(x):
    if x == 1:
        return 'This is one.'
    if x == 2:
        return 'This is two.'
    if x == 3:
        return 'This is three.'
    if x == 4:
        return 'This is four.'
    if x == 5:
        return 'This is five.'
    if x == 6:
        return 'This is six.'
    if x == 7:
        return 'This is seven.'

Compliant Code:

Copy
NUMBERS_TO_STRINGS = {
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four',
    5: 'five',
    6: 'six',
    7: 'seven'
}
def foo(x):
    return f'This is {NUMBERS_TO_STRINGS.get(x)}.'

The content on this page is adapted from the Pylint User Guide, Copyright ©2003-2022, Logilab, PyCQA and contributors. All rights reserved. https://pylint.pycqa.org/en/latest/index.html#, and is used under the Python Software Foundation License Version 2. Examples, recipes, and other code in the Pylint documentation are additionally licensed under the Zero Clause BSD License.