Java 拟环复杂度大-克隆对象更好的方法?
我的度量工具说copyTo方法的圈复杂度为44。太大了。 如何使用更好更快的方法创建地址对象的克隆?注意:我没有可用的可克隆接口移动应用程序 下面是课堂:Java 拟环复杂度大-克隆对象更好的方法?,java,Java,我的度量工具说copyTo方法的圈复杂度为44。太大了。 如何使用更好更快的方法创建地址对象的克隆?注意:我没有可用的可克隆接口移动应用程序 下面是课堂: class Address { Address() { init(); } String company1; String company2; String degree; String firstName; String lastName; Strin
class Address
{
Address()
{
init();
}
String company1;
String company2;
String degree;
String firstName;
String lastName;
String salutation;
String toPerson;
String poBox;
String country;
String postalCode;
String place;
String street;
String telephone1;
String telephone2;
String mobilePhone;
String privatePhone;
String fax;
String eMail;
String homepage;
String socialSecurityNumber;
String bank1Name;
String bank1BCN;
String bank1AccountNr;
String bank2Name;
String bank2BCN;
String bank2AccountNr;
String free01Name;
String free01Value;
String free02Name;
String free02Value;
String free03Name;
String free03Value;
String free04Name;
String free04Value;
String free05Name;
String free05Value;
String comment;
String cityCode;
GpsPosition position;
int recId;
int recStatus;
DateTime recCreated;
String recCreatedBy;
String recCreatedByProg;
DateTime recChanged;
String recChangedBy;
String recChangedByProg;
AddressPropertyUsage usage;
/* Generates a duplicate of the Address object */
Address clone()
{
Address clonedObject = new Address();
copyTo(clonedObject);
return clonedObject;
}
void copyTo(Address destination)
{
if (destination.usage == null)
destination.usage = new AddressPropertyUsage(this.usage.value);
else
destination.usage.value = this.usage.value;
destination.recId = this.recId;
destination.recStatus = this.recStatus;
destination.recChanged = this.recChanged.clone();
if (this.recChangedBy == null)
destination.recChangedBy = null;
else
destination.recChangedBy = this.recChangedBy.substring(0);
if (this.recChangedByProg == null)
destination.recChangedByProg = null;
else
destination.recChangedByProg = this.recChangedByProg.substring(0);
destination.recCreated = this.recCreated.clone();
if (this.recCreatedBy == null)
destination.recCreatedBy = null;
else
destination.recCreatedBy = this.recCreatedBy.substring(0);
if (this.recCreatedByProg == null)
destination.recCreatedByProg = null;
else
destination.recCreatedByProg = this.recCreatedByProg.substring(0);
if (this.company1 == null)
destination.company1 = null;
else
destination.company1 = this.company1.substring(0);
if (this.company2 == null)
destination.company2 = null;
else
destination.company2 = this.company2.substring(0);
if (this.degree == null)
destination.degree = null;
else
destination.degree = this.degree.substring(0);
if (this.firstName == null)
destination.firstName = null;
else
destination.firstName = this.firstName.substring(0);
if (this.lastName == null)
destination.lastName = null;
else
destination.lastName = this.lastName.substring(0);
if (this.street == null)
destination.street = null;
else
destination.street = this.street.substring(0);
if (this.postalCode == null)
destination.postalCode = null;
else
destination.postalCode = this.postalCode.substring(0);
if (this.place == null)
destination.place = null;
else
destination.place = this.place.substring(0);
if (this.country == null)
destination.country = null;
else
destination.country = this.country.substring(0);
if (this.poBox == null)
destination.poBox = null;
else
destination.poBox = this.poBox.substring(0);
if (this.telephone1 == null)
destination.telephone1 = null;
else
destination.telephone1 = this.telephone1.substring(0);
if (this.telephone2 == null)
destination.telephone2 = null;
else
destination.telephone2 = this.telephone2.substring(0);
if (this.mobilePhone == null)
destination.mobilePhone = null;
else
destination.mobilePhone = this.mobilePhone.substring(0);
if (this.fax == null)
destination.fax = null;
else
destination.fax = this.fax.substring(0);
if (this.eMail == null)
destination.eMail = null;
else
destination.eMail = this.eMail.substring(0);
if (this.homepage == null)
destination.homepage = null;
else
destination.homepage = this.homepage.substring(0);
if (this.salutation == null)
destination.salutation = null;
else
destination.salutation = this.salutation.substring(0);
if (this.toPerson == null)
destination.toPerson = null;
else
destination.toPerson = this.toPerson.substring(0);
if (this.privatePhone == null)
destination.privatePhone = null;
else
destination.privatePhone = this.privatePhone.substring(0);
if (this.comment == null)
destination.comment = null;
else
destination.comment = this.comment.substring(0);
if (this.cityCode == null)
destination.cityCode = null;
else
destination.cityCode = this.cityCode.substring(0);
this.position.copyTo(destination.position);
if (this.bank1AccountNr == null)
destination.bank1AccountNr = null;
else
destination.bank1AccountNr = this.bank1AccountNr.substring(0);
if (this.bank1BCN == null)
destination.bank1BCN = null;
else
destination.bank1BCN = this.bank1BCN.substring(0);
if (this.bank1Name == null)
destination.bank1Name = null;
else
destination.bank1Name = this.bank1Name.substring(0);
if (this.bank2AccountNr == null)
destination.bank2AccountNr = null;
else
destination.bank2AccountNr = this.bank2AccountNr.substring(0);
if (this.bank2BCN == null)
destination.bank2BCN = null;
else
destination.bank2BCN = this.bank2BCN.substring(0);
if (this.bank2Name == null)
destination.bank2Name = null;
else
destination.bank2Name = this.bank2Name.substring(0);
if (this.socialSecurityNumber == null)
destination.socialSecurityNumber = null;
else
destination.socialSecurityNumber = this.socialSecurityNumber.substring(0);
if (this.free01Name == null)
destination.free01Name = null;
else
destination.free01Name = this.free01Name.substring(0);
if (this.free01Value == null)
destination.free01Value = null;
else
destination.free01Value = this.free01Value.substring(0);
if (this.free02Name == null)
destination.free02Name = null;
else
destination.free02Name = this.free02Name.substring(0);
if (this.free02Value == null)
destination.free02Value = null;
else
destination.free02Value = this.free02Value.substring(0);
if (this.free03Name == null)
destination.free03Name = null;
else
destination.free03Name = this.free03Name.substring(0);
if (this.free03Value == null)
destination.free03Value = null;
else
destination.free03Value = this.free03Value.substring(0);
if (this.free04Name == null)
destination.free04Name = null;
else
destination.free04Name = this.free04Name.substring(0);
if (this.free04Value == null)
destination.free04Value = null;
else
destination.free04Value = this.free04Value.substring(0);
if (this.free05Name == null)
destination.free05Name = null;
else
destination.free05Name = this.free05Name.substring(0);
if (this.free05Value == null)
destination.free05Value = null;
else
destination.free05Value = this.free05Value.substring(0);
}
private void init()
{
this.company1 = StringHelper.emptyString;
this.company2 = StringHelper.emptyString;
this.degree = StringHelper.emptyString;
this.firstName = StringHelper.emptyString;
this.lastName = StringHelper.emptyString;
this.salutation = StringHelper.emptyString;
this.toPerson = StringHelper.emptyString;
this.poBox = StringHelper.emptyString;
this.country = StringHelper.emptyString;
this.postalCode = StringHelper.emptyString;
this.place = StringHelper.emptyString;
this.street = StringHelper.emptyString;
this.telephone1 = StringHelper.emptyString;
this.telephone2 = StringHelper.emptyString;
this.mobilePhone = StringHelper.emptyString;
this.privatePhone = StringHelper.emptyString;
this.fax = StringHelper.emptyString;
this.eMail = StringHelper.emptyString;
this.homepage = StringHelper.emptyString;
this.socialSecurityNumber = StringHelper.emptyString;
this.bank1Name = StringHelper.emptyString;
this.bank1BCN = StringHelper.emptyString;
this.bank1AccountNr = StringHelper.emptyString;
this.bank2Name = StringHelper.emptyString;
this.bank2BCN = StringHelper.emptyString;
this.bank2AccountNr = StringHelper.emptyString;
this.free01Name = StringHelper.emptyString;
this.free01Value = StringHelper.emptyString;
this.free02Name = StringHelper.emptyString;
this.free02Value = StringHelper.emptyString;
this.free03Name = StringHelper.emptyString;
this.free03Value = StringHelper.emptyString;
this.free04Name = StringHelper.emptyString;
this.free04Value = StringHelper.emptyString;
this.free05Name = StringHelper.emptyString;
this.free05Value = StringHelper.emptyString;
this.comment = StringHelper.emptyString;
this.cityCode = StringHelper.emptyString;
this.position = new GpsPosition();
this.recId = 0;
this.recStatus = -1;
this.recCreated = DateTime.UNKNOWN_PC.clone();
this.recCreatedBy = StringHelper.emptyString;
this.recCreatedByProg = StringHelper.emptyString;
this.recChanged = DateTime.UNKNOWN_PC.clone();
this.recChangedBy = StringHelper.emptyString;
this.recChangedByProg = StringHelper.emptyString;
this.usage = new AddressPropertyUsage(AddressPropertyUsage.VERSION3_ALL);
}
你有很多这样的someString.substring0如果我计算正确的话会发生43次。由于字符串在Java中是不可变的,只需分配它们就足够了,因此null检查将过时。如果我计算正确,您有很多这样的someString.substring0。由于字符串在Java中是不可变的,只需分配它们就足够了,因此null检查将过时。您试图通过.substring0调用实现什么 这将创建另一个与原始对象具有相同底层char数组的对象,我可以看到这可能会吸引您使用它。但你为什么还需要另一件东西呢?字符串是不可变的,所以地址的两个实例可以直接使用相同的字符串 一旦排除了这种情况,您就不需要任何if语句,可以直接赋值,例如:
// No need to create new instances of String...
// if (this.recChangedBy == null)
// destination.recChangedBy = null;
// else
// destination.recChangedBy = this.recChangedBy.substring(0);
// so above is replaced by:
destination.recChangedBy = this.recChangedBy;
瞧,大多数/所有的条件句都消失了,圈复杂度急剧下降
在不同的主题上,您的地址多久修改一次?我很想让它成为一个不可变的对象,并在构造函数中设置所有的值,这将使它更容易推理和使用。尤其是与构造空地址相比
也不要使用调用私有init方法的构造函数——只需将该方法的主体设置为构造函数即可
我不认为地址真的有44个字段,这看起来像一个。如果Address类仅表示地址,银行详细信息转到Account类,最后的更改详细信息出现在AuditDetails类中,等等,那么代码可能会更清晰。您试图通过.substring0调用实现什么 这将创建另一个与原始对象具有相同底层char数组的对象,我可以看到这可能会吸引您使用它。但你为什么还需要另一件东西呢?字符串是不可变的,所以地址的两个实例可以直接使用相同的字符串 一旦排除了这种情况,您就不需要任何if语句,可以直接赋值,例如:
// No need to create new instances of String...
// if (this.recChangedBy == null)
// destination.recChangedBy = null;
// else
// destination.recChangedBy = this.recChangedBy.substring(0);
// so above is replaced by:
destination.recChangedBy = this.recChangedBy;
瞧,大多数/所有的条件句都消失了,圈复杂度急剧下降
在不同的主题上,您的地址多久修改一次?我很想让它成为一个不可变的对象,并在构造函数中设置所有的值,这将使它更容易推理和使用。尤其是与构造空地址相比
也不要使用调用私有init方法的构造函数——只需将该方法的主体设置为构造函数即可
我不认为地址真的有44个字段,这看起来像一个。如果Address类仅表示地址,并且银行详细信息转到Account类,最后一次更改详细信息出现在AuditDetails类中,则代码可能更清晰。您的Address类包含太多字段。理想情况下,您应该将对象拆分为许多小对象,如:BankInfo、RecInfo、PhoneInfo、FreeValues等,然后让Address类包含这些对象的变量。您的Address类包含太多字段。理想情况下,您应该将对象拆分为许多小对象,如:BankInfo、RecInfo、PhoneInfo、FreeValues等,然后让Address类包含这些对象的变量。我同意其他人的看法,他们说您不应该尝试复制字符串,不这样做可以消除条件。但值得补充的是:如果您确实需要进行条件复制(例如,如果这些东西不是字符串而是其他类型的对象),那么您最好排除重复的逻辑:
String copyUnlessNull(String s) {
if (s==null) return null;
return s.substring(0);
}
void copyTo(Address destination) {
// ...
destination.recChangedBy = copyUnlessNull(this.recChangedBy);
destination.recChangedByProg = copyUnlessNull(this.recChangedByProg);
// ...
}
为免生疑问,在这种特殊情况下,上述方法不是正确的解决方案-您不应该为子字符串的内容而烦恼,然后您就不需要条件逻辑-但是每当您发现自己一遍又一遍地编写相同的代码时,你应该寻找一种只写一次并重复使用的方法。我同意其他人的观点,他们说你不应该试图复制字符串,不这样做可以让你摆脱条件。但值得补充的是:如果您确实需要进行条件复制(例如,如果这些东西不是字符串而是其他类型的对象),那么您最好排除重复的逻辑:
String copyUnlessNull(String s) {
if (s==null) return null;
return s.substring(0);
}
void copyTo(Address destination) {
// ...
destination.recChangedBy = copyUnlessNull(this.recChangedBy);
destination.recChangedByProg = copyUnlessNull(this.recChangedByProg);
// ...
}
为免生疑问,在这种特殊情况下,上述方法不是正确的解决方案-您不应该为子字符串的内容而烦恼,然后您就不需要条件逻辑-但是
每当您发现自己一遍又一遍地编写相同的代码时,您应该寻找一种只编写一次并重复使用的方法