rspec/rules/S3878/rule.adoc

47 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +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
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
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
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
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) {
// ...
}
----