将变量初始化为虚拟值是不好的做法吗?
此问题是对此问题 我刚刚问过。
有人声称这段代码“丑陋”,因为它将变量初始化为永远不会被读取的值:
String tempName = null;
try{
tempName = buildFileName();
}
catch(Exception e){
...
System.exit(1);
}
FILE_NAME = tempName;
这确实是不好的做法吗?是否应该避免将变量初始化为永远不会实际使用的虚拟值?
(编辑 - 那么在将值连接到字符串的循环之前将字符串变量初始化为 ""
怎么样?或者这是一个单独的类别?
例如
String whatever = "";
for(String str : someCollection){
whatever += str;
}
)
This question is a result of the answers to this question that I just asked.
It was claimed that this code is "ugly" because it initializes a variable to a value that will never be read:
String tempName = null;
try{
tempName = buildFileName();
}
catch(Exception e){
...
System.exit(1);
}
FILE_NAME = tempName;
Is this indeed bad practice? Should one avoid initializing variables to dummy values that will never actually be used?
(EDIT -
And what about initializing a String variable to ""
before a loop that will concatenate values to the String...? Or is this in a separate category?
e.g.
String whatever = "";
for(String str : someCollection){
whatever += str;
}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为将变量初始化为除非语言需要否则不会使用的值是没有意义的。
例如,在 C# 中,声明的字符串变量的默认值为 null,因此您甚至无法通过显式写出它来获得任何内容。这主要是一种风格选择,但由于字符串是不可变的,将其初始化为其他内容实际上会在内存中分配一个额外的字符串,无论如何你都会扔掉它。其他语言可能会带来其他考虑因素。
I think there's no point in initializing variables to values that won't be used unless required by the language.
For example, in C#, the default value for a declared string variable is null, so you're not even gaining anything by explicitly writing it out. It's mostly a style choice, but since strings are immutable, initializing it to something else would actually allocate an extra string in memory that you'd just throw away anyway. Other languages may impose other considerations.
关于字符串循环,如果你把它改为StringBuilder,你甚至不用考虑它。
编辑:删除了其他人更好回答的部分。
Regarding the string loop, if you change it to a StringBuilder instead you don't even have to think about it.
Edit: removed bits better answered by others.
作为一种实践,我倾向于避免将变量设置为任意值,而是将它们初始化为默认值。
即
我最喜欢这种方法,因为它给你的代码带来了一种统一感,同时又不会强迫下一个来处理类似这样的事情:
string name = "luke skywalker";
这就是更多个人喜好,因此程序员之间会有所不同。
至少,您应该遵循项目设定的标准。您将了解遗留代码如何处理这些事情,并且最好遵循这一点,以便整个系统的整体编码风格是相同的。
As a practice, I tend to avoid setting variables to arbitrary values, and instead initialize them to a default value.
i.e.
I like this method best because it brings a sense of uniformity to your code while not forcing the next guy that comes along to deal with stuff like:
string name = "luke skywalker";
This is all more of a personal preference, so it will vary between programmers.
At the very lest, you should be following the standards that your project as set. You'll have an idea of how the legacy code handles these things, and it's probably best to conform to that so the overall coding style is the same throughout the system.
这取决于编译器。 C#编译器要求变量在使用前进行初始化。但CLR没有这个要求。在运行时,CLR 会验证变量是否已初始化。如果不是,它将抛出空引用异常。
It depends on the compiler. C# compiler requires that the variable be initialized before using. But the CLR does not have this requirement. At run time the CLR verifies if the variable is initialized or no. If not it will throw a nullreference exception.
在我看来,将其称为“代码气味”可能更准确——按照 Martin Fowler 的说法。
我认为您不能单独更改默认初始化 - 它需要与其他重构方法结合使用。它还假设您已经重构了代码,以便不需要临时变量:
然后它还假设该代码段只是包含它的方法中的代码 - 即该方法只做一件事
当我正在编码,我会担心我使用了临时变量的虚拟值,但只有当我完成该部分的编码并且它按预期解决问题时我才会更改 - 并且只能与其他重构步骤结合使用。
In my opinion, it might be more accurate to refer to it as a "code smell" - in the Martin Fowler sense of the word.
I don't think you can change your default initialisation in isolation - it would need to be made in conjunction with other refactoring methods. It is also assuming that you have refactored your code so that you don't need temp variables:
It also then makes the assumtion that this code segment is only code in the method in which it is contained - i.e. the method only does one thing
When I'm coding I would be concerned that I am using dummy values with temp variables but I would only change when I am finished coding that section and it solves the problem as intended - and only in conjunction with other refactoring steps.