Whitespaces are a Production Risk

Count one violation each time a format replacement field contains a whitespace. bad

print "{ what} is {how it is}".format({"what": "life", "how it is": "hard"})
# Output: KeyError; the correct key would be ' what' with a leading whitespace

print "{ what} is {how it is}".format({" what": "life", "how it is": "hard"})
# Output: KeyError; notice the double whitespace in the placeholder

good

print "{what} is {how_it_is}".format({"what": "life", "how_it_is": "so much easier if you use QuantifiedCode"})
# Output: "life is so much easier if you use QuantifiedCode"

Why you should care

If you use a replacement field like { var} in your format string, which contains a whitespace in its name, the key of the corresponding entry in your dictionary must contain this whitespace, too. Since whitespaces are easy to miss and you should avoid them.

Business Impacts


CAST recommendations


References

https://www.quantifiedcode.com/knowledge-base/correctness/Avoid%20whitespaces%20in%20your%20replacement%20field%20names/9n9zeiZc

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.


Leave a Reply

Your email address will not be published. Required fields are marked *