重构之 if else
写if-else不外乎两种场景:异常逻辑处理和不同状态处理。写着写着就嵌套过深,虽然当时能看懂,过几天再看的话就要吐血了。大量的if else 会降低代码的可读性,维护性也特别的差。今天看了别人写的一些文章再结合自己的理解整理出此文。
异常逻辑处理
1.将相同的返回值合并
//重构之前
if( BeijingTemperature > 30 ){
return "It's really hot."
}
if( ShanghaiTemperature > 32 ){
return "It's really hot."
}
if( GuangzhouTemperature > 35 ){
return "It's really hot."
}
//不知道为啥7月份这几天热死了,赶快下点雨吧。
//重构之后
if( BeijingTemperature > 30 || ShanghaiTemperature > 32 || GuangzhouTemperature > 35 ){
return "It's really hot."
}
这个呢比较简单相同的返回值用 || 来精简他们
2.取消临时变量
//重构之前 (缩写:TEMP 全称:Temperature (温度))
public static double getTempByCity(String city){
double temperature = 0.0;
if( "Beijing".equals(city) ){
temperature = getBeijingTemp();
} else if( "Shanghai".equals(city) ){
temperature = getShanghaiTemp();
} else {
if( "Guangzhou".equals(city) ){
temperature = getGuangzhouTemp();
}
}
return temperature ;
}
//重构之后
public static double getTempByCity(String city){
if( "Beijing".equals(city) ){
return getBeijingTemp();
}
if( "Shanghai".equals(city) ){
return getShanghaiTemp();
}
if( "Guangzhou".equals(city) ){
return getGuangzhouTemp();
}
}
返回值如果复杂的话最好封装成方法来精简他们。
3.尽可能地把主要逻辑代码写在最外层
//重构之前
//摄氏度转华氏温度( (9/5)* ℃ + 32 )
//华氏温度转摄氏度( (5/9)*(°F-32))
//单位转换(对30摄氏度 转换成 华氏温度)
public static double temperatureConversion(String city,double temperature){
if( !"".equals(city) ){
if( temperature == 30 ){
return (9/5)* temperature +32;
}else{
return 0.0;
}else {
return 0.0;
}
}
//重构之后
public static double temperatureConversion(String city,double temperature){
if( "".equals(city) || temperature != 30 ){
return 0.0;
}
return (9/5)* temperature +32;
最好把你想要的返回值写在最外面,而不是一大堆逻辑处理。
3.有异常就先退出 保持主要代码整洁
//重构之前
//查找大于30°的城市
public List<String> getCityByTemp(String cityId){
List<String> hotCity = new ArrayList<String>();
String city = getCityById(cityId);
if( city != null ){
double temp = getTempByCity(city);
if(temp >30){
hotCity.add(city);
}else{
throw new RuntimeException("温度不够30摄氏度");
}
}else{
throw new RuntimeException("城市为空");
}
return hotCity;
}
//重构之后
public List<String> getCityByTemp(String cityId){
List<String> hotCity = new ArrayList<String>();
String city = getCityById(cityId);
if( city == null ){
throw new RuntimeException("城市为空");
}
double temp = getTempByCity(city);
if(temp < 30 ){
throw new RuntimeException("温度不够30摄氏度");
}
hotCity.add(city);
return hotCity;
}
像这样根据条件查找,然后在根据结果再去进行其他的操作,就把错误先返回回去,主要的代码也是放到最外面。
不同状态处理
1.合理的使用Map
//重构之前
Device device = new Device();
if(device.getType() == 1){
return "飞机";
}else if(device.getType() == 2){
return "无人机";
}else if(device.getType() == 3){
return "汽车";
}
//重构之后
Device device = new Device();
getNameByType(device.getType());
public static String getNameByType(String type){
Map<String,Object> data = new HashMap<String,Object>();
data.push(1,"飞机");
data.push(2,"无人机");
data.push(3,"汽车");
return data,get(type).toString();
}
使用这个的话是事先知道值的,如果是动态的话就不能用这个了。
2.使用多态
//重构之前
public double getArea(String type){
if("square".equals(type)){
return 2*3;
}else if("triangle".equals(type)){
return 2*3/2;
}
return 0;
}
//重构之后
public abstract class Area {
abstract double getArea();
}
public class Square extends Area{
@Override
double getArea() {
return 2*3;
}
}
public class Triangle extends Area{
@Override
double getArea() {
return 2*3/2;
}
}
------------------------------------------------
public double getArea(String type){
if("triangle".equals(type)){
return (new triangle().getArea();
}else if("square".equals(type)){
return new square().getArea();
}
return 0;
}
3.使用枚举
public enum EnumTest {
ONE("one") {
@Override
public void apply() {
System.out.println("one");
}
},
TWO("two") {
@Override
public void apply() {
System.out.println("two");
}
}, THREE("three") {
@Override
public void apply() {
System.out.println("three");
}
}, FOUR("four") {
@Override
public void apply() {
System.out.println("four");
}
};
public abstract void apply();
private String type;
EnumTest(String type){
this.type = type;
}
public String getType() {
return type;
}
}
-----------------------------------------------
public String chineseNumbers(String cnum) {
EnumTest.valueOf(cnum.toUpperCase()).apply();//这里得大写
}
总而言之,言而总之。异常逻辑处理就是把能合并的合并,把主干代码写到最外面。不同状态处理就是利用枚举,map,多态等方法进行处理。就总结了这么些,有不对的地方大家可以提出来,也欢迎大家补充。