Why chaining constructors is bad
I personally find that this code snippet demonstrating constructor chaining is hard to read.
This code makes it very hard to identify which of the many constructors is the "real" one: the constructor that does the real job and that I should call if I add a new overloaded constructor.
This is why I use the following two rules when I write overloaded constructors:
- Have all your constructors converge toward one unique private method that you will name consistently throughout your code base (e.g. init) and that will contain the entirety of all the parameters that your constructors may receive.
- Don't add any logic to the constructors, all they should do is invoke init with the right parameters and null for default ones.
Here is an example, from worst:
public class Person {
private String m_firstName = null;
private String m_lastName = null;
private long m_ssn = 0;
public Person(String lastName) {
m_firstName = null;
m_lastName = lastName;
m_ssn = getSsn();
}
public Person(String firstName, String lastName) {
m_firstName = firstName;
m_lastName = lastName;
}
... to better:
public Person(String lastName) {
this(null, lastName);
m_ssn = getSsn();
}
public Person(String firstName, String lastName) {
m_firstName = firstName;
m_lastName = lastName;
}
... to best:
public Person(String lastName) {
init(null, lastName);
}
public Person(String firstName, String lastName) {
init(firstName, lastName);
}
private void init(String firstName, String lastName) {
m_firstName = null;
m_lastName = lastName;
if (null == m_firstName) {
m_ssn = getSsn();
}
}
Here is why the latter form is preferable:
- It doesn't duplicate any code.
- It makes the initialization rules clear : "if no firstName was given, then ssn gets initialized".
- The signature of init gives a good indication of the various parameters this class needs to be initialized, while a multitude of overloaded constructors obscures this fact.
http://beust.com/weblog/mt-tb.cgi/205
Listed below are links to weblogs that reference 'Why chaining constructors is bad' from Otaku, Cedric's weblog.
博客指出构造函数链式调用的代码难以阅读,难以确定真正起作用的构造函数。作者提出编写重载构造函数的两条规则:让所有构造函数收敛到一个统一命名的私有方法;构造函数不添加逻辑,仅调用该方法并传入参数。还举例说明了规则的优势。
2889

被折叠的 条评论
为什么被折叠?



