【问题标题】:Is there a way to refactor these if statements?有没有办法重构这些 if 语句?
【发布时间】:2020-06-26 19:11:58
【问题描述】:

只是想知道是否有办法重构以下代码?我是 Java 新手,正在尝试使用 DRY 代码 - 我编写了以下代码,但似乎需要检查很多条件

void printDirection() {
  if (yDirection > 0) {
    if (xDirection < 0) {
      println("Travelling South-West");
    } else {
      println("Travelling South-East");
    }
  } else if (yDirection < 0) {
    if (xDirection <0) {
      println("Travelling North-West");
    } else {
      println("Travelling North-East");
    }
  }
}

提前感谢您的帮助!

【问题讨论】:

  • 您可以将North/South 部分和East/West 部分分配给两个字符串,然后打印包含这两个字符串的消息。

标签: java if-statement conditional-statements refactoring


【解决方案1】:

您可以单独评估北/南和东/西条件,并将方向粘贴到您的信息中。

System.out.printf("Travelling %s-%s%n", (yDirection < 0 ? "North" : "South"),
                  (xDirection < 0 ? "West" : "East"));

我从您问题中的代码假设您只关心这四个互补方向(不是正北、正东、静止等)。

【讨论】:

  • 事件更好,可以使用枚举来编码NORTH, SOUTH, WEST, EAST
【解决方案2】:

如果你真的想让它干燥,可以使用操作符来完成?但它既不容易阅读也不推荐。它用于目标是尽可能快的编程比赛。

它遵循以下方案: (条件?WhatHappenIfConditionIsTrue:WhatHappenIfConditionIsFalse); 您可以在作业中使用它:

int i = (a>0)?a:0;

在这种情况下,如果 a>0 则 i=a,否则 a=0

在你的情况下,我会这样做

void printDirection()
{
    System.out.println("Travelling " + (yDirection > 0?"South":"North") + "-" + (xDirection>0?"East":"West"));
}

【讨论】:

  • 啊,已经有人这样回答了,但我很欣赏为什么有人会这样编码的解释!谢谢你:)
【解决方案3】:

一些建议: 1.由于x,y组合;有五个状态;您可以使用枚举类型来定义这些状态; 2、如果你想减少代码中的if...else语句,请参考状态机设计模式;但我认为,在你的情况下,状态很简单,不需要太复杂

public class Status {

    public enum Direction {
        SOUTH_WEST((x, y) -> y > 0 && x < 0, "Travelling South-West")
        , SOUTH_EAST((x, y) -> y >0 && x > 0, "Travelling South-East")
        , NORTH_EAST((x, y) -> x > 0 && y < 0, "Travelling North-East")
        , NORTH_WEST((x,y) -> x < 0 && y < 0, "Travelling North-West"), CENTER((x,y) -> x == 0 && y == 0, "");

        BiPredicate<Integer, Integer> bp;
        String desc;

        public BiPredicate<Integer, Integer> getBp() {
            return bp;
        }
        public void setBp(BiPredicate<Integer, Integer> bp) {
            this.bp = bp;

        }

        public String getDesc() {
            return desc;
        }
        public void setDesc(String desc) {
            this.desc = desc;
        }
        private Direction(BiPredicate<Integer, Integer> bp, String desc) {
            this.bp = bp;
            this.desc = desc;
        }
        public static Direction getDirection(int x, int y) {
            for (Direction direction : Direction.values()) {
                if(direction.getBp().test(x, y)) {
                    return direction;
                }
            }
            return null;
        }
    }

    public static void main(String[] args) {
        Direction d =  Direction.getDirection(3, 4);
        System.out.println(d.getDesc());
        /*      if(d == Direction.SOUTH_WEST){
                    System.out.println("do some thing");
                } else if(d == Direction.SOUTH_EAST){
                    System.out.println("do some thing");
                } else if(d == Direction.NORTH_EAST){
                    System.out.println("do some thing");
                } else if(d == Direction.NORTH_WEST){
                    System.out.println("do some thing");
                }*/
    }
}

【讨论】:

    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多