Java 一行变量声明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20117500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java one line variable declaration?
提问by Subodh Joshi
In my Java class i am declaring variable like this
在我的 Java 类中,我声明了这样的变量
BigDecimal sumFeeBilled = new BigDecimal(0), sumPaid = new BigDecimal(0);
Or we have to declare like this in multiple line
或者我们必须在多行中这样声明
BigDecimal sumFeeBilled = new BigDecimal(0);
BigDecimal sumPaid = new BigDecimal(0);
Which one we should follow ?
我们应该遵循哪一个?
采纳答案by Olly
This is simply a matter of taste and preference. However if you don't set guidelines it will become a hotbed of endless debate/arguments in most development teams, alongside Vim vs Emacs or IntelliJ vs Eclipse.
这只是品味和偏好的问题。但是,如果您不制定指导方针,它将成为大多数开发团队中无休止的争论/争论的温床,与 Vim 与 Emacs 或 IntelliJ 与 Eclipse 一起。
What I would recommend is setting coding standards for your team, and the simplest way to do this is to reference already-existing ones such as the Sun (now Oracle) Java Guidelines which in this case suggest using one declaration per line.
我推荐的是为你的团队设置编码标准,最简单的方法是参考已经存在的标准,比如 Sun(现在是 Oracle)Java 指南,在这种情况下,它建议每行使用一个声明。
Here what Sun's definitive guide says about declarations[1]:
Sun 的权威指南对声明 [1] 的说明如下:
6.1 Number Per Line
6.1 每行数
One declaration per line is recommended since it encourages commenting. In other words,
建议每行一个声明,因为它鼓励评论。换句话说,
int level; // indentation level
int size; // size of table
is preferred over
优先于
int level, size;
Do not put different types on the same line. Example:
不要将不同的类型放在同一行上。例子:
int foo, fooarray[]; //WRONG!
[1] http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#2991
[1] http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#2991
回答by Ivaylo Strandjev
The less things that happen on a single line the better. If you choose the second option the code will be easier to debug(you can put a breakpoint on the line where the second initialization happens skipping the first one for instance) and easier to read(IMHO). The only thing that this costs you is a single line. I think it is worth it.
在一条线上发生的事情越少越好。如果您选择第二个选项,代码将更容易调试(例如,您可以在第二个初始化发生的行上放置一个断点,跳过第一个)并且更容易阅读(恕我直言)。唯一让你付出代价的是一条线。我认为这是值得的。
回答by Oleksandr H
Second variant more readable. Think about people who will read your code
第二个变体更具可读性。想想那些会阅读你的代码的人
回答by Andrei Nicusan
Java best practices state "one declaration per line".
Java 最佳实践声明“每行一个声明”。
回答by Lingasamy Sakthivel
Declaration of multiple variables per line can reduce code readability and lead to programmer confusion. It could also cause confusion about the types of variables and their initial values. In particular, do not declare any of the following in a single declaration:
每行声明多个变量会降低代码可读性并导致程序员混淆。它还可能导致对变量类型及其初始值的混淆。特别是,不要在单个声明中声明以下任何一项:
1. Variables of different types
2. A mixture of initialized and uninitialized variables
In addition, You can declare multiple variables, and assign multiple variables, but not both at the same time
此外,您可以声明多个变量,并分配多个变量,但不能同时进行
回答by tilpner
This is your choice, but I often use the second one when using local variables, and for globals, I use the following to reduce clutter:
这是您的选择,但我经常在使用局部变量时使用第二个,对于全局变量,我使用以下方法来减少混乱:
public static final String CONSTANT_A = "constant a",
CONSTANT_B = "constant b",
CONSTANT_C = "constant c";
instead of
代替
public static final String CONSTANT_A = "constant a";
public static final String CONSTANT_B = "constant b";
public static final String CONSTANT_C = "constant c";
With this example it is not thatsignificant, but imagine having 40+ constants and tell me which of both you prefer...
在这个例子中,它不是那么重要,但是想象一下有 40 多个常量并告诉我你更喜欢哪一个......