Is it violation of Clean Code to call init method in constructor like this

后端 未结 6 2181
自闭症患者
自闭症患者 2020-12-14 21:05

My concern in the code below is that the param to constructor is not actually directly mapped to the class\'s instance fields. The instance fields derive value from the para

6条回答
  •  情话喂你
    2020-12-14 21:44

    It is generally a bad idea for a constructor to contain non-trivial code. As a rule, constructors should at most assign supplied values to fields. If an object requires complex initialization, that initialization should be the responsibility of another class (typically a factory). See Miško Hevery's great write-up on this topic: Flaw: Constructor does Real Work.

提交回复
热议问题