
在Java编程中,尤其是在构造器或方法中处理多个参数时,我们常常会遇到需要进行一系列条件判断的情况。如果这些判断逻辑相似或重复,很容易导致代码变得冗长、难以阅读和维护,即所谓的“多重if语句”问题。本教程将以一个具体的 Latency 类构造器为例,深入探讨如何有效地重构这类代码,提升其质量。
考虑以下 Latency 类的构造器:
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;
// CPU值校验及赋值逻辑
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;
}
}
}
// ... 其他方法
}这段代码存在以下几个主要问题:
接下来,我们将介绍几种优化策略。
立即学习“Java免费学习笔记(深入)”;
条件运算符(? :)提供了一种简洁的方式来表达简单的 if/else 逻辑。它可以替代那些只包含一个表达式赋值的 if/else 块。
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;
// CPU值校验及赋值逻辑
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;
}
}
// ...
}优点: 显著减少了代码行数,提高了简洁性。 缺点: 对于不熟悉条件运算符的开发者来说,初期可能需要适应。
当多处代码执行相同的逻辑时,将其封装成一个独立的辅助方法是提高代码复用性和可维护性的最佳实践。
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;
// CPU值校验及赋值逻辑
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);
}
}
// 辅助方法:将0.5替换为full值,否则保持原值
private static double changeHalfToFull(double value, double full) {
if (value == 0.5) {
return full;
} else {
return value;
}
}
// ...
}优点:
cpuOne 到 cpuFour 明显表示一组CPU相关的值。将它们存储在一个数组中,可以更好地体现数据的同质性,并允许我们使用循环来处理,进一步减少重复代码。
首先,修改类的成员变量以使用数组:
public class Latency {
private double full;
private double[] cpuValues; // 使用数组存储CPU值
// ...
}然后,修改构造器以接受数组参数,并利用循环进行处理:
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");
}
if (cpuValues == null || cpuValues.length != 4) { // 校验数组长度
throw new IllegalArgumentException("cpuValues must be an array of 4 elements.");
}
this.full = full;
// CPU值校验:检查是否所有CPU值都为0
boolean allZero = true;
for (double cpuValue : cpuValues) {
if (cpuValue != 0) {
allZero = false;
break;
}
}
if (allZero) {
throw new IllegalArgumentException("not all can be zero");
}
this.cpuValues = new double[4];
for (int i = 0; i < cpuValues.length; i++) {
if (cpuValues[i] == 0.5) {
this.cpuValues[i] = full;
} else {
this.cpuValues[i] = cpuValues[i];
}
}
}
// ...
}优点:
将上述优化策略结合起来,可以达到最佳效果。例如,我们可以将数组与辅助方法、条件运算符结合使用。
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");
}
if (cpuValues == null || cpuValues.length != 4) {
throw new IllegalArgumentException("cpuValues must be an array of 4 elements.");
}
this.full = full;
// CPU值校验:检查是否所有CPU值都为0
boolean allZero = true;
for (double cpuValue : cpuValues) {
if (cpuValue != 0) {
allZero = false;
break;
}
}
if (allZero) {
throw new IllegalArgumentException("not all can be zero");
}
this.cpuValues = new double[4];
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构造器中冗余的多重 if 语句。从使用简洁的条件运算符,到提取重复逻辑为辅助方法,再到利用数组重构数据结构,每种策略都旨在提高代码的清晰度、可维护性和扩展性。最终,通过组合这些技术,我们可以编写出更专业、更易于理解和管理的Java代码。记住,持续的代码重构是提升软件质量的关键环节。
以上就是优化Java中多重if语句:构造器重构策略的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号