Java Reference
In-Depth Information
Itmaybedesirabletoviolatethisruleforperformancereasons.Onesuchreasonwould
be to avoid the cost of creating an array instance and initializing it on every invocation of
a method [Bloch 2008].
Click here to view code image
public void foo() { }
public void foo(int a1) { }
public void foo(int a1, int a2, int... rest) { }
When overloading variable arity methods, it is important to avoid any ambiguity re-
garding which method should be invoked. The preceding code sample avoids the possib-
ility of incorrect method selection by using unambiguous method signatures.
Automated detection is straightforward.
Bibliography
[Bloch 2008]
Item 42, “Use Varargs Judiciously”
[Steinberg 2008]
Using the Varargs Language Feature
[Oracle 2011b]
Varargs
52. Avoid in-band error indicators
Anin-banderrorindicatorisavaluereturnedbyamethodthatindicateseitheralegitimate
return value or an illegitimate value that denotes an error. Some common examples of in-
band error indicators include
A valid object or a null reference.
An integer indicating a positive value, or −1 to indicate that an error occurred.
An array of valid objects or a null reference indicating the absence of valid ob-
jects. (This topic is further addressed in Guideline 41 , “ Return an empty array or
collection instead of a null value for methods that return an array or collection . ”)
In-banderrorindicatorsrequirethecallertocheckfortheerror;however,thischecking
is often overlooked. Failure to check for such error conditions not only violates The
CERT ® Oracle ® Secure Coding Standard for Java [Long 2012], “EXP00-J. Do not ig-
Search WWH ::




Custom Search