java - 重构/优化代码

标签 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 等于什么)并且只需更改设置的变量。我该怎么做?

最佳答案

无需深入研究有关方法签名的设计以及此代码要实现的目标[为什么要减去 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;
    }
}

关于java - 重构/优化代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21458654/

相关文章:

android - Android上的系统应用程序和特权应用程序有什么区别?

android - 类中的 NullPointerException

java - Java方法声明中的方括号是什么意思?

java - 为什么在声明子类的对象时会调用父类(super class)的构造函数? ( java )

java - 如何传递导出的 RMI-IIOP 对象的远程引用

java - 标记字符串数字中的每个数字?

android - firebase的app token在什么时期发生变化,如何管理?

android - 如何从android中的resourceid获取图像名称?

java - Android:通知栏中未显示通知

java - 我如何制作一棵圣诞树,其线条为 1、3、5、3、5、7、5、7、9、7、9