1.使用 return
我们使用 return 去掉多余的 else,条件不符合直接结束方法,实现代码如下。
优化前代码:
if ("java".equals(str)) {
// 业务代码......
} else {
return;
优化后代码:
if (!"java".equals(str)) {
return;
}
public enum CountryEnum {
ONE(1,"齐"),TWO(2,"楚"),THREE(3,"燕"),FOUR(4,"赵"),FIVE(5,"魏"),SIX(6,"韩");
private Integer retCode;
private String retMessage;
//省略 有参 空参 get set
public static CountryEnum forEach_CountryEnum(int index){
CountryEnum[] values = CountryEnum.values();
for (CountryEnum value : values) {
if (index==value.getRetCode()){
return value;
}
}
return null;
}
----------------------------------------------------------------
调用方法:CountryEnum.forEach_CountryEnum(i).getRetMessage()
2.使用 Map
使用 Map 数组,把相关的判断信息,定义为元素信息可以直接避免 if else 判断,实现代码如下。
优化前代码:
if (t == 1) {
type = "name";
} else if (t == 2) {
type = "id";
} else if (t == 3) {
type = "mobile";
}
我们先定义一个 Map 数组,把相关判断信息存储起来
Map<Integer, String> typeMap = new HashMap<>();
typeMap.put(1, "name");
typeMap.put(2, "id");
typeMap.put(3, "mobile");
之前的判断语句可以使用以下一行代码代替了:
type = typeMap.get(t);
3.使用三元运算符
三元运算符也叫三元表达式或者三目运算符/表达式,不过代表的都是一个意思,优化代码如下。
优化前代码
Integer score = 81;
if (score > 80) {
score = 100;
} else {
score = 60;
}
优化后代码:
score = score > 80 ? 100 : 60;
4.合并条件表达式
在项目中有些逻辑判断是可以通过梳理和归纳,变更为更简单易懂的逻辑判断代码,如下所示。
优化前代码:
String city = "广州";
String area = "020";
String province = "广东";
if ("广州".equals(city)) {
return "guang'zhou";
}
if ("020".equals(area)) {
return "guang'zhou";
}
if ("广东".equals(province)){
return "guang'zhou";
}
优化后代码:
if ("广州".equals(city) || "020".equals(area) || "广东".equals(province)){
return "guang'zhou";
}
5.使用枚举
JDK 1.5 中引入了新的类型——枚举(enum),我们使用它可以完成很多功能,例如下面这个。
优化前代码:
Integer typeId = 0;
String type = "Name";
if ("Name".equals(type)) {
typeId = 1;
} else if ("Age".equals(type)) {
typeId = 2;
} else if ("Address".equals(type)) {
typeId = 3;
}
优化时,我们先来定义一个枚举:
public enum TypeEnum {
Name(1), Age(2), Address(3);
public Integer typeId;
TypeEnum(Integer typeId) {
this.typeId = typeId;
}
}
之前的 if else 判断就可以被如下一行代码所替代了:
typeId = TypeEnum.valueOf(“Name”).typeId;
6.使用 Optional
从 JDK 1.8 开始引入 Optional 类,在 JDK 9 时对 Optional 类进行了改进,增加了 ifPresentOrElse() 方法,我们可以借助它,来消除 if else 的判断,使用如下。
优化前代码:
String str = "java";
if (str == null) {
System.out.println("Null");
} else {
System.out.println(str);
}
优化后代码:
Optional<String> opt = Optional.of("java");
opt.ifPresentOrElse(v ->
System.out.println(v), () -> System.out.println("Null"));
小贴士:注意运行版本,必须是 JDK 9+ 才行。
7.梳理优化判断逻辑
和第 4 点比较类似,我们可以通过分析 if else 的逻辑判断语义,写出更加易懂的代码,例如以下这个嵌套判断的优化。
优化前代码:
// 年龄大于 18
if (age > 18) {
// 工资大于 5000
if (salary > 5000) {
// 是否漂亮
if (pretty == true) {
return true;
}
}
}
return false;
优化后代码:
if (age < 18) {
return false;
}
if (salary < 5000) {
return false;
}
return pretty;
我们需要尽量把表达式中的包含关系改为平行关系,这样代码可读性更高,逻辑更清晰。
8.使用多态
继承、封装和多态是 OOP(面向对象编程)的重要思想,本文我们使用多态的思想,提供一种去除 if else 方法。
优化前代码:
Integer typeId = 0;
String type = "Name";
if ("Name".equals(type)) {
typeId = 1;
} else if ("Age".equals(type)) {
typeId = 2;
} else if ("Address".equals(type)) {
typeId = 3;
}
使用多态,我们先定义一个接口,在接口中声明一个公共返回 typeId 的方法,在添加三个子类分别实现这三个子类,实现代码如下:
public interface IType {
public Integer getType();
}
public class Name implements IType {
@Override
public Integer getType() {
return 1;
}
}
public class Age implements IType {
@Override
public Integer getType() {
return 2;
}
}
public class Address implements IType {
@Override
public Integer getType() {
return 3;
}
}
注意:为了简便我们这里把类和接口放到了一个代码块中,在实际开发中应该分别创建一个接口和三个类分别存储。
此时,我们之前的 if else 判断就可以改为如下代码:
IType itype = (IType) Class.forName(“com.example.” + type).newInstance();
Integer typeId = itype.getType();
有人可能会说,这样反而让代码更加复杂了,此可谓“杀鸡焉用宰牛刀”的典型范例了。这里作者只是提供一种实现思路和提供了一些简易版的代码,以供开发者在实际开发中,多一种思路和选择,具体用不用需要根据实际情况来定了。灵活变通,举一反三,才是开发的上乘心法。
策略模式
Step1: 定义一个抽象的父类 AbstractFlowInfoExecutor ,抽取公共属性和方法
public abstract class AbstractFlowInfoExecutor {
// 公共属性,提取到抽象类中,子类直接使用即可
FlowInfoDao flowDao = new FlowInfoDao();
// 子类可重写该方法
public List<FlowInfo> process(FlowInfo flowInfo) {
return null;
}
}
Step2: 子类重写父类的方法,实现自定义业务逻辑
完成了抽象父类的定义后,就可以把代码块中不同条件下的方法抽到各个不同的具体类里面去了,如下
flowInfo.getCurrentState() == 0 分支
public class DraftExecutor extends AbstractFlowInfoExecutor {
@Override
public List<FlowInfo> process(FlowInfo flowInfo) {
return flowDao.getWorkDraftList(flowInfo);
}
}
flowInfo.getCurrentState() == 1 分支
public class TodoExecutor extends AbstractFlowInfoExecutor {
@Override
public List<FlowInfo> process(FlowInfo flowInfo) {
return flowDao.getWorkTodoList(flowInfo);
}
}
flowInfo.getCurrentState() == 2 分支
public class FinishExecutor extends AbstractFlowInfoExecutor {
@Override
public List<FlowInfo> process(FlowInfo flowInfo) {
return flowDao.getWorkFinishList(flowInfo);
}
}
flowInfo.getCurrentState() == 3 分支
public class RejectExecutor extends AbstractFlowInfoExecutor {
@Override
public List<FlowInfo> process(FlowInfo flowInfo) {
return flowDao.getWorkRejectList(flowInfo);
}
}
Step3: 改造Service层
public class FlowInfoService {
public List<FlowInfo> getWorkFlowList(final FlowInfo flowInfo) {
AbstractFlowInfoExecutor executor = null;
// 根据状态实例化不同的executor
// 0:草稿
if (0 == flowInfo.getCurrentState()) {
executor = new DraftExecutor();
}
// 1:待办
if (1 == flowInfo.getCurrentState()) {
executor = new TodoExecutor();
}
// 2:完成
if (2 == flowInfo.getCurrentState()) {
executor = new FinishExecutor();
}
// 3:驳回
if (3 == flowInfo.getCurrentState()) {
executor = new RejectExecutor();
}
// 执行
return executor.process(flowInfo);
}
}
当然了,工厂中状态和Executor的映射关系可以使用Map来维护
public class FlowInfoExecutorFactory {
//使用map存储
private static Map<Ingteger,AbstractFlowInfoExecutor> executorMap=new HashMap<>();
static{
executorMap.put(0,new DraftExecutor());
executorMap.put(0,new TodoExecutor());
executorMap.put(0,new FinishExecutor());
executorMap.put(0,new RejectExecutor());
executorMap=Collections.unmodifiableMap(executorMap){
}
public static AbstractFlowInfoExecutor executor(FlowInfo flowInfo)
return executorMap.get(flowInfo.getCurrentState());
}
Enum也是可以的
public enum ExecutorEnum{
Draft(0),Todo(1),Finish(2),Reject(3);
private int state;
private ExecutorEnum(int state){
this.state=state;
}
private int getState(){
return state;
}
}
9.选择性的使用 switch
很多人都搞不懂 switch 和 if else 的使用场景,但在两者都能使用的情况下,可以尽量使用 switch,因为 switch 在常量分支选择时,switch 性能会比 if else 高。
if else 判断代码:
if ("add".equals(cmd)) {
result = n1 + n2;
} else if ("subtract".equals(cmd)) {
result = n1 - n2;
} else if ("multiply".equals(cmd)) {
result = n1 * n2;
} else if ("divide".equals(cmd)) {
result = n1 / n2;
} else if ("modulo".equals(cmd)) {
result = n1 % n2;
}
switch 代码:
switch (cmd) {
case "add":
result = n1 + n2;
break;
case "subtract":
result = n1 - n2;
break;
case "multiply":
result = n1 * n2;
break;
case "divide":
result = n1 / n2;
break;
case "modulo":
result = n1 % n2;
break;
}
在 Java 14 可使用 switch 代码块,实现代码如下:
// java 14
switch (cmd) {
case "add" -> {
result = n1 + n2;
}
case "subtract" -> {
result = n1 - n2;
}
case "multiply" -> {
result = n1 * n2;
}
case "divide" -> {
result = n1 / n2;
}
case "modulo" -> {
result = n1 % n2;
}
}