, , ,

Explicit comparison to singleton can be unreadable

·

[vc_row][vc_column][nz_btn text=”Cost” link=”http://casthighlight.wpengine.com/category/product/indicators-methodology/cost/” target=”_self” icon=”icon-office” animate=”false” animation_type=”ghost” color=”green” size=”small” shape=”rounded” type=”normal” hover_normal=”opacity” hover_ghost=”fill”][nz_btn text=”Software Agility” 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=”Code Readability” 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”][vc_empty_space height=”12px”][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

Explicit comparison to singleton can be unreadable

[/vc_column_text][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]This code insight count one violation each time a condition use an explicit comparison to “none“, “false” or “true” (using operators == or !=).

Bad:

if number == None: print(“This works, but is not the preferred PEP 8 pattern”)

if number ! = None : print(“This works, but is not the preferred PEP 8 pattern”)

if otherNumber == false
print(“yooo !”)

if otherNumber != false
print(“yooo !”)

if T_flag == True :
print ( “This works, but is not the preferred PEP 8 pattern” )

if F_flag ! = True :
print ( “This works, but is not the preferred PEP 8 pattern” )

good:

if number is None : print ( “This works, but is not the preferred PEP 8 pattern” )

if not otherNumber
print ( “yooo !” )

# or better :
if not otherNumber and otherNumber is not None

print ( “yooo !” )

if not otherNumber
print ( “yooo !” )

 

if  T_ flag :

 

print ( “PEP 8 Style Guide prefers this pattern” )


# or moire restrictive check (exclude assimiled True values like non nul or non empty values)

  if  T_ flag is True :

print ( “PEP 8 Style Guide prefers this pattern” )


if   not F_ flag :

print ( “PEP 8 Style Guide prefers this pattern” )

[/vc_column_text][vc_empty_space height=”12px”][vc_empty_space height=”32px”][vc_single_image image=”5362″ img_link_large=”” img_link_target=”_self” img_size=”full” alignment=”none”][vc_row_inner][vc_column_inner width=”1/2″][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

Why you should care

[/vc_column_text][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]Per the PEP 8 Style Guide, the preferred way to compare something to  None  is the pattern  if Cond is None . This is only a guideline. It can be ignored if needed. But the purpose of the PEP 8 style guidelines is to improve the readability of code.

Python evaluates certain values as false when in a boolean context. A quick “rule of thumb” is that all “empty” values are considered false so 0, None, [], {}, '' all evaluate as false in a boolean context. So comparing to “false” may be confusing.

Moreover, conditions using Python booleans are easier to read and less error-prone. In most cases, they’re also faster

Per the PEP 8 Style Guide, the preferred ways to compare something to  True  are the patterns  if cond is True:  or if cond: . This is only a guideline. It can be ignored if needed. But the purpose of the PEP 8 Style Guide is to improve the readability of code.

 

[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

Business Impacts

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

[/vc_column_text][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/innovation/” icon=”icon-office”][/vc_column_inner][/vc_row_inner][vc_row_inner][vc_column_inner width=”1/2″][vc_separator color=”grey” align=”align_center” style=”” border_width=”” el_width=””][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

CAST recommendations

[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_separator color=”grey” align=”align_center” style=”” border_width=”” el_width=””][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

References

http://docs.quantifiedcode.com/python-code-patterns/readability/comparison_to_none.html

https://google.github.io/styleguide/pyguide.html#Conditional_Expressions

http://docs.quantifiedcode.com/python-code-patterns/readability/comparison_to_true.html[/vc_column_text][/vc_column_inner][/vc_row_inner][vc_row_inner][vc_column_inner width=”1/1″][vc_single_image image=”5362″ img_link_large=”” img_link_target=”_self” img_size=”full” alignment=”none”][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

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.

[/vc_column_text][vc_empty_space height=”16px”][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”][/vc_column_inner][/vc_row_inner][/vc_column][/vc_row][vc_row][vc_column][vc_column_text]

c

[/vc_column_text][/vc_column][/vc_row]


Leave a Reply

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