优雅永不过时!
优化前:
public class NoEarlyReturnExample {
public boolean hasPositiveNumber(int[] numbers) {
boolean foundPositive = false;
for (int number : numbers) {
if (number > 0) {
foundPositive = true;
// 没有早返回,而是继续循环
}
}
return foundPositive; // 循环结束后返回结果
}
}
优化后:
public class EarlyReturnExample {
public boolean hasPositiveNumber(int[] numbers) {
for (int number : numbers) {
if (number > 0) {
return true; // 找到正数立即返回
}
}
return false; // 没有找到正数
}
}
减少了多次循环
优化前:
public class NoTernaryOperatorExample {
public String getGender(int number) {
if (number > 0) {
return "girl";
} else if (number < 0) {
return "boy";
} else {
return "other";
}
}
}
优化后:
public class TernaryOperatorExample {
public String getGender(int number) {
return (number > 0) ? "girl" : (number < 0) ? "boy" : "other";
}
}
优化前:
public class NoSwitchCaseExample {
public void performAction(String action) {
if ("start".equals(action)) {
System.out.println("Starting...");
} else if ("stop".equals(action)) {
System.out.println("Stopping...");
} else {
System.out.println("Unknown action");
}
}
}
优化后:
public class SwitchCaseExample {
public void performAction(String action) {
switch (action) {
case "start":
System.out.println("Starting...");
break;
case "stop":
System.out.println("Stopping...");
break;
default:
System.out.println("Unknown action");
}
}
}
优化前:
public class NoStrategyExample {
public void context() {
// 没有使用策略模式,而是直接执行代码
System.out.println("Direct execution");
// do something...
}
}
优化后:
public class StrategyExample {
interface Strategy {
void execute();
}
public class ConcreteStrategyA implements Strategy {
public void execute() {
System.out.println("Strategy A executed");
}
}
public void context(Strategy strategy) {
strategy.execute();
}
}
优化前:
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");
}
}
}
优化后:
public class LookupTableExample {
public void performAction(Map<String, Runnable> actions, String key) {
actions.getOrDefault(key, () -> System.out.println("No action found")).run();
}
}
优化前:
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");
}
}
}
优化后:
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");
}
}
这个是大家比较常用的,通过不同的功能拆分成不同的函数。
优化前:
public class NoCommandExample {
public void performAction(String action) {
// 直接执行动作,没有使用命令模式
if ("start".equals(action)) {
System.out.println("Starting...");
} else if ("stop".equals(action)) {
System.out.println("Stopping...");
}
}
}
优化后:
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();
}
}
优化前:
public class NoStateExample {
public void handleAction(String state) {
// 没有使用状态模式,直接在代码中处理逻辑
if ("start".equals(state)) {
System.out.println("Handling start");
} else if ("stop".equals(state)) {
System.out.println("Handling stop");
}
}
}
优化后:
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();
}
}
}
状态转换类似于我们在做一个简单的工单流转,每一步都是确定且可复用的场景。
优化前:
public class UnrefactoredConditionExample {
public boolean isWeekend(int day) {
// 没有重构的条件表达式,切套多、不好阅读
if (day == 6 || (day == 7 && !isHoliday(day))) {
return true;
}
return false;
}
private boolean isHoliday(int day) {
// 法定的假日检查逻辑(法定节假日每年都在变)
return false;
}
}
优化后:
public class RefactoredConditionExample {
public boolean isWeekend(int day) {
return day == 6 || day == 7;
}
}
简洁了很多
优化前:
public class NoAssertExample {
public void process(int value) {
if (value <= 0) {
throw new IllegalArgumentException("Value must be positive");
}
// 处理逻辑
System.out.println("Processing value: " + value);
}
}
优化后:
public class AssertExample {
public void process(int value) {
assert value > 0 : "Value must be positive";
// 处理逻辑
System.out.println("Processing value: " + value);
}
}
多数编程中,断言被用在自动化测试用例。不过预设条件判断用起来也非常丝滑。
优化前:
public class NoExceptionHandlingExample {
public int divide(int dividend, int divisor) {
if (divisor == 0) {
// 没有使用异常处理,而是直接返回错误代码
System.out.println("Cannot divide by zero");
return -1;
}
return dividend / divisor;
}
}
优化后:
public class ExceptionHandlingExample {
public int divide(int dividend, int divisor) {
try {
return dividend / divisor;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
return -1;
}
}
}
当遇到异常,尽可能在合适的地方捕获并处理,不要直接把所有异常都抛到最外层。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。