How much code should one put in a constructor?

前端 未结 9 2183
一整个雨季
一整个雨季 2020-12-15 05:34

I was thinking how much code one should put in constructors in Java? I mean, very often you make helper methods, which you invoke in a constructor, but sometimes there are s

相关标签:
9条回答
  • 2020-12-15 06:18

    Constructor is like an Application Setup Wizard where you do only configuration. If the Instance is ready to take any (possible) Action on itself then Constructor doing well.

    0 讨论(0)
  • 2020-12-15 06:21

    As Knuth said, "Premature optimization is the root of all evil."

    How much should you put in the consructor? Everything you need to. This is the "eager" approach. When--and only when--performance becomes an issue do you consider optimizing it (to the "lazy" or "over-eager" approaches).

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

    Take a look at this SO question. Even though the other one is for C++, the concepts are still very similar.

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