都说大量使用if else不好,使代码有坏味道,像下面这种代码应该如何改善呢?就是如何去除那些if else? 
小弟拜谢~!     public Integer getShouldFilledWorkload(Date startDate, Date endDate) {   
           
        Long count = 0l;                              //起始时间和结束时间之间的天数   
         Integer shouldFilledWorkDay = 0;              //除去周六和周日剩下的天数   
           
         Calendar cal = Calendar.getInstance();   
           
        cal.setTime(startDate);   
        count = (endDate.getTime() - startDate.getTime())/1000/60/60/24 + 1;   
           
        for(int i=0;i<count;i++) {   
            if(cal.get(Calendar.DAY_OF_WEEK)!=Calendar.SATURDAY&&cal.get(Calendar.DAY_OF_WEEK)!=Calendar.SUNDAY){   
                shouldFilledWorkDay+=1;   
            }   
            cal.add(Calendar.DAY_OF_WEEK, 1);   
        }   
           
        return shouldFilledWorkDay*8;   
    }   
  
    public Integer getDayCount(List<DomainActorVO> l, Date startDate, Date endDate) {   
        Integer workDayCount = 0;   
        if(startDate==null||endDate==null||l==null) {   
            return 0;   
        }   
        for(DomainActorVO daVO:l) {   
            if(daVO.getJoinTime()==null) {   
                continue;   
            }   
            if(daVO.getLeaveTime()==null) {   
                if(endDate.compareTo(daVO.getJoinTime())<0) {   
                    workDayCount += getShouldFilledWorkload(startDate,endDate);   
                    continue;   
                }   
                if(startDate.compareTo(daVO.getJoinTime())<0) {   
                    workDayCount += (getShouldFilledWorkload(startDate,daVO.getJoinTime())-8);   
                }   
            }else {   
                if(startDate.compareTo(daVO.getLeaveTime())>0) {   
                    workDayCount += getShouldFilledWorkload(startDate,endDate);   
                    continue;   
                }   
                if(endDate.compareTo(daVO.getJoinTime())<0) {   
                    workDayCount += getShouldFilledWorkload(startDate,endDate);   
                    continue;   
                }   
                if(startDate.compareTo(daVO.getJoinTime())<0) {   
                    workDayCount += (getShouldFilledWorkload(startDate,daVO.getJoinTime())-8);   
                }   
                if(endDate.compareTo(daVO.getLeaveTime())>0) {   
                    workDayCount += (getShouldFilledWorkload(daVO.getLeaveTime(),endDate)-8);   
                }   
            }   
        }   
        return workDayCount;   
    }

解决方案 »

  1.   

    没有大量嵌套if else呀 不用改也行。
      

  2.   

    嗯  我觉得也是  if...else很清晰的
    可以适当加点注释啊  这样就不会看晕了...
      

  3.   

    还有个办法减少if else,就是使用java的多态的特性,让子类自己处理特殊的操作也可以减少if else。 不过lz的这些操作貌似都是必需判断的,所以代码多点很正常啊。
      

  4.   

    可读性可以,实在要改就改:
    if(startDate.compareTo(daVO.getLeaveTime())>0) {   
                        workDayCount += getShouldFilledWorkload(startDate,endDate);   
                        continue;   
                    }   
                    if(endDate.compareTo(daVO.getJoinTime())<0) {   
                        workDayCount += getShouldFilledWorkload(startDate,endDate);   
                        continue;   
                    }   
    改为:
    if((startDate.compareTo(daVO.getLeaveTime())>0) || (endDate.compareTo(daVO.getJoinTime())<0)) {   
                        workDayCount += getShouldFilledWorkload(startDate,endDate);   
                        continue;   
                    }   尽量把兼容条件,这样if else就少了