Java 编程风格建议

发布于 2024-12-10 19:32:25 字数 1468 浏览 0 评论 0原文

我做了很多 Java 编程,然后放弃了它并做了一堆 ruby​​。现在我又回到了Java,我想知道我的编程风格是否很奇怪。

下面的代码对我来说非常冗长,问题是它是相当地道的 Java 吗? 您有什么建议/改进建议吗?

    final static int FORCE_RIGHT = 0;
    final static int FORCE_DOWN = 1;
    final static int FORCE_LEFT = 2;
    final static int FORCE_UP = 3;
    final static int IMP_RIGHT = 4;
    final static int IMP_DOWN = 5;
    final static int IMP_LEFT = 6;
    final static int IMP_UP = 7;


    public void applyForce(int dir) {
        counter++;
        Vector2 vect = new Vector2();
        switch (dir) {
            case FORCE_RIGHT: vect = new Vector2(3.0f, 0.0f); break;
            case IMP_RIGHT: vect = new Vector2(1.0f, 0.0f); break;
            case FORCE_LEFT: vect = new Vector2(-3.0f, 0.0f); break;
            case IMP_LEFT: vect = new Vector2(-1.0f, 0.0f); break;
            case FORCE_UP: vect = new Vector2(0.0f, -3.0f); break;
            case IMP_UP: vect = new Vector2(0.0f, -1.0f); break;
            case FORCE_DOWN: vect = new Vector2(0.0f, 3.0f); break;
            case IMP_DOWN: vect = new Vector2(0.0f, 1.0f); break;
        }
        Vector2 place = body.getWorldCenter();
        if (dir == FORCE_RIGHT || dir == FORCE_LEFT || dir == FORCE_DOWN || dir == FORCE_UP) 
            { 
            body.applyForce(vect, place);
            }
        else 
            { 
            body.applyLinearImpulse(vect, place);
            }
        Log.v("CAR", "Applied force: " + dir + "("+counter+")");
    }

I did a lot of Java programming and then dropped it and did a bunch of ruby. Now I am back in Java and I am wondering whether my programming style is strange.

My code below feels very verbose to me, and the question is it reasonably idiomatic Java?
Any suggestions/improvements that you would recommend?


    final static int FORCE_RIGHT = 0;
    final static int FORCE_DOWN = 1;
    final static int FORCE_LEFT = 2;
    final static int FORCE_UP = 3;
    final static int IMP_RIGHT = 4;
    final static int IMP_DOWN = 5;
    final static int IMP_LEFT = 6;
    final static int IMP_UP = 7;


    public void applyForce(int dir) {
        counter++;
        Vector2 vect = new Vector2();
        switch (dir) {
            case FORCE_RIGHT: vect = new Vector2(3.0f, 0.0f); break;
            case IMP_RIGHT: vect = new Vector2(1.0f, 0.0f); break;
            case FORCE_LEFT: vect = new Vector2(-3.0f, 0.0f); break;
            case IMP_LEFT: vect = new Vector2(-1.0f, 0.0f); break;
            case FORCE_UP: vect = new Vector2(0.0f, -3.0f); break;
            case IMP_UP: vect = new Vector2(0.0f, -1.0f); break;
            case FORCE_DOWN: vect = new Vector2(0.0f, 3.0f); break;
            case IMP_DOWN: vect = new Vector2(0.0f, 1.0f); break;
        }
        Vector2 place = body.getWorldCenter();
        if (dir == FORCE_RIGHT || dir == FORCE_LEFT || dir == FORCE_DOWN || dir == FORCE_UP) 
            { 
            body.applyForce(vect, place);
            }
        else 
            { 
            body.applyLinearImpulse(vect, place);
            }
        Log.v("CAR", "Applied force: " + dir + "("+counter+")");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

嘿咻 2024-12-17 19:32:25

将所有静态整数转换为枚举可能是一个更好的主意。

It might be a better idea to convert all your static integers to an enum.

嗼ふ静 2024-12-17 19:32:25

您可以考虑使用枚举,例如,

public enum Force {
    FORCE_RIGHT (3.0f, 0.0f), 
    FORCE_DOWN (1.0f, 0.0f),
    // ... 
    IMP_UP (0.0f, 1.0f);

    private int f1,f2;

    private Force(float f1, float f2) {
      this.f1 = f1;
      this.f2 = f2;
    }
    public void getF1 { return f1; }
    public void getF2 { return f2; }
}

You might consider using an enum, e.g.,

public enum Force {
    FORCE_RIGHT (3.0f, 0.0f), 
    FORCE_DOWN (1.0f, 0.0f),
    // ... 
    IMP_UP (0.0f, 1.0f);

    private int f1,f2;

    private Force(float f1, float f2) {
      this.f1 = f1;
      this.f2 = f2;
    }
    public void getF1 { return f1; }
    public void getF2 { return f2; }
}
深陷 2024-12-17 19:32:25

以下是我的改进建议:

  • 枚举更聪明,因为您还可以应用行为,例如在日志中您不仅会看到一个数字
  • 而不是 switch 语句,您可以应用更通用的算法,但这不一定更好,因为那么它就更难以理解和更改,即使它的代码行数较少,
  • 如果您之后再次调用构造函数,也不需要执行 new Vector2()
  • 使用一致的代码格式(例如放置 {、空格字符、.. .)

Here are my improvement suggestions:

  • enums are smarter, since you can also apply behaviour, for example in your log you'd not only see a number
  • instead of the switch statement you could apply a more generic algorithm, but it's not necessarily better, since it then is more difficult to understand and change, even if it comes with less lines of code
  • no need to do new Vector2() if you call the constructor again afterwards
  • use consistent code formatting (e.g. place to put {, space charachters, ...)
昔梦 2024-12-17 19:32:25

看起来您陷入了 1.5 之前的 Java :) 阅读有关 枚举EnumSet 以及一些关于 Java 实践 答案对你来说是显而易见的。

Looks like you're stuck in pre-1.5 Java :) Read about Enum's, EnumSet and a bit about Java Practices and the answer will be obvious to you.

牛↙奶布丁 2024-12-17 19:32:25

您应该阅读有关枚举的内容。这显然应该属于 Enum 类。

You should read about Enums. This is clearly something that should be in an Enum class.

提赋 2024-12-17 19:32:25

您可以使用枚举和抽象方法(以及一般而言良好的设计)获得更好的效果。很难针对您想要做的事情的具体情况提供具体的指导。以下是您可能编写的代码类型的示例:

public class Physics {
    public void apply(final Body body, final Vect place, final Direction dir) {
    float magnitude = findMagnitude();
    PhenomType phenomType = findPhenom();
    phenomType.apply(body, dir.createVect(magnitude), place);
    System.out.println("CAR Applied force: " + dir);
    }

    public enum Direction {
    UP {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(0.0f, magnitude);
        }
    },
    DOWN {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(0.0f, -magnitude);
        }
    },
    LEFT {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(-magnitude, 0.0f);
        }
    },
    RIGHT {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(magnitude, 0.0f);
        }
    };

    public abstract Vect createVect(float magnitude);
    }

    public enum PhenomType {
    FORCE {
        @Override
        public void apply(final Body body, final Vect vect, final Vect place) {
        body.applyForce(vect, place);

        }
    },
    IMPULSE {
        @Override
        public void apply(final Body body, final Vect vect, final Vect place) {
        body.applyImpulse(vect, place);
        }
    };

    public abstract void apply(Body body, Vect vect, Vect place);
    }
}

