too-many-lines / C0302#

Message emitted:

Too many lines in module (%s/%s)

Description:

Used when a module has too many lines, reducing its readability.

Problematic code:

def is_palindrome(string):  # [too-many-lines]
    left_pos = 0
    right_pos = len(string) - 1
    while right_pos >= left_pos:
        if not string[left_pos] == string[right_pos]:
            return False
        left_pos += 1
        right_pos -= 1
    return True


def main():
    print(is_palindrome("aza"))
    print(is_palindrome("racecar"))
    print(is_palindrome("trigger"))
    print(is_palindrome("ogre"))

Correct code:

__init__.py:

__all__ = ["is_palindrome", "main"]

from is_palindrome import is_palindrome
from main import main

is_palindrome.py:

def is_palindrome(string):
    return string == string[::-1]

main.py:

from is_palindrome import is_palindrome


def main():
    for string in ["aza", "racecar", "trigger", "ogre"]:
        print(is_palindrome(string))

Configuration file:

[main]
max-module-lines=15

Additional details:

When a module has too many lines it can make it difficult to read and understand. There might be performance issue while editing the file because the IDE must parse more code. You need more expertise to navigate the file properly (go to a particular line when debugging, or search for a specific code construct, instead of navigating by clicking and scrolling)

This measure is a proxy for higher cyclomatic complexity that you might not be calculating if you're not using load-plugins=pylint.extensions.mccabe,. Cyclomatic complexity is slower to compute, but also a more fine grained measure than raw SLOC. In particular, you can't make the code less readable by making a very complex one liner if you're using cyclomatic complexity.

The example simplify the code, but it's not always possible. Most of the time bursting the file by creating a package with the same API is the only solution. Anticipating and creating the file from the get go will permit to have the same end result with a better version control history.

Created by the format checker.