洞察探索如何利用兼容微信生态的小程序容器,实现跨平台开发,助力金融和车联网行业的数字化转型。
694
2022-11-19
写给所有程序员_你的逻辑不要太贪心
有的时候,可能一段逻辑代码需要大量的运算,为了偷懒,我们把本不该写在一起的东西勉强拼凑在一起。
1.贪心的函数(贪心的方法)
例子:
public static void main(String[]args){ int firstNum = 5; int secondNum = 3; System.out.println(getSumOfTwoNums(firstNum,secondNum)); //doSomething... System.out.println(getSumOfTwoNums(5,2));}public static int getSumOfTwoNums(int firstNum,int secondNum){ ++firstNum; return
发现问题了吗?求和函数(方法)里面竟然有一句++firstNum;!!!第一次用的时候,可能是因为某种需要,所以需要把firstNum加一。为了方便,把它和后面的相加写在一起了。这样后面用的时候可能导致错误,因为后面用到的时候你已经忘记了这个函数(方法)里面你居然一时脑抽把加一这样的代码写在里面了。 贪心:你的函数和方法把没提到的事顺便做了。 修改:不要做你的函数(方法)没有提到的事,这可能导致你后面的调用造成隐患。
正确的做法:
public static void main(String[] args){ int firstNum = 5; int secondNum = 3; System.out.println(getSumOfTwoNums(firstNum+1,secondNum)); //doSomething... System.out.println(getSumOfTwoNums(5,2));}public static int getSumOfTwoNums(int firstNum,int secondNum){ return
2.贪心的选择结构
public static void main(String[] args){ int CODE_A = 1; int CODE_B = 2; //doSomething int firstNum = 5; int secondNum = 3; int thirdNum 8; int fourthNum = 2; int sum = 0; int difference = 0; if(CODE_A > CODE_B){ sum = thirdNum + fourthNum; difference = fourthNum - thirdNum; }else if(CODE_A == CODE_B){ difference = firstNum - secondNum; }else{ sum
上面的例子要讲的是达到某些条件要做一些操作,暂时以CODE_A,CODE_B为例子,表示某状态暂时以这种方式代替。sum表示求的和,difference表示求的差。 贪心:把这两个逻辑写在同一段代码里面了,虽然两者并没有关系。 修改:把不同部分的代码逻辑分开。 正确的做法:
public static void main(String[] args){ int CODE_A = 1; int CODE_B = 2; //doSomething int firstNum = 5; int secondNum = 3; int thirdNum 8; int fourthNum = 2; int sum = 0; if(CODE_A > CODE_B){ sum = thirdNum + fourthNum; }else if(CODE_A < CODE_B){ sum = thirdNum + secondNum; } int difference = 0; if(CODE_A > CODE_B){ difference = fourthNum - thirdNum; }else if(CODE_A == CODE_B){ difference = firstNum - secondNum; }}
可以看到,虽然代码判断了两次,但是逻辑更清晰了,因为很多时候,贪心并不满足全匹配,这个改一下,那个又改一下,可能你第一次写的时候很爽:只要写一次。但后面看的时候,保证你会疯掉。而且,贪心写法,不方便分割,像上面的代码,可以改成这样:
选择结构贪心识别:一个选择结构if…else if(…)…else不能使用一个函数(方法)表达出来
public class TestGreedy{ private int CODE_A = 1; private int CODE_B = 2; int firstNum = 5; int secondNum = 3; int thirdNum 8; int fourthNum = 2; public static void main(String[] args){ TestGreedy greedy = new TestGreedy(); int sum = getSumByCode(); int diference = getDifferenceByCode(); } public int getSumByCode(){ if(CODE_A > CODE_B){ return thirdNum + fourthNum; }else if(CODE_A < CODE_B){ return thirdNum + secondNum; } return 0; } public int getDifferenceByCode(){ if(CODE_A > CODE_B){ return fourthNum - thirdNum; }else if(CODE_A == CODE_B){ return firstNum - secondNum; } return 0; }}
因为不贪心,所以if…else存在完整的逻辑含义,所以我们可以把它们单独放到函数(方法)里面,这样看起来,马上清爽了,而这种做法的前提是:不要太贪心,又求和又求差,很难以一个函数(方法)表达它的意思。
public void onClick(View v){ swich(v.getId()){ case ...:break; case ...:break; }}
3.贪心的循环结构
public static void main(String[] args){ TestGreedy greedy = new TestGreedy(); int arrNums = new int[]{2,4,5,6}; int sum = 0; int muti = 1; for(int i = 0; i < arrNums.size(); i++){ sum += arrNums[i]; muti *= arrNums[i]; } }
贪心:一个循环里面做了多件事 首先,不能不承认,你很“聪明”,从逻辑的角度讲你把O(2n)的时间复杂度降低到O(n)。但实际上,它的指数级别还是一样的,都是n级别!!!。除非数据量非常大(数组级别在千万以上),否则这样做除了搞乱逻辑只外,实在没有什么太重大的意义。
修改后的代码:
public static void main(String[] args){ int[] nums = new int[]{2,4,5,6}; int sum = 0; for(int i = 0; i < nums .size(); i++){ sum += nums [i]; } int muti = 1; for(int i = 0; i < nums .size(); i++){ muti *= nums [i]; }}
同样,我们可以单独把它拆分到单独的函数或者方法当中:
public static void main(String[] args){ int[] nums = new int[]{2,4,5,6}; int sum = getSumOfNums(nums); int muti = getMutiOfNums(nums);}public static int getSumOfNums(int[] nums){ int sum = 0; for(int i = 0; i < nums .size(); i++){ sum += nums [i]; }}public static int getMutiOfNums(int[] nums){ int muti = 1; for(int i = 0; i < nums .size(); i++){ muti *= nums [i]; }}
循环结构贪心识别:一个选择结构for(…){}不能使用一个函数(方法)表达出来
4.贪心的变量
public static void main(String[] args){ int sum = 0; for(int i = 0; i < arrNums.size(); i++){ sum += arrNums[i]; } sum -= 5; System.out.println(sum); }
你的和有表达它会减少的含义么?你确定这样使用不会出错?在我看来,这种一个变量两种用途的方式,除了给你后面的代码造成困惑和麻烦外,没什么太有用的地方,正确的写法是:
public static void main(String[] args){ int sum = 0; for(int i = 0; i < arrNums.size(); i++){ sum += arrNums[i]; } int offsetSum -= 5; System.out.println(offsetSum); }
贪心变量的特点是,你对变量做了某件事情,可完全无法用原有的变量名称看出它有表达有做这件事情的意向
补充:一直在想一个例子,怎么讲不贪心的好处更加具体,然后重新改了一下公司一个计算价格的逻辑,当然,由于保密不能贴出来,所以打算用一个叫外卖的例子,因为这个可以足够复杂。
假设收外卖费是这样收的(虚拟的,非真实):外卖费由食物费,餐具费,送货费,红包,优惠(满100-22)组成,然后计算。
public double getTakeOutFoodCost(){ double foodCost = 0; double tableWareCost = 0; double redPacketCost = 0; double realCost = 0;//实际价格(不减) double fillSubMoney = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } takeOutBean.setFoodCost(foodCost); tableWareCost = takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost(); takeOutBean.setTableWareCost(tableWareCost); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); redPacketCost = takeOutBean.getRedPacketCost(); foodCost = (foodCost -tableWareCost.getRedPacketCost()>0)?foodCost -tableWareCost.getRedPacketCost():0; if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost()+tableWareCost.getRedPacketCost(); }else{ takeOutBean.setCostByRedPacket(0); if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost(); } takeOutBean.setRealCost(realCost); fillSubMoney = realCost/100 * 22; takeOutBean.setFillSubMoney(fillSubMoney); return foodCost + tableWareCost - redPacketCost - fillSubMoney;
首先,我们分析到这个函数(方法)本质是让我们获取各种价钱,然后返回的。但是这个方法里面放了太多的东西(实际还好,我看的那个有五层if…): 1.这个方法名说明了是用于获取外卖价钱的,但此方法对于takeOutBean(外卖对象)以各种姿势进行设置,你确定以后还能找的到?
2.foodCost,这应该指的是食物费吧?为何要减红包费,为何要加送货费?注意到你的realCost实际上把快递费算了两次吗?注意到最后又减了一次红包吗?这就是变量贪心的后果。
3.以下代码再if,else都有写到,这是贪心太多,自己都看不清代码的时候,很容易犯的错误,实际上,当你的代码够少,逻辑不贪心的时候完全不会犯这样的错误:
if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost();
好了,现在对这个复杂的函数进行改造。
第一步,分离takeOutBean的设置和价格计算部分
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public void setTakeOutBeanData(){}
大致结构是这样的,然后我们先把getTakeOutFoodCost()的代码复制一份到setTakeOutBeanData()里面变成:
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public void setTakeOutBeanData(){ double foodCost = 0; double tableWareCost = 0; double redPacketCost = 0; double realCost = 0;//实际价格(不减) double fillSubMoney = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } takeOutBean.setFoodCost(foodCost); tableWareCost = takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost(); takeOutBean.setTableWareCost(tableWareCost); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); redPacketCost = takeOutBean.getRedPacketCost(); foodCost = (foodCost -tableWareCost.getRedPacketCost()>0)?foodCost -tableWareCost.getRedPacketCost():0; if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost()+tableWareCost; }else{ takeOutBean.setCostByRedPacket(0); if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost(); } takeOutBean.setRealCost(realCost); fillSubMoney = realCost/100 * 22; takeOutBean.setFillSubMoney(fillSubMoney); return foodCost + tableWareCost - redPacketCost - fillSubMoney;}public double getTakeOutFoodCost(){ double foodCost = 0; double tableWareCost = 0; double redPacketCost = 0; double realCost = 0;//实际价格(不减) double fillSubMoney = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } takeOutBean.setFoodCost(foodCost); tableWareCost = takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost(); takeOutBean.setTableWareCost(tableWareCost); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); redPacketCost = takeOutBean.getRedPacketCost(); foodCost = (foodCost -tableWareCost.getRedPacketCost()>0)?foodCost -tableWareCost.getRedPacketCost():0; if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost()+tableWareCost; }else{ takeOutBean.setCostByRedPacket(0); if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost(); } takeOutBean.setRealCost(realCost); fillSubMoney = realCost/100 * 22; takeOutBean.setFillSubMoney(fillSubMoney); return
代码暂时性的变多了,看的好乱,好想吐。。。为了整洁,为了优雅,为了不贪心,我们姑且忍受,现在,我们把所有getTakeOutFoodCost()中设置takeOutBean的部分全部删掉,把setTakeOutBeanData()的所有非takeOutBean设置部分全部删掉:
第二步,删除getTakeOutFoodCost()设置takeOutBean的部分,删除setTakeOutBeanData()的所有非takeOutBean设置部分
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public void setTakeOutBeanData(){ takeOutBean.setFoodCost(foodCost); takeOutBean.setTableWareCost(tableWareCost); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); }else{ takeOutBean.setCostByRedPacket(0); } takeOutBean.setRealCost(realCost); takeOutBean.setFillSubMoney(fillSubMoney);}public double getTakeOutFoodCost(){ double foodCost = 0; double tableWareCost = 0; double redPacketCost = 0; double realCost = 0;//实际价格(不减) double fillSubMoney = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } tableWareCost = takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost(); if(takeOutBean.getRedPacketCost()!=0){ foodCost = (foodCost -tableWareCost.getRedPacketCost()>0)?foodCost -tableWareCost.getRedPacketCost():0; if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost()+tableWareCost; }else{ if(takeOutBean.needDeliveryCost()) foodCost += foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+foodBean.getDeliveryCost(); } fillSubMoney = realCost/100 * 22; return
好吧,依然乱的要命,不过setTakeOutBeanData()里面已经初见成效了,这里的错误现在先不管它。现在把getTakeOutFoodCost()的foodCost权责分开,把realCost的计算错误纠正:
第三步,分离getTakeOutFoodCost()的权责
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public void setTakeOutBeanData(){ takeOutBean.setFoodCost(foodCost); takeOutBean.setTableWareCost(tableWareCost); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); }else{ takeOutBean.setCostByRedPacket(0); } takeOutBean.setRealCost(realCost); takeOutBean.setFillSubMoney(fillSubMoney);}public double getTakeOutFoodCost(){ double foodCost = 0; double tableWareCost = 0; double redPacketCost = 0; double deliveryCost = 0; double realCost = 0;//实际价格(不减) double fillSubMoney = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } tableWareCost = takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost(); if(takeOutBean.getRedPacketCost()!=0){ redPacketCost = takeOutBean.getRedPacketCost(); } if(takeOutBean.needDeliveryCost()) deliveryCost = foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+deliveryCost +tableWareCost; fillSubMoney = realCost/100 * 22; return
然后稍微整理一下,代码整齐了好多,注意返回的数据要加上送货费deliveryCost,实际上,红包价格是否为零对获取红包价格完全没影响不是吗?那么if(takeOutBean.getRedPacketCost()!=0){}的判断可以去掉了:
第四步,去除无效的判断:
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public void setTakeOutBeanData(){ takeOutBean.setFoodCost(foodCost); takeOutBean.setTableWareCost(tableWareCost); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); }else{ takeOutBean.setCostByRedPacket(0); } takeOutBean.setRealCost(realCost); takeOutBean.setFillSubMoney(fillSubMoney);}public double getTakeOutFoodCost(){ double foodCost = 0; double tableWareCost = 0; double redPacketCost = 0; double deliveryCost = 0; double realCost = 0;//实际价格(不减) double fillSubMoney = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } tableWareCost = takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost(); redPacketCost = takeOutBean.getRedPacketCost(); if(takeOutBean.needDeliveryCost()) deliveryCost = foodBean.getDeliveryCost(); realCost = foodCost + tableWareCost+deliveryCost +tableWareCost; fillSubMoney = realCost/100 * 22; return
然后,我们可以发现,每种价格都已经完美的分割开了,可以独立成函数了:
第五步,独立各种价格成函数
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public double getTakeOutFoodCost(){ return getFoodCost()+ getTableWareCost()+ getDeliveryCost()- takeOutBean.getRedPacketCost()- getFillSubMoney();}public void setTakeOutBeanData(){ takeOutBean.setFoodCost(foodCost); takeOutBean.setTableWareCost(tableWareCost); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); }else{ takeOutBean.setCostByRedPacket(0); } takeOutBean.setRealCost(realCost); takeOutBean.setFillSubMoney(fillSubMoney);}private double getFillSubMoney(){ return getRealCost()/100 * 22;}public double getRealCost(){ return getFoodCost() + getTableWareCost() + takeOutBean.getRedPacketCost() + getDeliveryCost();}public double getFoodCost(){ double foodCost = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } return foodCost;}public double getTableWareCost(){ return takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost();}public double getDeliveryCost(){ if(takeOutBean.needDeliveryCost()) return foodBean.getDeliveryCost(); return 0;}
棒极了,非常整洁!所有的地方最多只有一层!此时可以发现,上面的setTakeOutBeanData()竟然很容易就可以改好了:
第五步,修改setTakeOutBeanData()
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public void setTakeOutBeanData(){ takeOutBean.setFoodCost(getFoodCost()); takeOutBean.setTableWareCost(getTableWareCost()); if(takeOutBean.getRedPacketCost()!=0){ takeOutBean.setCostByRedPacket(1); }else{ takeOutBean.setCostByRedPacket(0); } takeOutBean.setRealCost(getRealCost()); takeOutBean.setFillSubMoney(getFillSubMoney());}public double getTakeOutFoodCost(){ return getFoodCost()+ getTableWareCost()+ getDeliveryCost()- takeOutBean.getRedPacketCost()- getFillSubMoney();}public double getRealCost(){ return getFoodCost() + getTableWareCost() + takeOutBean.getRedPacketCost() + getDeliveryCost();}public double getFoodCost(){ double foodCost = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } return foodCost;}public double getTableWareCost(){ return takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost();}public double getDeliveryCost(){ if(takeOutBean.needDeliveryCost()) return foodBean.getDeliveryCost(); return 0;}public double getFillSubMoney(){ return getRealCost()/100 * 22;}
setTakeOutBeanData()里面有个if…else,把这个放出来效果更好:
第六步,抽离设置里面的if…else
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public void setTakeOutBeanData(){ takeOutBean.setFoodCost(getFoodCost()); takeOutBean.setTableWareCost(getTableWareCost()); takeOutBean.setCostByRedPacket(getTypeByRedPacketCost()); takeOutBean.setRealCost(getRealCost()); takeOutBean.setFillSubMoney(getFillSubMoney());}public int getTypeByRedPacketCost(){ return takeOutBean.getRedPacketCost()==0?0:1;}public double getTakeOutFoodCost(){ return getFoodCost()+ getTableWareCost()+ getDeliveryCost()- takeOutBean.getRedPacketCost()- getFillSubMoney();}public double getRealCost(){ return getFoodCost() + getTableWareCost() + takeOutBean.getRedPacketCost() + getDeliveryCost();}public double getFoodCost(){ double foodCost = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } return foodCost;}public double getTableWareCost(){ return takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost();}public double getDeliveryCost(){ if(takeOutBean.needDeliveryCost()) return foodBean.getDeliveryCost(); return 0;}public double getFillSubMoney(){ return getRealCost()/100 * 22;}
第七步,为了阅读方便,我们调换一下各个函数的顺序:
public void payMoney(){ int takeOutFoodCost = getTakeOutFoodCost(); setTakeOutBeanData(); //doSomething}public double getTakeOutFoodCost(){ return getFoodCost()+ getTableWareCost()+ getDeliveryCost()- takeOutBean.getRedPacketCost()- getFillSubMoney();}public void setTakeOutBeanData(){ takeOutBean.setFoodCost(getFoodCost()); takeOutBean.setTableWareCost(getTableWareCost()); takeOutBean.setCostByRedPacket(getTypeByRedPacketCost()); takeOutBean.setRealCost(getRealCost()); takeOutBean.setFillSubMoney(getFillSubMoney());}public double getFoodCost(){ double foodCost = 0; for(FoodBean foodBean:foodBeans){ foodCost += foodBean.getCost(); } return foodCost;}public double getTableWareCost(){ return takeOutBean.getNumOfPeople() * takeOutBean.getSingleTableWareCost();}public double getDeliveryCost(){ if(takeOutBean.needDeliveryCost()) return foodBean.getDeliveryCost(); return 0;}public double getFillSubMoney(){ return getRealCost()/100 * 22;}public double getRealCost(){ return getFoodCost() + getTableWareCost() + takeOutBean.getRedPacketCost() + getDeliveryCost();}public int getTypeByRedPacketCost(){ return takeOutBean.getRedPacketCost()==0?0:1;}
现在这段代码终于“合格”了,看计算收费的代码:
public double getTakeOutFoodCost(){ return getFoodCost()+ getTableWareCost()+ getDeliveryCost()- takeOutBean.getRedPacketCost()- getFillSubMoney();}
马上知道是收费=食物费+餐具费+送货费-红包-满减,然后如果后期有修改,改需要的地方就可以了。意识到了吗?实际上,只要我们把贪心的权责分开,然后就能够把各个分离出的部分写成函数(方法),我们的代码量实际上并没有增加多少,但是逻辑上更清晰了,各变量的耦合都降低,易读性和可扩展性都有显著的提升。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~