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

前端 未结 17 2117
臣服心动
臣服心动 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: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.

提交回复
热议问题