Backslash line continuation makes code less readable

CostSoftware AgilityCode Readability

Backslash line continuation makes code less readable

This code insight shows that each time a backslash is the last non blank character of a line.

Bad

my_very_big_string = “”””””For a long time I used to go to bed early. Sometimes, \
when I had put out my candle, my eyes would close so quickly that I had not even \
time to say “I’m going to sleep.”””””””

from some.deep.module.inside.a.module import a_nice_function, another_nice_function, \
yet_another_nice_function

Good

my_very_big_string = (
“”For a long time I used to go to bed early. Sometimes, “”
“”when I had put out my candle, my eyes would close so quickly “”
“”that I had not even time to say “I’m going to sleep.”””
)

from some.deep.module.inside.a.module import (
a_nice_function, another_nice_function, yet_another_nice_function)

5362

Why you should care

When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the backslash, will break the code and may have unexpected results.

A better solution is to use parentheses around your elements. Left with an unclosed parenthesis on an end-of-line the Python interpreter will join the next line until the parentheses are closed. The same behavior holds for curly and square braces.

Business Impacts

It is recommended to avoid these in order to ensure the code is more readable and cost effective.

Cost

CAST recommendations

References

http://support.objectivity.com/sites/default/files/docs/objy/R11_0_0/html/java/guide/jgdObjectQualification.html

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.

See featuresHow it works