我应该永远/永远/永远不会将对象字段初始化为默认值吗? [英] Should I always/ever/never initialize object fields to default values?

查看:100
本文介绍了我应该永远/永远/永远不会将对象字段初始化为默认值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里代码样式问题。



我查看了这个问题询问是否.NET CLR将真正总是初始化字段值。 (答案是。)但它让我震惊,我不知道它总是一个好主意,它有这样做。我的想法是,如果我看到这样的声明:

  int myBlorgleCount = 0; 

我有一个很好的想法,程序员希望计数从零开始,至少在不久的将来。另一方面,如果我只看到:

  int myBlorgleCount; 



如果0是合法或合理的价值,我没有真正的想法。如果程序员刚刚开始读取和修改它,我不知道程序员是否想在它设置一个值之前开始使用它,或者如果他们期望它为零等。



另一方面,一些相当聪明的人,和Visual Studio代码清理实用工具,告诉我删除这些冗余的声明。关于这一点的一般共识是什么? (是否有共识?)



我将这标记为语言不可知,但如果有一个奇怪的情况,在那里它是一个特别的好主意,

:虽然我确实认为这个问题是语言不可知的,但显然不是

编辑:我非常感谢John的回答,但这正是我的感觉正在搜索。我理解.NET(或Java或任何)将做的工作,并初始化值一致和正确。我说的是,如果我看到的代码是修改以前没有明确设置在代码中的值,我,作为代码维护者,不知道原始的编码器是否意味着它是默认值,或者只是忘记设置该值,或者希望将其设置在其他位置等。

解决方案




  • 保持代码尽可能明确。

  • 不要依赖语言特定的方式初始化如果你不必。

  • 未来的程序员会感谢你。

  • 管理层会感谢你。

  • 为什么要对某些事情进行模糊处理?



更新:未来的维护者可能来自不同的背景。它真的不是什么是正确的,它是更多的是最容易的长期。


Code styling question here.

I looked at this question which asks if the .NET CLR will really always initialize field values. (The answer is yes.) But it strikes me that I'm not sure that it's always a good idea to have it do this. My thinking is that if I see a declaration like this:

int myBlorgleCount = 0;

I have a pretty good idea that the programmer expects the count to start at zero, and is okay with that, at least for the immediate future. On the other hand, if I just see:

int myBlorgleCount;

I have no real immediate idea if 0 is a legal or reasonable value. And if the programmer just starts reading and modifying it, I don't know whether the programmer meant to start using it before they set a value to it, or if they were expecting it to be zero, etc.

On the other hand, some fairly smart people, and the Visual Studio code cleanup utility, tell me to remove these redundant declarations. What is the general consensus on this? (Is there a consensus?)

I marked this as language agnostic, but if there is an odd case out there where it's specifically a good idea to go against the grain for a particular language, that's probably worth pointing out.

EDIT: While I did put that this question was language agnostic, it obviously doesn't apply to languages like C, where no value initialization is done.

EDIT: I appreciate John's answer, but it is exactly what I'm not looking for. I understand that .NET (or Java or whatever) will do the job and initialize the values consistently and correctly. What I'm saying is that if I see code that is modifying a value that hasn't been previously explicitly set in code, I, as a code maintainer, don't know if the original coder meant it to be the default value, or just forgot to set the value, or was expecting it to be set somewhere else, etc.

解决方案

Think long term maintenance.

  • Keep the code as explicit as possible.
  • Don't rely on language specific ways to initialize if you don't have to. Maybe a newer version of the language will work differently?
  • Future programmers will thank you.
  • Management will thank you.
  • Why obfuscate things even the slightest?

Update: Future maintainers may come from a different background. It really isn't about what is "right" it is more what will be easiest in the long run.

这篇关于我应该永远/永远/永远不会将对象字段初始化为默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