Whitespaces are a Production Risk
Count one violation each time a format replacement field contains a whitespace. badprint "{ 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
[nz_btn text="Production Risk" target="_self" animate="false" animation_type="ghost" color="pink" size="small" shape="rounded" type="normal" hover_normal="opacity" hover_ghost="fill" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/innovation/" icon="icon-office"]CAST recommendations
References
https://www.quantifiedcode.com/knowledge-base/correctness/Avoid%20whitespaces%20in%20your%20replacement%20field%20names/9n9zeiZcAbout 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.
For reference only. For the complete details please refer the original article
https://doc.casthighlight.com/alt_stringformat-avoid-whitespaces-in-replacement-field-names/
https://doc.casthighlight.com/alt_stringformat-avoid-whitespaces-in-replacement-field-names/
Comments