方法中的字符串参数太多
Too much String parameters in method
有时,我在对我的代码进行一次 PMD 检查时遇到问题。我的意思是 - 字符串参数太多:
"Rather than using a lot of String arguments, consider using a container object for those values"
f.e.:
public void printPersons (final String name, final String surname, final String day, final String car, final String city, final String petName)
{
}
如何在您的项目中避免这种情况?
您可以定义一个简单的 class 来封装所有这些字段,并将其传递给您的原始方法。
public class Person {
private final String name;
private final String surname;
private final String day;
private final String car;
private final String city;
private final String petName;
public Person(final String name, final String surname, final String day, final String car, final String city, final String petName) {
this.name = name;
this.surname = surname;
this.day = day;
this.car = car;
this.city = city;
this.petName = petName;
}
public String getName() {
return this.name;
}
public String getSurname() {
return this.surname;
}
public String getDay() {
return this.day;
}
public String getCar() {
return this.car;
}
public String getCity() {
return this.city;
}
public String getPetName() {
return this.petName;
}
}
您的方法调用现在变为:
public void printPersons(Person person) {}
对我来说,在这里使用容器对象(Person)是毫无疑问的。为了使 Person 实例的构造更容易,使用了内部 class PersonBuilder,没有公开构造函数或 setter。
public final class Person {
private final String name;
private final String surname;
private final String day;
private final String car;
// all the other attributes omitted
public static void main(final String[] args) {
// usage example
Person person = Person.builder().name("Name").surname("Surname")
.car("Tesla").day("Friday").build();
person.toString();
}
// private constructor, only used by the PersonBuilder
private Person(final PersonBuilder pb) {
this.name = pb.name;
this.surname = pb.surname;
this.day = pb.day;
this.car = pb.car;
}
public static PersonBuilder builder() {
return new PersonBuilder();
}
public String toString() {
return "return a nicely formatted String for printing, logging etc.";
}
/**
* expose a nice, fluent API to construct Persons.
*/
public static final class PersonBuilder {
private String name;
private String surname;
private String day;
private String car;
// use the static method Person.builder() to get an instance.
private PersonBuilder() {
}
public PersonBuilder surname(final String surname) {
this.surname = surname;
return this;
}
public PersonBuilder name(final String name) {
this.name = name;
return this;
}
public PersonBuilder day(final String day) {
this.day = day;
return this;
}
public PersonBuilder car(final String car) {
this.car = car;
return this;
}
public Person build() {
return new Person(this);
}
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public String getDay() {
return day;
}
public String getCar() {
return car;
}
}
有时,我在对我的代码进行一次 PMD 检查时遇到问题。我的意思是 - 字符串参数太多:
"Rather than using a lot of String arguments, consider using a container object for those values"
f.e.:
public void printPersons (final String name, final String surname, final String day, final String car, final String city, final String petName)
{
}
如何在您的项目中避免这种情况?
您可以定义一个简单的 class 来封装所有这些字段,并将其传递给您的原始方法。
public class Person {
private final String name;
private final String surname;
private final String day;
private final String car;
private final String city;
private final String petName;
public Person(final String name, final String surname, final String day, final String car, final String city, final String petName) {
this.name = name;
this.surname = surname;
this.day = day;
this.car = car;
this.city = city;
this.petName = petName;
}
public String getName() {
return this.name;
}
public String getSurname() {
return this.surname;
}
public String getDay() {
return this.day;
}
public String getCar() {
return this.car;
}
public String getCity() {
return this.city;
}
public String getPetName() {
return this.petName;
}
}
您的方法调用现在变为:
public void printPersons(Person person) {}
对我来说,在这里使用容器对象(Person)是毫无疑问的。为了使 Person 实例的构造更容易,使用了内部 class PersonBuilder,没有公开构造函数或 setter。
public final class Person {
private final String name;
private final String surname;
private final String day;
private final String car;
// all the other attributes omitted
public static void main(final String[] args) {
// usage example
Person person = Person.builder().name("Name").surname("Surname")
.car("Tesla").day("Friday").build();
person.toString();
}
// private constructor, only used by the PersonBuilder
private Person(final PersonBuilder pb) {
this.name = pb.name;
this.surname = pb.surname;
this.day = pb.day;
this.car = pb.car;
}
public static PersonBuilder builder() {
return new PersonBuilder();
}
public String toString() {
return "return a nicely formatted String for printing, logging etc.";
}
/**
* expose a nice, fluent API to construct Persons.
*/
public static final class PersonBuilder {
private String name;
private String surname;
private String day;
private String car;
// use the static method Person.builder() to get an instance.
private PersonBuilder() {
}
public PersonBuilder surname(final String surname) {
this.surname = surname;
return this;
}
public PersonBuilder name(final String name) {
this.name = name;
return this;
}
public PersonBuilder day(final String day) {
this.day = day;
return this;
}
public PersonBuilder car(final String car) {
this.car = car;
return this;
}
public Person build() {
return new Person(this);
}
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public String getDay() {
return day;
}
public String getCar() {
return car;
}
}