Broken Tail Python


“Broken tail” is a style of coding in Python that, while being syntactically valid, is visually awkward. Treat your code like art.

Broken tail continuations

MongoClient.database.get_or_create_object(name='cool_user', date='today',
				          text='example')

V.S

MongoClient.database.get_or_create_object(
    name='cool_user',
    date='today',
    text='example'
)

Observe the opening and closing parentheses around the method parameters. They are like little hands cradling the content. Each parameter has its own line which makes it easy for other developers to quickly scan the method signature.

Don’t do this:

short_name(
    "One parameter"
)

If it all fits on one line cleanly, do it.

short_name("One parameter")

Broken tail comments

This one is painful but I see it frequently, especially in cases when the comment line wouldn’t have exceeded 120 or even 80 characters (the line lengths commonly established in the PEP8 style guidelines for Python).

# This is a comment that must go onto the next
# line

V.S

# This is a comment that must go onto the next line

OR

# This is a comment that must
# go onto the next line

Readability is as important in programming as the function of the code itself.


Ultimately, Python isn’t the only programming language that is an accomplice of this coding style. Numerous other languages allow this to happen including Java Script, PHP, C, and so on. In the end, it isn’t necessarily something that the language itself should enforce, but rather a positive quality that the authors of code written in those languages should take into consideration when creating software.