ECMAScript Array Instantiation
There are two ways of instantiating Arrays in EMCAScript (if you
exclude every conceivable hack that you could pull out of your hat
just to show me that there are other ways as well). One1 uses the
regular object instantiation syntax, namely new Array and
it’s also possible to omit the new in order to call Array
as a function instead (the latter is simply an alias of the former as
can be seen in the specification). The other way is leaner on the
fingers and is written as [], called the literal
notation2.
Conventionally I always use the literal notation (except for in a single case—which I’ll get to later). The reason is that the Array constructor is overloaded into two distinct functions where one is a special case. Essentially (although in practice, not exactly) the special case function is called if the constructor has a single argument whose type is [Number]. The normal case creates an array containing all objects passed as arguments to the constructor. The special case creates an Array with its length matching the value of the argument passed to the constructor. This is where the interface breaks down.
Suppose I want to create an Array of two numbers (1 and
2) and I’m using the regular instantiation syntax, this
is done by typing new Array (1, 2); (or, as described,
Array (1, 2);). If on the other hand I want to create an
array of only one number (3), what seems logical would be
to type new Array(3);. Wrong. What just got
instantiated was an array equal to one that can be created by the
literal notation [undefined, undefined, undefined]. There
is simply no way to create an Array of precisely one numerical element
using this syntax.
If instead the literal notation was used per convention, the problem would never occur.
This is why I suggest that the following convention should be in place:
The literal notation for instantiating Arrays should always be used when specific values are to be inserted into the array upon creation. If an Array of a known amount of elements is to be created, the regular object instantiation syntax should be used, as in
new Array (len). The code in place should make it clear that the argumentlenis numerical. Whether to use thenewprefix or not for this case will depend on other conventions.
As for the use of the special case, I can imagine cases where using it would be solely over-optimization. If for instance an Array is to be instantly filled to a known size (where we don’t want to add the elements in the same statement as we instantiate the Array) one could use the special case to prevent unnecessary resizing of the Array (Array being of the ArrayList ADT). However it seems to me that one should empirically prove that this is a bottleneck before such an operation is used. On the other hand I might be overly attached to the literal notation. I have a hard time seeing why this operation would hurt.
A case where an Array won’t be filled but still needs a certain size would be bulletproof usage of the special case.
Post new comment