多个if else替换为一个if判断

来源:互联网 发布:mac 鼠标 触摸板 编辑:程序博客网 时间:2024/06/09 21:16

项目中总是会出现类似下面的代码写法,比如:

String result = "";if(A){  if(B){  result = "hello_world";  }else{  result = "hello";  }}else{  result = "hello";}

重构后的代码:

String result = "hello";if(A && B){result = "hello_world";}

以上的代码不是我凭空想出来的,而是我在实际上线代码中看到过多次。
这里写图片描述

看了代码重构的书,记得一句话,小步重构,全面测试。

0 0
原创粉丝点击