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

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

    I think it should be done if it really helps to make the code more understandable.

    But I think this is a general problem with all language features. My opinion on that is: If it is an official feature of the language, you can use it. (Of course there are some anti-features which should be used with caution or avoided at all, like a missing option explicit in Visual Basic or diamond inheritance in C++)

    There was I time when I was very paranoid and added all kinds of unnecessary initializations, explicit casts, über-paranoid try-finally blocks, ... I once even thought about ignoring auto-boxing and replacing all occurrences with explicit type conversions, just "to be on the safe side".

    The problem is: There is no end. You can avoid almost all language features, because you do not want to trust them.

    Remember: It's only magic until you understand it :)

提交回复
热议问题