Should I always/ever/never initialize object fields to default values?

前端 未结 17 2081
臣服心动
臣服心动 2020-12-09 14:49

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

相关标签:
17条回答
  • 2020-12-09 15:28

    This is tagged as language-agnostic but most of the answers are regarding C#.

    In C and C++, the best practice is to always initialize your values. There are some cases where this will be done for you such as static globals, but there shouldn't be a performance hit of any kind for redundantly initializing these values with most compilers.

    0 讨论(0)
  • 2020-12-09 15:28

    I wouldn't initialise them. If you keep the declaration as close as possible to the first use, then there shouldn't be any confusion.

    0 讨论(0)
  • 2020-12-09 15:29

    In the case where I cannot immediately set it to something useful

    int myValue = SomeMethod();
    

    I will set it to 0. That is more to avoid having to think about what the value would be otherwise. For me, the fact that integers are always set to 0 is not on the tip of my fingers, so when I see

    int myValue;
    

    it will take me a second to pull up that fact and remember what it will be set to, disrupting my thought process. For someone who has that knowledge readily available, they will encounter

    int myValue = 0;
    

    and wonder why the hell is that person setting it to zero, when the compiler would just do it for them. This thought would interrupt their thought process.

    So do which ever makes the most sense for both you and the team you are working in. If the common practice is to set it, then set it, otherwise don't.

    0 讨论(0)
  • 2020-12-09 15:32

    I always initialize fields explicitly in the constructor. For me, it's THE place to do it.

    0 讨论(0)
  • 2020-12-09 15:36

    I think a lot of that comes down to past experiences.

    In older and unamanged languages, the expectation is that the value is unknown. This expectation is retained by programmers coming from these languages.

    Almost all modern or managed languages have defined values for recently created variables, whether that's from class constructors or language features.

    For now, I think it's perfectly fine to initialize a value; what was once implicit becomes explicit. In the long run, say, in the next 10 to 20 years, people may start learning that a default value is possible, expected, and known - especially if they stay consistent across languages (eg, empty string for strings, 0 for numerics).

    0 讨论(0)
  • 2020-12-09 15:41

    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.

    0 讨论(0)
提交回复
热议问题