Rule Definition
It is common among software projects to agree on a maximum length for code lines. The main reason behind this restriction is to improve readability, especially when comparing line by line different versions of the same file. Widespread presence of long lines can also indicate the need of code refactoring. Python offers different solutions to wrap long statements that help enhancing the visual structure of the code.
Remediation
The first step is to wrap long statements trying to visually enhance at the same time its inner structure. As recommended in PEP-8: "The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation." Nowadays IDEs can be configurated to automatically signal too long code lines and visualize length margins. When systematically finding the need to have long lines in a module, it should be considered code refactoring, in particular when highly nested code blocks, too large number of function parameters, or too long object names are found.
Violation Code Sample
castle = MakeBigCastle(rocks = rocks_from_the_mountain, wood = wood_from_the_forest, water = water_from_the_well)
Fixed Code Sample
castle = MakeBigCastle(rocks = rocks,
wood = wood,
water = water)
Reference
PEP-8
Related Technologies
Technical Criterion
Documentation - Style Conformity
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.