
在java编程中,尤其是在构造函数或方法内部,我们常常会遇到需要根据不同条件进行参数校验或值初始化的场景。当这些条件判断逻辑变得复杂且重复时,往往会导致一长串的if-else语句,使代码变得冗长、难以阅读和维护。例如,以下是一个典型的案例,在一个latency类的构造函数中,存在多重if语句用于参数校验和条件赋值:
public class Latency {
private double full;
private double cpuOne;
private double cpuTwo;
private double cpuThree;
private double cpuFour;
public Latency(final double full, final double cpuOne, final double cpuTwo, final double cpuThree, final double cpuFour) {
if (full > 10.0 || (full <= 0.0)) {
throw new IllegalArgumentException("Must check the values");
}
this.full = full;
if (cpuOne == 0 && cpuTwo == 0 && cpuThree == 0 && cpuFour == 0) {
throw new IllegalArgumentException("not all can be zero");
} else {
if (cpuOne == 0.5) {
this.cpuOne = full;
} else {
this.cpuOne = cpuOne;
}
if (cpuTwo == 0.5) {
this.cpuTwo = full;
} else {
this.cpuTwo = cpuTwo;
}
if (cpuThree == 0.5) {
this.cpuThree = full;
} else {
this.cpuThree = cpuThree;
}
if (cpuFour == 0.5) {
this.cpuFour = full;
} else {
this.cpuFour = cpuFour;
}
}
}
}这段代码的问题在于:
针对这些问题,我们可以采用多种重构策略来优化代码。
条件运算符(? :)提供了一种简洁的方式来表达简单的if-else赋值逻辑。它能将一行if-else语句压缩为一行表达式,显著减少代码行数,提高简洁性。
实现示例:
立即学习“Java免费学习笔记(深入)”;
public Latency(final double full, final double cpuOne, final double cpuTwo, final double cpuThree, final double cpuFour) {
if (full > 10.0 || (full <= 0.0)) {
throw new IllegalArgumentException("Must check the values");
}
this.full = full;
if (cpuOne == 0 && cpuTwo == 0 && cpuThree == 0 && cpuFour == 0) {
throw new IllegalArgumentException("not all can be zero");
} else {
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;
}
}优点:
注意事项:
当多处代码块执行相同的逻辑时,可以将其封装到一个独立的辅助方法中。这遵循了“Don't Repeat Yourself (DRY)”原则,提高了代码的复用性,减少了错误发生的可能性。
实现示例:
立即学习“Java免费学习笔记(深入)”;
首先,定义一个私有的静态辅助方法:
public class Latency {
// ... 现有成员变量 ...
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) {
if (full > 10.0 || (full <= 0.0)) {
throw new IllegalArgumentException("Must check the values");
}
this.full = full;
if (cpuOne == 0 && cpuTwo == 0 && cpuThree == 0 && cpuFour == 0) {
throw new IllegalArgumentException("not all can be zero");
} else {
this.cpuOne = changeHalfToFull(cpuOne, full);
this.cpuTwo = changeHalfToFull(cpuTwo, full);
this.cpuThree = changeHalfToFull(cpuThree, full);
this.cpuFour = changeHalfToFull(cpuFour, full);
}
}
}优点:
如果多个参数(如cpuOne到cpuFour)在概念上是同类项,且处理逻辑相似,那么将它们组织成一个数组是更优雅的解决方案。这不仅能简化代码,还能明确表达这些数据之间的关联性,提高代码的扩展性。
实现示例:
立即学习“Java免费学习笔记(深入)”;
首先,修改类的成员变量,用一个数组来存储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");
}
if (cpuValues == null || cpuValues.length != 4) { // 校验数组长度
throw new IllegalArgumentException("cpuValues must be an array of 4 elements");
}
// 校验所有cpu值不能同时为0
boolean allCpuZero = true;
for (double val : cpuValues) {
if (val != 0) {
allCpuZero = false;
break;
}
}
if (allCpuZero) {
throw new IllegalArgumentException("not all cpu values can be zero");
}
this.full = full;
this.cpuValues = new double[4]; // 初始化内部数组
for (int index = 0; index < cpuValues.length; index++) {
if (cpuValues[index] == 0.5) {
this.cpuValues[index] = full;
} else {
this.cpuValues[index] = cpuValues[index];
}
}
}
}优点:
最强大的重构往往是多种策略的组合。将条件运算符、辅助方法和数组结构结合起来,可以实现最简洁、最优雅的代码。
实现示例:
立即学习“Java免费学习笔记(深入)”;
public class Latency {
private double full;
private double[] cpuValues;
// 辅助方法,使用条件运算符简化
private static double changeHalfToFull(double value, double full) {
return value == 0.5 ? full : value;
}
public Latency(final double full, final double[] cpuValues) {
// 初始参数校验
if (full > 10.0 || (full <= 0.0)) {
throw new IllegalArgumentException("Full value must be between 0.0 (exclusive) and 10.0 (inclusive).");
}
if (cpuValues == null || cpuValues.length != 4) {
throw new IllegalArgumentException("cpuValues must be an array of 4 elements.");
}
// 校验所有cpu值不能同时为0
boolean allCpuZero = true;
for (double val : cpuValues) {
if (val != 0) {
allCpuZero = false;
break;
}
}
if (allCpuZero) {
throw new IllegalArgumentException("Not all cpu values can be zero.");
}
this.full = full;
this.cpuValues = new double[4]; // 初始化内部数组
// 结合数组和辅助方法进行赋值
for (int index = 0; index < cpuValues.length; index++) {
this.cpuValues[index] = changeHalfToFull(cpuValues[index], full);
}
}
}优点:
优化Java构造函数中多重if语句是提升代码质量的重要一环。通过灵活运用条件运算符、提取公共辅助方法以及重构为数组等策略,并结合实际项目需求进行组合,我们可以将冗余、复杂的代码转化为简洁、高效且易于维护的专业代码。选择合适的重构方法,不仅能提高开发效率,也能为项目的长期健康发展奠定基础。
以上就是Java构造函数中多重if语句的优化与重构实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号