优化代码 PlanOrderCheckResponseVO planOrderCheckResponseVO = JSONObject.parseObject(response, PlanOrderCheckResponseVO.class); if (null != planOrderCheckResponseVO){ if (planOrderCheckResponseVO.isFailed()){ designPlanOrder.setOrderPushResult("成功"); designPlanOrder.setOrderPushStatus(NumberConstant.CONSTANT_TWO); } else { designPlanOrder.setOrderPushResult(planOrderCheckResponseVO.getMessage()); designPlanOrder.setOrderPushStatus(NumberConstant.CONSTANT_THREE); } } else { designPlanOrder.setOrderPushResult("无返回信息"); designPlanOrder.setOrderPushStatus(NumberConstant.CONSTANT_THREE); }
时间: 2024-01-15 09:02:43 浏览: 46
可以对代码进行如下优化:
1. 可以使用`JSON.parseObject()`方法代替`JSONObject.parseObject()`方法,因为`JSON`是`fastjson`库的类,而`JSONObject`是`alibaba`库的类,`JSON`库的效率更高。
2. 可以使用三目运算符来简化代码,避免使用多个`if-else`语句。
3. 可以将重复的常量提取出来,定义为常量,避免多次创建。
改进后的代码如下:
```
PlanOrderCheckResponseVO planOrderCheckResponseVO = JSON.parseObject(response, PlanOrderCheckResponseVO.class);
String orderPushResult;
int orderPushStatus;
if (planOrderCheckResponseVO != null) {
orderPushResult = planOrderCheckResponseVO.isFailed() ? "成功" : planOrderCheckResponseVO.getMessage();
orderPushStatus = planOrderCheckResponseVO.isFailed() ? NumberConstant.CONSTANT_TWO : NumberConstant.CONSTANT_THREE;
} else {
orderPushResult = "无返回信息";
orderPushStatus = NumberConstant.CONSTANT_THREE;
}
designPlanOrder.setOrderPushResult(orderPushResult);
designPlanOrder.setOrderPushStatus(orderPushStatus);
```
阅读全文