You can achieve better with enums and abstract methods (and good design, in general). It's hard to provide concrete guidance with the specifics of what you are trying to do. Here is an example of the type of code you might write:

public class Physics {
    public void apply(final Body body, final Vect place, final Direction dir) {
    float magnitude = findMagnitude();
    PhenomType phenomType = findPhenom();
    phenomType.apply(body, dir.createVect(magnitude), place);
    System.out.println("CAR Applied force: " + dir);
    }

    public enum Direction {
    UP {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(0.0f, magnitude);
        }
    },
    DOWN {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(0.0f, -magnitude);
        }
    },
    LEFT {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(-magnitude, 0.0f);
        }
    },
    RIGHT {
        @Override
        public Vect createVect(final float magnitude) {
        return new Vect(magnitude, 0.0f);
        }
    };

    public abstract Vect createVect(float magnitude);
    }

    public enum PhenomType {
    FORCE {
        @Override
        public void apply(final Body body, final Vect vect, final Vect place) {
        body.applyForce(vect, place);

        }
    },
    IMPULSE {
        @Override
        public void apply(final Body body, final Vect vect, final Vect place) {
        body.applyImpulse(vect, place);
        }
    };

    public abstract void apply(Body body, Vect vect, Vect place);
    }
}
苍景流年 2024-12-17 19:32:25
Vector2 vect = new Vector2();

可以在该 switch 语句的 default 分支中完成,以防止 JVM 在满足其他条件之一时创建空 Vector2。

Vector2 vect = new Vector2();

could be done in a default branch of that switch statement to keep the JVM from creating an empty Vector2 when one of the other conditions would be satisfied.

半山落雨半山空 2024-12-17 19:32:25

单真大括号样式更容易阅读(恕我直言),并且在 Java 中更常见:

if (dir == FORCE_RIGHT || dir == FORCE_LEFT || dir == FORCE_DOWN || dir == FORCE_UP) {
body.applyForce(vect, 地点);
标准缩进

是 4 个空格。

One-true-brace style is easier to read (IMHO), and more common in Java:

if (dir == FORCE_RIGHT || dir == FORCE_LEFT || dir == FORCE_DOWN || dir == FORCE_UP) {
body.applyForce(vect, place);
}

Standard indent is 4 spaces.

请你别敷衍 2024-12-17 19:32:25

将赋值放在 switch 之前的 place 中,然后将以下 if 语句中的内容与 switch 语句结合起来,而不是测试两次。

Put the assignment to place before the switch, and then combine what's in the following if statement with the switch statement instead of testing it all twice.

依 靠 2024-12-17 19:32:25

除了使用牙套坚持一种风格之外:

if (condition) {

vs

if (condition)
{

没有太多可以改进的地方。其余的只是个人喜好。

Other than sticking to one style with your braces:

if (condition) {

vs

if (condition)
{

There isn't much you can improve on. The rest is just personal preference.

情绪操控生活 2024-12-17 19:32:25

你在这里提出的建议通常会带有偏见,最终会引发无休止的争论。我只需安装 checkstylePMDfindBugs 等,并研究这些工具给出的每一个建议我

Suggestions that you will get here will usually be biased and will end up in endless arguments. I would just install checkstyle, PMD, findBugs etc and work on every suggestion that these tools give me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文