Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
81 lines (59 sloc) 4.02 KB
Root.Smell.MethodSmell.MethodLength Parent Index
Sibling aspects ParameterListLength

MethodLength

Number of lines of code in a function or method definition.

Depending on the value of method_length_count_comment, comments are considered. The rule of 30 suggests that the maximum number of lines for a method is 30. PMD defines a default value of 100 lines per method, checkstlye` 150 and 60 (when comments are not considered), rubocop 10.

Tastes

Taste Meaning Values
max_method_length Represents the max number of lines for a method or a function'sdefinition. 30, 10, 50, 60, 100
method_length_count_comment Allows when set to True to considered comments while calculatingmethods' length. 60, 30, 100, 150

* bold denotes default value

Subaspects

This aspect does not have any sub aspects.

Example

def _is_positive(num):
    if num > 0:
        return True
    else:
        return False

# This function can be defined as follow:

def is_positive(num):
    return num > 0

Importance

It is really important is to stay DRY ("Don't Repeat Yourself") and respect separation of concerns. Long methods are sometimes faster and easier to write, and don't lead to maintenance problems. But most of the time they are an easy way to detect that something may be wrong in the code, and that special care is required while maintaining it.

How to fix this

Refactoring methods into smaller more generic methods, making code more compact by using inline and language specific syntax tricks, implementing methods or functions to avoid redundant operation(s), making use of methods provided in libraries rather than reimplementing them.