How much code should one put in a constructor?

前端 未结 9 2211
一整个雨季
一整个雨季 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: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).

提交回复
热议问题