java 拆分声明和分配 = 良好实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15953895/
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
Splitting Declaration and Assignment = Good Practice?
提问by Fiery Phoenix
Being an avid user of NetBeans, I keep getting suggestions to split my variable declarations and assignments when I introduce a new variable. As a super quick example off the top of my head, let's take this Java line:
作为 NetBeans 的狂热用户,当我引入新变量时,我不断收到关于拆分变量声明和赋值的建议。作为我脑海中的一个超级简单的例子,让我们看看这个 Java 代码行:
String someInput = JOptionPane.showInputDialog(null, "- Enter something: ");
versus:
相对:
String someInput;
someInput = JOptionPane.showInputDialog(null, "- Enter something: ");
NetBeans seems to prefer the latter (I wouldn't know about other IDEs, though). While it clearly works both ways, which would be considered 'good practice', if at all? Or s it purely a matter of personal preference?
NetBeans 似乎更喜欢后者(不过我不知道其他 IDE)。虽然它显然是双向的,但如果有的话,这会被认为是“良好的做法”吗?还是纯粹是个人喜好问题?
(Obviously splitting the two means an extra line that you could've just combined into one, but that's beside the point.)
(显然,将两者分开意味着一条额外的线,您可以将其合并为一条线,但这无关紧要。)
回答by Bill the Lizard
There's no reason to split the declaration and the assignment if you're just going to have them on consecutive lines. I'd only split them if the assignment were conditional, or if it needed to go in a separate code block (like a try/catch, or if the assignment goes in a constructor, etc.).
如果您只想将它们放在连续的行上,则没有理由拆分声明和分配。如果赋值是有条件的,或者如果它需要进入单独的代码块(如 try/catch,或者如果赋值进入构造函数等),我只会拆分它们。
回答by jpredham
A common pattern that traces back to early statically typed programming is to declare all the variables you need at the top of the block they need to be scoped in, and then assign to those values subsequently.
追溯到早期静态类型编程的一个常见模式是在需要限定范围的块的顶部声明您需要的所有变量,然后随后分配给这些值。
With that said, as long as you're able to clearly communicate the intent of your code to the people you work on it with, or yourself in a year's time, it shouldn't really matter.
话虽如此,只要您能够在一年内将代码的意图清楚地传达给与您一起工作的人,或者您自己,那么这并不重要。
Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. -- Donald Knuth
让我们改变对程序构建的传统态度:与其想象我们的主要任务是指导计算机做什么,不如集中精力向人类解释我们希望计算机做什么。——唐纳德·克努斯
回答by Stanley De Boer
This was an option added to NetBeans somewhere between 7.0 and 7.3. If you don't like this hint it can be changed by unchecking: Tools > Options > Editor > Hints > Suggestions > Split Declaration
这是在 7.0 和 7.3 之间添加到 NetBeans 的一个选项。如果您不喜欢此提示,可以通过取消选中来更改它:工具 > 选项 > 编辑器 > 提示 > 建议 > 拆分声明
回答by Boris the Spider
Netbeans suggests this as it can speed up coding, for example consider this assignment
Netbeans 建议这样做,因为它可以加快编码速度,例如考虑这个分配
final String myString = "somevalue"
You then decide that actually the value depends on some boolean
然后您决定该值实际上取决于某些 boolean
final String myString;
if(something) {
myString = "somevalue"
} else {
myString = "someothervalue"
}
To convert one from to the other you can use the code hint to spilt declaration and assignment and the if...else
template to generate the code in very few steps.
要将一个从一个转换为另一个,您可以使用代码提示溢出声明和赋值,并使用if...else
模板在很少的步骤中生成代码。
In general if you are assigning unconditionally then there is not reason to split declaration and assignment.
一般来说,如果您是无条件分配,则没有理由拆分声明和分配。
回答by rlinden
Code Complete, which is well regarded by most of the programming community, suggests in its page 241 that as a matter of defensive programming you can either initialize each variable as it's declared (your first option) or initialize it close to where it is first used (your second option).
Code Complete 受到大多数编程社区的好评,在其第 241 页中建议,作为防御性编程的问题,您可以按照声明的方式初始化每个变量(您的第一个选项),或者在接近首次使用的地方初始化它(你的第二个选择)。
Nevertheless, the book suggests that the second option is better because if you use the first option, your variable may have changed between its declaration and its usage.
尽管如此,本书建议第二个选项更好,因为如果您使用第一个选项,您的变量可能在其声明和使用之间发生了变化。
In your case, the difference between declaration and usage in the second case is a single line. Hence, the book's concerns do not apply. Nevertheless, its concerns are legitimate and this may be the reason Netbeans designers included this tip (even though they overused it).
在您的情况下,第二种情况下声明和使用之间的区别是一行。因此,本书的担忧不适用。尽管如此,它的担忧是合理的,这可能是 Netbeans 设计人员包含此提示的原因(即使他们过度使用了它)。
回答by NPE
I don't think it's a big deal. Pick a style you like, and stick to it throughout (if you're in a team, it helps if everyone is laying out their code similarly).
我不认为这有什么大不了的。选择一种你喜欢的风格,并始终坚持它(如果你在一个团队中,如果每个人都以类似的方式布置他们的代码会很有帮助)。
I personally prefer to initialize my variables right at the point of declaration. For that reason, I would prefer the former over the latter.
我个人更喜欢在声明时初始化我的变量。出于这个原因,我更喜欢前者而不是后者。