49 lines
1.1 KiB
Plaintext
49 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
There's no point in creating an array solely for the purpose of passing it as a varargs (``++...++``) argument; varargs _is_ an array. Simply pass the elements directly. They will be consolidated into an array automatically. Incidentally passing an array where ``++Object ...++`` is expected makes the intent ambiguous: Is the array supposed to be one object or a collection of objects?
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,text]
|
|
----
|
|
public void callTheThing() {
|
|
//...
|
|
doTheThing(new String[] { "s1", "s2"}); // Noncompliant: unnecessary
|
|
doTheThing(new String[12]); // Compliant
|
|
doTheOtherThing(new String[8]); // Noncompliant: ambiguous
|
|
// ...
|
|
}
|
|
|
|
public void doTheThing (String ... args) {
|
|
// ...
|
|
}
|
|
|
|
public void doTheOtherThing(Object ... args) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,text]
|
|
----
|
|
public void callTheThing() {
|
|
//...
|
|
doTheThing("s1", "s2");
|
|
doTheThing(new String[12]);
|
|
doTheOtherThing((Object[]) new String[8]);
|
|
// ...
|
|
}
|
|
|
|
public void doTheThing (String ... args) {
|
|
// ...
|
|
}
|
|
|
|
public void doTheOtherThing(Object ... args) {
|
|
// ...
|
|
}
|
|
----
|
|
|