Initialize class fields in constructor or at declaration?

前端 未结 15 2128
南旧
南旧 2020-11-22 01:16

I\'ve been programming in C# and Java recently and I am curious where the best place is to initialize my class fields.

Should I do it at declaration?:



        
相关标签:
15条回答
  • 2020-11-22 01:55

    In C# it doesn't matter. The two code samples you give are utterly equivalent. In the first example the C# compiler (or is it the CLR?) will construct an empty constructor and initialise the variables as if they were in the constructor (there's a slight nuance to this that Jon Skeet explains in the comments below). If there is already a constructor then any initialisation "above" will be moved into the top of it.

    In terms of best practice the former is less error prone than the latter as someone could easily add another constructor and forget to chain it.

    0 讨论(0)
  • 2020-11-22 01:58

    What if I told you, it depends?

    I in general initialize everything and do it in a consistent way. Yes it's overly explicit but it's also a little easier to maintain.

    If we are worried about performance, well then I initialize only what has to be done and place it in the areas it gives the most bang for the buck.

    In a real time system, I question if I even need the variable or constant at all.

    And in C++ I often do next to no initialization in either place and move it into an Init() function. Why? Well, in C++ if you're initializing something that can throw an exception during object construction you open yourself to memory leaks.

    0 讨论(0)
  • 2020-11-22 01:58

    There is a slight performance benefit to setting the value in the declaration. If you set it in the constructor it is actually being set twice (first to the default value, then reset in the ctor).

    0 讨论(0)
  • 2020-11-22 01:58

    When you don't need some logic or error handling:

    • Initialize class fields at declaration

    When you need some logic or error handling:

    • Initialize class fields in constructor

    This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity. If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used.

    From https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html .

    0 讨论(0)
  • 2020-11-22 02:00

    In Java, an initializer with the declaration means the field is always initialized the same way, regardless of which constructor is used (if you have more than one) or the parameters of your constructors (if they have arguments), although a constructor might subsequently change the value (if it is not final). So using an initializer with a declaration suggests to a reader that the initialized value is the value that the field has in all cases, regardless of which constructor is used and regardless of the parameters passed to any constructor. Therefore use an initializer with the declaration only if, and always if, the value for all constructed objects is the same.

    0 讨论(0)
  • 2020-11-22 02:00

    The design of C# suggests that inline initialization is preferred, or it wouldn't be in the language. Any time you can avoid a cross-reference between different places in the code, you're generally better off.

    There is also the matter of consistency with static field initialization, which needs to be inline for best performance. The Framework Design Guidelines for Constructor Design say this:

    ✓ CONSIDER initializing static fields inline rather than explicitly using static constructors, because the runtime is able to optimize the performance of types that don’t have an explicitly defined static constructor.

    "Consider" in this context means to do so unless there's a good reason not to. In the case of static initializer fields, a good reason would be if initialization is too complex to be coded inline.

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