
在java编程中,尤其是在构造器或方法中,当需要对多个相似参数进行条件判断和赋值时,很容易出现一系列重复的if/else语句。这不仅会使得代码显得冗长,降低可读性,还会增加未来维护和修改的难度。本教程将深入探讨几种有效的重构技巧,帮助开发者摆脱这种“if语句地狱”,提升代码质量。
当if/else分支仅仅用于根据条件选择一个值进行赋值时,条件运算符(? :)提供了一种更简洁的写法。它将条件、真值和假值紧凑地表达在一行代码中,极大地减少了代码量。
原始代码示例(部分):
if (cpuOne == 0.5) {
this.cpuOne = full;
} else {
this.cpuOne = cpuOne;
}
// 类似的if/else块重复了四次优化后的代码示例:
public Latency(final double full, final double cpuOne, final double cpuTwo, final double cpuThree, final double cpuFour) {
// ... 初始参数校验部分 ...
this.full = full;
// 使用条件运算符简化赋值
this.cpuOne = cpuOne == 0.5 ? full : cpuOne;
this.cpuTwo = cpuTwo == 0.5 ? full : cpuTwo;
this.cpuThree = cpuThree == 0.5 ? full : cpuThree;
this.cpuFour = cpuFour == 0.5 ? full : cpuFour;
}优点:
立即学习“Java免费学习笔记(深入)”;
注意事项:
当多个if/else块执行相似的逻辑时,将这部分逻辑封装到一个私有辅助方法中,可以有效消除代码重复,遵循DRY(Don't Repeat Yourself)原则。这不仅使代码更易于理解,也方便未来的修改和测试。
辅助方法示例:
private static double changeHalfToFull(double value, double full) {
if (value == 0.5) {
return full;
} else {
return value;
}
}在构造器中调用:
public Latency(final double full, final double cpuOne, final double cpuTwo, final double cpuThree, final double cpuFour) {
// ... 初始参数校验部分 ...
this.full = full;
this.cpuOne = changeHalfToFull(cpuOne, full);
this.cpuTwo = changeHalfToFull(cpuTwo, full);
this.cpuThree = changeHalfToFull(cpuThree, full);
this.cpuFour = changeHalfToFull(cpuFour, full);
}优点:
立即学习“Java免费学习笔记(深入)”;
如果多个变量(如cpuOne, cpuTwo等)在语义上是同类型的数据,例如表示一组CPU的延迟值,那么将它们组织成一个数组是更合理的设计。这不仅能减少构造器参数的数量,还能通过循环结构简化处理逻辑。
构造器参数和内部处理使用数组:
public class Latency {
private double full;
private double[] cpuValues; // 使用数组存储CPU值
public Latency(final double full, final double[] cpuValues) {
// 参数校验:
if (full > 10.0 || full <= 0.0) {
throw new IllegalArgumentException("Must check the values for full.");
}
if (cpuValues == null || cpuValues.length == 0) {
throw new IllegalArgumentException("cpuValues cannot be null or empty.");
}
// 校验至少一个cpu值不为0(根据原始逻辑)
boolean allCpuZero = true;
for (double cpu : cpuValues) {
if (cpu != 0) {
allCpuZero = false;
break;
}
}
if (allCpuZero) {
throw new IllegalArgumentException("Not all cpu values can be zero.");
}
this.full = full;
this.cpuValues = new double[cpuValues.length]; // 初始化内部数组
for (int i = 0; i < cpuValues.length; i++) {
if (cpuValues[i] == 0.5) {
this.cpuValues[i] = full;
} else {
this.cpuValues[i] = cpuValues[i];
}
}
}
// ... 其他方法 ...
}优点:
立即学习“Java免费学习笔记(深入)”;
最强大的优化往往是多种策略的结合。例如,我们可以将“提取公共逻辑到独立方法”与“利用数组”结合起来,实现高度简洁且可维护的代码。
综合优化示例:
public class Latency {
private double full;
private double[] cpuValues;
public Latency(final double full, final double[] cpuValues) {
// 参数校验(与上例相同)
if (full > 10.0 || full <= 0.0) {
throw new IllegalArgumentException("Must check the values for full.");
}
if (cpuValues == null || cpuValues.length == 0) {
throw new IllegalArgumentException("cpuValues cannot be null or empty.");
}
boolean allCpuZero = true;
for (double cpu : cpuValues) {
if (cpu != 0) {
allCpuZero = false;
break;
}
}
if (allCpuZero) {
throw new IllegalArgumentException("Not all cpu values can be zero.");
}
this.full = full;
this.cpuValues = new double[cpuValues.length];
for (int i = 0; i < cpuValues.length; i++) {
// 结合了数组遍历和辅助方法
this.cpuValues[i] = changeHalfToFull(cpuValues[i], full);
}
}
// 私有辅助方法,可进一步使用条件运算符
private static double changeHalfToFull(double value, double full) {
return value == 0.5 ? full : value;
}
// ... 其他方法 ...
}优点:
立即学习“Java免费学习笔记(深入)”;
优化Java构造器中的多重if语句是一个常见的重构任务,旨在提高代码的可读性、可维护性和扩展性。
通过以上策略,开发者可以有效地重构Java构造器中冗余的if语句,使代码更加健壮、优雅和易于管理。
以上就是Java构造器中多重if语句的优化实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号