将变量初始化为虚拟值是不好的做法吗?

发布于 2024-08-31 13:28:16 字数 593 浏览 5 评论 0原文

此问题是对此问题 我刚刚问过。

有人声称这段代码“丑陋”,因为它将变量初始化为永远不会被读取的值:

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 技术交流群。

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

发布评论

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

评论(5

乖乖兔^ω^ 2024-09-07 13:28:16

我认为将变量初始化为除非语言需要否则不会使用的值是没有意义的。

例如,在 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.

旧人哭 2024-09-07 13:28:16

关于字符串循环,如果你把它改为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.

极度宠爱 2024-09-07 13:28:16

作为一种实践,我倾向于避免将变量设置为任意值,而是将它们初始化为默认值。

int x = 0;
string name = "";
bool done = false;
Person randomGuy = null; //or = new Person();

我最喜欢这种方法,因为它给你的代码带来了一种统一感,同时又不会强迫下一个来处理类似这样的事情: 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.

int x = 0;
string name = "";
bool done = false;
Person randomGuy = null; //or = new Person();

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.

执手闯天涯 2024-09-07 13:28:16

这取决于编译器。 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.

嘿哥们儿 2024-09-07 13:28:16

在我看来,将其称为“代码气味”可能更准确——按照 Martin Fowler 的说法。

我认为您不能单独更改默认初始化 - 它需要与其他重构方法结合使用。它还假设您已经重构了代码,以便不需要临时变量:

try{  
    FILE_NAME = buildFileName();  
    //Do other stuff with file name
}  
catch(Exception e){  
    ...  
    System.exit(1);  
} 

然后它还假设该代码段只是包含它的方法中的代码 - 即该方法只做一件事

当我正在编码,我会担心我使用了临时变量的虚拟值,但只有当我完成该部分的编码并且它按预期解决问题时我才会更改 - 并且只能与其他重构步骤结合使用。

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:

try{  
    FILE_NAME = buildFileName();  
    //Do other stuff with file name
}  
catch(Exception e){  
    ...  
    System.exit(1);  
} 

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.

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