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

后端 未结 6 2194
自闭症患者
自闭症患者 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:29

    It depends.

    It is not bad for a constructor to call a private method, provided that the private method doesn't call other methods that could be overridden. However if the method or one of the methods that it calls could be overridden, you can run into trouble. Specifically the override method will be called before the overriding classes constructor runs and will see instance fields before they have been initialized.

    A second problem if that the some of the methods in your initalizeCanvas method look like they my "publish" the current object before it has been fully initialized. This is potentially problematic if the application is multi-threaded, and could result in other threads seeing stale field values.

提交回复
热议问题