Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
As an API designer, you should use \[varargs methods\] sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called. 

Noncompliant Code Example

In this noncompliant code example, overloading varargs methods makes it unclear which definition of the doSomething() method is invoked.

...

because the non-varargs definition is more specific and, consequently, a better fit for the arguments given. However, this complexity is best avoided.

Compliant Solution

To avoid overloading varargs methods, use distinct method names to ensure that the intended method is invoked, as shown in this compliant solution.

Code Block
bgColor#ccccff
class Varargs {
  private static void doSomething1(boolean... bool) {
    System.out.print("Number of arguments: " + bool.length + ", Contents: ");

    for (boolean b : bool)
      System.out.print("[" + b + "]");
  } 
  private static void doSomething2(boolean bool1, boolean bool2) {
    System.out.println("Overloaded method invoked");  
  }
  public static void main(String[] args) {
    doSomething1(true, false);
  }
}

Exceptions

Wiki Markup
*DCL08-EX1:* It may be desirable to violate this guideline for performance reasons. One such reason would be to avoid the cost of creating an array instance and initializing it on every invocation of a method \[[Bloch 2008|AA. Bibliography#Bloch 08]\].

...

When overloading varargs methods, it is important to avoid any ambiguity regarding which method would be invoked. This code sample avoids the possibility of incorrect method selection by using unambiguous method signatures.

Risk Assessment

Unmindful use of the varargs feature may create ambiguity and diminish code readability.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

DCL08 DCL01-J

low

unlikely

medium

P2

L3

Automated Detection

Automated detection is straightforward.

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="26cdbf80d9a383ea-1b26460b-4d4f4299-967c8bca-5da74c7921872fae47c8e140"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 42: "Use Varargs Judiciously"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ac8160e010839dca-1d0c7cf4-45ca4fcb-ab3a8839-70546d28ea7cff3a985e3233"><ac:plain-text-body><![CDATA[

[[Steinberg 2005

AA. Bibliography#Steinberg 05]]

"Using the Varargs Language Feature"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c7f8a990d1942122-f7151252-423649f8-bb15a9ac-1a579692328bcce2a2ffaf93"><ac:plain-text-body><![CDATA[

[[Sun 2006

AA. Bibliography#Sun 06]]

[varargs

http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html]

]]></ac:plain-text-body></ac:structured-macro>

...