Whitespaces are a Production Risk

[nz_btn text="Production Risk" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/risk/" target="_self" icon="icon-office" animate="false" animation_type="ghost" color="pink" size="small" shape="rounded" type="normal" hover_normal="opacity" hover_ghost="fill"][nz_btn text="Software Resiliency" link="http://casthighlight.wpengine.com/software-resiliency/" target="_self" animate="false" animation_type="ghost" color="black" size="medium" shape="rounded" type="normal" hover_normal="fill" hover_ghost="fill" icon="icon-dashboard"][nz_btn text="Programming Best Practices" target="_self" animate="false" animation_type="ghost" color="black" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="screen" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/code-insights/software-resiliency/code-reliability/" icon="icon-code"]

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

[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/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.
[nz_btn text="See features" link="http://casthighlight.wpengine.com/outputs-analytics/" target="_self" icon="icon-stats" animate="false" animation_type="ghost" color="black" size="small" shape="rounded" type="normal" hover_normal="fill" hover_ghost="fill"][nz_btn text="How it works" link="http://casthighlight.wpengine.com/how-it-works/" target="_self" icon="icon-cog" animate="false" animation_type="ghost" color="black" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="fill"]
For reference only. For the complete details please refer the original article
https://doc.casthighlight.com/alt_stringformat-avoid-whitespaces-in-replacement-field-names/
Have more questions? Submit a request

Comments

Powered by Zendesk