借助java lambdas的重构方法清理许多if ELSE

借助java lambdas的重构方法清理许多if ELSE,java,code-cleanup,Java,Code Cleanup,目前,我正在清理难以维护和测试的杂物,这些杂物基于必须在隔离中检查的条件: 条件的基本语义是什么? 大实体对象必须基于两个实体键(即Trans和Right)进行检查,以进行状态更改,如下例所示: 目前,if-else都在一个地方杂乱无章: if (oldTrans.getfOrder().equals(newTrans.getfOrder()) { compound.setIsStateChanged(true); LOGGER.info("major c

目前,我正在清理难以维护和测试的杂物,这些杂物基于必须在隔离中检查的条件:

条件的基本语义是什么? 大实体对象必须基于两个实体键(即Trans和Right)进行检查,以进行状态更改,如下例所示:

目前,if-else都在一个地方杂乱无章:

    if (oldTrans.getfOrder().equals(newTrans.getfOrder()) {
        compound.setIsStateChanged(true);
        LOGGER.info("major change detected");
        return compound;
    } if (oldTrans.getgOrder().equals(newTrans.getgOrder()) {
        compound.setIsStateChanged(true);
LOGGER.info("major change detected");
        return compound;   
    }
我在这里看到两个主要问题

  • 每个if都有一个return语句,由于if太多,很难知道什么时候和什么点方法存在

  • 对许多人来说,如果分支容易出错,条件的数量可能会增加

  • 为了避免太多基本上基于相同语义的if,从干净代码的角度来看,我尝试用多态的方法来解决它

    将枚举中的条件提取为常量,并实现一个将新对象和旧对象作为参数的检查器接口

        public interface ICheckStateChange<T>(T old, T new) {
            boolean check(T old, T new);
        }
    
        //implementations
        public TransChecker implements ICheckStateChange<Trans> {
    
          List<BiPredicate<Trans, Trans>> allTransConditions = transConditions.getValues();
    
        public boolean check(Trans oldTrans, Trans newTrans) {
             //all conditions null check here
            //loop through conditions
            for (BiPredicate<Trans, Trans> transCondition: allTransConditions) {
                if (transCondition).test()) {
                    return true;
                 LOGGER.info("major state change detected, taking apt action")
              }
        }
    
    public RightChecker implements ICheckStateChange<Right> {
    
          List<BiPredicate<Right, Right>> allTransConditions = RightConditions.getValues();
    
        public boolean check(Right oldRight, Right newRIght) {
             //all conditions null check here
            //loop through conditions
            for (BiPredicate<Right, Right> rightCondition: allRightConditions) {
                if (rightCondition).test()) {
                    return true;
                 LOGGER.info("major state change detected, taking apt action")
              }
        }
    

    我在这里的问题是关于在干净代码的后期借助lambda BiPredicates重构If-else的方法?可读性、可扩展性和可维护性

    我没有投反对票,但我不明白你的问题是什么。如果你的问题是“这段代码会编译吗?”,那么用你的编译器编译你的代码,你就会得到答案。问题是什么?如果代码没有编译,请附上相关的错误日志,否则请描述您是否观察到任何意外输出。问题是重构后的代码是否更干净,请更新问题语句以获得更清晰的理解。
        public interface ICheckStateChange<T>(T old, T new) {
            boolean check(T old, T new);
        }
    
        //implementations
        public TransChecker implements ICheckStateChange<Trans> {
    
          List<BiPredicate<Trans, Trans>> allTransConditions = transConditions.getValues();
    
        public boolean check(Trans oldTrans, Trans newTrans) {
             //all conditions null check here
            //loop through conditions
            for (BiPredicate<Trans, Trans> transCondition: allTransConditions) {
                if (transCondition).test()) {
                    return true;
                 LOGGER.info("major state change detected, taking apt action")
              }
        }
    
    public RightChecker implements ICheckStateChange<Right> {
    
          List<BiPredicate<Right, Right>> allTransConditions = RightConditions.getValues();
    
        public boolean check(Right oldRight, Right newRIght) {
             //all conditions null check here
            //loop through conditions
            for (BiPredicate<Right, Right> rightCondition: allRightConditions) {
                if (rightCondition).test()) {
                    return true;
                 LOGGER.info("major state change detected, taking apt action")
              }
        }
    
    public enum rightConditions {
        FORDER_CHANGE_NULL_TO_NOT_NULL((Order old, Order new)
           -> old == null && new != null),
    
        //to be replaced by the right condition
        GORDER_CHANGE_FROM_OPEN_TO_DONE((Order old, Order new)
           -> old == null && new != null)
    
        //to be replaced by the right condition
        LORDER_CHANGE_FROM_OPEN_TO_REVERTED((Order old, Order new)
           -> old == null && new != null)
    
    ........
       }