Why you should care
Avoid extraneous whitespace in the following situations:-
Immediately inside parentheses, brackets or braces.
Yes: spam(ham[1], {eggs: 2}) No: spam( ham[ 1 ], { eggs: 2 } ) -
Immediately before a:
-
comma
-
semicolon
-
colon (except when using as slice operator)
Yes: if x == 4: print x, y; x, y = y, x No: if x == 4 : print x , y ; x , y = y , x
-
-
Immediately before the open parenthesis that starts the argument list of a function call:
Yes: spam(1) No: spam (1)
-
Immediately before the open parenthesis that starts an indexing or slicing:
Yes: dct['key'] = lst[index] No: dct ['key'] = lst [index]
-
More than one space around an assignment (or other) operator to align it with another.
Yes:x = 1 y = 2 long_variable = 3
No:x = 1 y = 2 long_variable = 3
-
Don’t use spaces around the = sign when used to indicate a keyword argument or a default parameter value.
Yes:def complex(real, imag=0.0): return magic(r=real, i=imag)No:def complex(real, imag = 0.0): return magic(r = real, i = imag)
Business Impacts
It is recommended to avoid these in order to ensure the code is more readable and cost effective.[nz_btn text="Cost" target="_self" animate="false" animation_type="ghost" color="green" size="small" shape="rounded" type="normal" hover_normal="opacity" hover_ghost="fill" link="http://casthighlight.wpengine.com/category/product/indicators-methodology/cost/" icon="icon-office"][nz_btn text="Time / Effort" target="_self" animate="false" animation_type="ghost" color="blue" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="screen" icon="icon-clock"]CAST Recommendations
CAST recommends having no space between function name and parenthesis. The JavaScript style guidelines by Douglas Crockford establish very consistent rules that help the code work well and perform with cost efficiency.References
https://stackoverflow.com/questions/9765942/space-after-function-name-is-wrong[nz_btn text="Style Guide" target="_self" animate="false" animation_type="ghost" color="turquoise" size="small" shape="rounded" type="ghost" hover_normal="fill" hover_ghost="screen" link="https://github.com/Kristories/awesome-guidelines" icon="icon-book"]How we detect
This code insight count one violation each time a whitespace is encountered :just before :
- openning parenthesis following an identifier (except language keywords like : or, and, if, import, in,…)
- closing parents (when preceded by number or identifier)
- openning brackets following an identifier
- closing bracket (when preceded by number or identifier)
- closing braces (when preceded by number or identifier)
- coma
- semicolon
- colon (except when using as slice operator)
- openning parenthese (when followed by number or identifier)
- openning bracket (when followed by number or identifier)
- openning braces (when followed by number or identifier)
- assignment operator when used to indicate a keyword argument or a default parameter value
- assignment operator
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.
For reference only. For the complete details please refer the original article
https://doc.casthighlight.com/alt_badspacing-avoid-extraneous-whitespaces/
https://doc.casthighlight.com/alt_badspacing-avoid-extraneous-whitespaces/
Comments