rspec/rules/S3878/rule.adoc

45 lines
1.0 KiB
Plaintext
Raw Normal View History

2020-12-23 14:59:06 +01:00
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?
2020-06-30 12:48:39 +02:00
== Noncompliant Code Example
----
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
----
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) {
// ...
}
----