优雅永不过时!
1. 使用早返回(Early Return):尽可能早地返回,避免嵌套的if-else。
优化前:
优化后:
减少了多次循环
2. 使用三元运算符:在条件简单的情况下,可以使用三元运算符来简化代码。
优化前:
优化后:
3. 使用switch-case语句:如果你的条件是基于不同的情况或值,更好的选择是switch-case。
优化前:
优化后:
4. 使用策略模式:将每个条件分支封装成一个策略对象,然后根据条件选择使用哪个策略。
优化前:
优化后:
5. 使用查找表:对于固定数量的条件分支,可以使用查找表(例如字典或哈希表)来映射条件和对应的行为。
优化前:
public class NoLookupTableExample {
public void performAction(String action) {
// 没有使用查找表,而是使用if-else
if ("start".equals(action)) {
System.out.println("Starting...");
} else if ("stop".equals(action)) {
System.out.println("Stopping...");
} else {
System.out.println("No action found");
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
优化后:
6. 使用函数或方法:将每个条件分支的逻辑封装到不同的函数或方法中,然后在if-else中调用这些函数。
优化前:
public class NoFunctionExample {
public void handleUserType(String userType) {
// 没有使用函数封装,而是直接在if-else中编写逻辑
if ("admin".equals(userType)) {
System.out.println("Admin logic here");
} else if ("user".equals(userType)) {
System.out.println("User logic here");
} else {
System.out.println("Guest logic here");
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
优化后:
public class FunctionExample {
public void handleUserType(String userType) {
if ("admin".equals(userType)) {
handleAdmin();
} else if ("user".equals(userType)) {
handleUser();
} else {
handleGuest();
}
}
private void handleAdmin() {
System.out.println("Handling admin");
}
private void handleUser() {
System.out.println("Handling user");
}
private void handleGuest() {
System.out.println("Handling guest");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
这个是大家比较常用的,通过不同的功能拆分成不同的函数。
7. 使用命令模式:将每个条件分支封装成一个命令对象,然后根据条件执行相应的命令。
优化前:
优化后:
public class CommandExample {
interface Command {
void execute();
}
public class StartCommand implements Command {
public void execute() {
System.out.println("Starting...");
}
}
public class StopCommand implements Command {
public void execute() {
System.out.println("Stopping...");
}
}
public void executeCommand(Command command) {
command.execute();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
8. 使用状态模式:如果逻辑分支与状态有关,可以使用状态模式来管理状态转换。
优化前:
优化后:
public class StateExample {
interface State {
void handle();
}
public class StartState implements State {
public void handle() {
System.out.println("Handling start state");
}
}
public class StopState implements State {
public void handle() {
System.out.println("Handling stop state");
}
}
public class Context {
private State state;
public void setState(State state) {
this.state = state;
}
public void request() {
state.handle();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
状态转换类似于我们在做一个简单的工单流转,每一步都是确定且可复用的场景。
9. 重构条件表达式:检查是否可以将复杂的条件表达式分解为更简单的部分。
优化前:
优化后:
简洁了很多
10. 使用断言:在某些情况下,使用断言来确保代码的预设条件被满足,避免复杂的条件判断。
优化前:
优化后:
多数编程中,断言被用在自动化测试用例。不过预设条件判断用起来也非常丝滑。
11. 使用异常处理:在某些情况下,使用异常处理来简化错误条件的处理。
优化前:
优化后:
当遇到异常,尽可能在合适的地方捕获并处理,不要直接把所有异常都抛到最外层。