, , ,

Avoid using risky patterns too frequently

·

[vc_row][vc_column][nz_btn text=”Software Resiliency” link=”http://casthighlight.wpengine.com/software-resiliency/” target=”_self” animate=”false” animation_type=”ghost” color=”black” size=”small” 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/programming-best-practices/” icon=”icon-code”][vc_empty_space height=”12px”][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″]Some specific code syntaxes, even if they compile and don’t make a software crash directly, are not recommended mainly for two reasons:

      [/vc_column_text][vc_empty_space height=”6px”][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

      • They’re often obscure and/or implicit and could definitely lead to misinterpretations by junior developers, which frequently means unexpected bugs when the code is modified
      • When located around these blurry syntaxes (coding gurus will qualify them as “art”), real bugs are much harder to catch, especially because the syntax doesn’t bestially describe what it does and needs advanced interpretations

To illustrate the confusion these risky patterns could generate, take a look at this real-life example below found on GitHub (line #421 and #424):

left_child = new_left_child;
left_weight = left_weight;
right_child = new_right_child;
right_weight = right_weight;

And most importantly, look at the issue and doubts this variable self-assignment has raised within the development team:[/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

Using risky syntax can lead to increased bugs in an application, since it makes troubleshooting and expansion of existing code needlessly complicated. This, in turn, will increase the time and effort that developers have to spend maintaining and updating a given codebase.

[/vc_column_text][vc_empty_space height=”6px”][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_single_image image=”7633″ img_link_large=”” img_link_target=”_self” img_size=”full” alignment=”none”][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″]

CAST Recommendations

When possible, software should use the most simple and explicit code syntax to perform actions. While some development gurus say they can do more with less code, it generally means spending more time for small bugs.[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

References

https://stackoverflow.com/questions/11008030/a-for-loop-without-any[/vc_column_text][nz_btn text=”Style Guide” target=”_self” animate=”false” animation_type=”ghost” color=”turquoise” size=”small” shape=”rounded” type=”ghost” hover_normal=”opacity” hover_ghost=”fill” link=”https://github.com/Kristories/awesome-guidelines” icon=”icon-book”][/vc_column_inner][/vc_row_inner][vc_separator color=”grey” align=”align_center” style=”” border_width=”” el_width=””][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

How we detect

[/vc_column_text][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]

This code insight verifies that the source code doesn’t contain technology-specific patterns that tend to generate unexpected bugs. Below are listed some examples for the technologies Highlight supports. Based on specific thresholds CAST has defined over time, Highlight counts penalty points for the scanned file.

C/C++ and Objective-C

while(…);
for(…);
if(…);
a = a;
a == b;
* expr ; with expr containing no function call nor assignment operator.

VB/VB.Net

: <end of line>
<begin of line> :
::
=
…….. = (no colon or coma between the peer of equals)

C#

while(…);
for(…);
foreach(…);
if(…);
a = a ;
a == b ;

Java and JSP

while(…);
for(…);
if(…);
a = a ;
a == b ;

[/vc_column_text][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]


Leave a Reply

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