Why chaining constructors is bad.
来源:互联网 发布:图书开卷数据 编辑:程序博客网 时间:2024/04/28 03:50
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.
TrackBack URL for this entry:
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.
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.
- Why chaining constructors is bad.
- Why LD_LIBRARY_PATH is bad
- Why cacheAsBitmap is bad!
- Why LD_LIBRARY_PATH is bad
- Why singleton is bad?
- Why Is Chinese Web Design So Bad?
- Why Is Chinese Web Design So Bad?
- Why catch(Exception)/empty catch is bad
- What is a magic number? And why is it bad?
- 为什么有时候 LD_LIBRARY_PATH 不起作用(Why LD_LIBRARY_PATH is bad)
- Why writing files from the kernel is bad ?
- Why don't we have virtual constructors?
- Why 使用TLS记录协议封装IP层VPN IS A Bad Idea
- Why SHRINKFILE is a very bad thing, and what to do about it.
- Why Domain Sharding is Bad News for Mobile Performance and Users
- Why we declare the constructors as protected or even private?
- Constructors
- Constructors
- 刚才写用户注册程序时碰到的怪问题
- VC中的一些常用方法(20条)
- 解析C语言中的sizeof
- 一个操作串口的类.
- 激光打印机典型故障维修
- Why chaining constructors is bad.
- 新浪短信Web Service 简介
- 精子的30种死法
- Starcraft 的心情
- LOMO效果的PS Action
- 函数指针
- 思MM!!!
- 什么是Blog ?
- 还是那片天--在巴音布鲁克