Java 编程风格建议
我做了很多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
将所有静态整数转换为枚举可能是一个更好的主意。
It might be a better idea to convert all your static integers to an enum.
您可以考虑使用枚举,例如,
You might consider using an enum, e.g.,
以下是我的改进建议:
Here are my improvement suggestions:
看起来您陷入了 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.
您应该阅读有关枚举的内容。这显然应该属于 Enum 类。
You should read about Enums. This is clearly something that should be in an Enum class.
您可以使用枚举和抽象方法(以及一般而言良好的设计)获得更好的效果。很难针对您想要做的事情的具体情况提供具体的指导。以下是您可能编写的代码类型的示例:
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:
可以在该 switch 语句的
default
分支中完成,以防止 JVM 在满足其他条件之一时创建空 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.单真大括号样式更容易阅读(恕我直言),并且在 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.
将赋值放在 switch 之前的
place
中,然后将以下if
语句中的内容与 switch 语句结合起来,而不是测试两次。Put the assignment to
place
before the switch, and then combine what's in the followingif
statement with the switch statement instead of testing it all twice.除了使用牙套坚持一种风格之外:
vs
没有太多可以改进的地方。其余的只是个人喜好。
Other than sticking to one style with your braces:
vs
There isn't much you can improve on. The rest is just personal preference.
你在这里提出的建议通常会带有偏见,最终会引发无休止的争论。我只需安装 checkstyle,PMD、findBugs 等,并研究这些工具给出的每一个建议我
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