Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2022.2

The variable arity (varargs) feature was introduced in the JDK v1.5.0 to support methods that accept a variable numbers of arguments.unmigrated-wiki-markup

According to the Java SE 6 documentation \[[Sun 2006|AA. Bibliography#Sun 06]\]:[Oracle 2011b],

As an API designer, you should use [variable arity 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. Wiki MarkupAs 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 variable arity methods makes it unclear which definition of the doSomethingdisplayBooleans() method is invoked.:

Code Block
bgColor#FFCCCC

class Varargs {
  private static void doSomethingdisplayBooleans(boolean... bool) {
    System.out.print("Number of arguments: " + bool.length + ", Contents: ");

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

When run, this program outputs:

Code Block

Overloaded method invoked

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

Compliant Solution

To avoid overloading varargs variable arity 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 doSomething1displayManyBooleans(boolean... bool) {
    System.out.print("Number of arguments: " + bool.length + ", Contents: ");

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

Exceptions

Applicability

Injudicious use of overloaded variable arity methods may create ambiguity and diminish code readability.

It may be desirable to violate this rule 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 Wiki Markup*DCL01-EX1:* It may be desirable to violate this rule 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]\].

Code Block
bgColor#ccccff

public void foo() { }
public void foo(int a1) { }
public void foo(int a1, int a2, int... rest) { }

When overloading varargs variable arity methods, it is important to avoid any ambiguity regarding which method would should be invoked. This The preceding 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

DCL01-J

low

unlikely

medium

P2

L3

Automated Detection

Automated detection is straightforward.

Bibliography

Automated detection is straightforward.

Automated Detection

ToolVersionCheckerDescription
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.DCL57.OVAMAvoid overloading varargs methods

Bibliography

[Bloch 2008]

Item 42, "Use Varargs Judiciously"

[Steinberg 2008]

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5fbbcf6a-fe62-42c8-abe2-9dff0503fb21"><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="2280db53-2f9d-486a-815d-c4196c99f5db"><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="b96add63-5579-431d-ac3c-dd45b98f6946"><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>

[Oracle 2011b]

varargs


...

Image Added Image Added Image AddedDCL00-J. Declare all enhanced for statement loop variables to be final      01. Declarations and Initialization (DCL)      DCL02-J. Enable compile-time type checking of varargs types