, , ,

Array literals are less risky than Array constructors in JavaScript

·

[vc_row][vc_column][nz_btn text=”Software Resiliency” link=”http://casthighlight.wpengine.com/software-resiliency/” target=”_self” animate=”false” animation_type=”ghost” color=”pink” size=”medium” shape=”rounded” type=”normal” hover_normal=”fill” hover_ghost=”fill” icon=”icon-dashboard”][nz_btn text=”Code Reliability” target=”_self” animate=”false” animation_type=”ghost” color=”black” size=”medium” 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″]

Array literals are less risky than Array constructors in JavaScript

[/vc_column_text][vc_column_text pl=”0″ pr=”0″ pt=”0″ pb=”0″]This code insight counts one violation each time “new array(…)” is encountered.


bad :

Length is 3 :
var a1 = new Array(x1, 1.2, “string”);

Length is 2 :
var a2 = new Array(x1, x2);

Length is x1 or error if not an integer :
var a3 = new Array(x1);

Length is 0.
var a4 = new Array();

good:

Length is 3 :
var a = [1.5, x2, “string”];

Length is 2 :
var a2 = [x1, x2];

Length is 1 :
var a3 = [x1];
var a3 = [“string”];

Length is 0 :
var a4 = [];

 [/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″]The array literal syntax is simple, straightforward, and elegant. After all, an array is just a zero-indexed list of values. There’s no need to complicate things (and write more code) by including a constructor and using the new operator.

One more reason to stay away from new Array() is to avoid a possible trap that this constructor has in store for you.

When you pass a single number to the Array() constructor, it doesn’t become the value of the first array element. It sets the length of the array instead. This means that new

Array(3) creates an array with length of 3, but no actual elements.

Although this behavior might be a little unexpected, it gets worse when you pass a floating point number to new Array() as opposed to an integer. This results in an error because the floating point is not a valid value for the array’s length

Length is 3 :
var a1 = new Array(x1, x2, x3);

Length is 2 :
var a2 = new Array(x1, x2);

Length is x1 :
var a3 = new Array(x1);

Length is 0.
var a4 = new Array();
[/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

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

https://www.tutorialspoint.com/sql/sql-transactions.htm[/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]


Leave a Reply

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