重构/优化代码

时间:2014-01-30 14:05:35

标签: java android

我已经编写了一种方法来尝试优化我的代码,因为同样的事情被调用了3次,但是,重写这种方法只是为了类似的问题。它基本上做同样的事情,但只是根据参数更改变量。

public void checkChance(String spawnX, int chance, int value) {
        if (spawnX.equals("smallX")) {
            if (player.getX() > screenWidth / 2) {
                if (chance > value) {
                    smallX = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
                } else {
                    smallX = random.nextInt((screenWidth / 2) - 0);
                }
            } else {
                if (chance > value) {
                    smallX = random.nextInt((screenWidth / 2) - 0);
                } else {
                    smallX = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
                }
            }
        } else if (spawnX.equals("mediumX")) {
            if (player.getX() > screenWidth / 2) {
                if (chance > value) {
                    mediumX = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
                } else {
                    mediumX = random.nextInt((screenWidth / 2) - 0);
                }
            } else {
                if (chance > value) {
                    mediumX = random.nextInt((screenWidth / 2) - 0);
                } else {
                    mediumX = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
                }
            }
        } else if (spawnX.equals("largeX")) {
            if (player.getX() > screenWidth / 2) {
                if (chance > value) {
                    largeX = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
                } else {
                    largeX = random.nextInt((screenWidth / 2) - 0);
                }
            } else {
                if (chance > value) {
                    largeX = random.nextInt((screenWidth / 2) - 0);
                } else {
                    largeX = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
                }
            }
        }

    }

理想情况下我喜欢它所以我只需要每个if中的部分(检查spawnX等于什么)并且只需更改变量get的设置。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

一开始,你可以做这样的事情(伪代码)

TYPE tmpX = (conditionSmall) ? smallX: ((conditionMedium)? medium : largeX);

然后,只需更改为tmpX即可。由于Java变量(除了基元之外)实际上是指向对象的指针,因此tmpX将指向您要修改的对象。

答案 1 :(得分:1)

关于方法签名以及此代码实现的目标没有过多地深入研究 [为什么减去0? screenWidth - (screenWidth / 2)总是简单地等于screenWidth / 2] ,我认为以下内容会更清晰,重复次数更少:

public void checkChance(final String spawnX, final int chance, final int value) {
    int intermediary;

    if (player.getX() > screenWidth / 2) {
        if (chance > value) {
            intermediary = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
        } else {
            intermediary = random.nextInt((screenWidth / 2) - 0);
        }
    } else {
        if (chance > value) {
            intermediary = random.nextInt((screenWidth / 2) - 0);
        } else {
            intermediary = random.nextInt(screenWidth - (screenWidth / 2)) + (screenWidth / 2);
        }
    }

    if (spawnX.equals("smallX")) {
        smallX = intermediary;
    } else if (spawnX.equals("mediumX")) {
        mediumX = intermediary;
    } else if (spawnX.equals("largeX")) {
        largeX = intermediary;
    }
}
相关问题