Design Opinions: How many options should I put in the constructor?

Personally, I like offering users of my classes the option of putting as much in the constructor as possible.

For example, I would rather use (and allow others to use) the second statement for initializing a textField, instead of the first way:

//Too much code
var tf:TextField = new TextField();
tf.text = "Hello World!";
tf.autoSize = TextFieldAutoSize.LEFT;
tf.textFormat = format1;

//Just right
var this.addChild(new TextField("Hello World!", format1, TextFieldAutoSize.LEFT));

However, after a while, constructors start getting REALLY long, and even though many properties inside of the constructor have default values, perhaps you want to manually set a value half way inside the constructor, but want to leave everything else default, you still have to include all those previous options. This can get annoying.

//Note, replace "<default>" with the default values for those fields
var this.addChild(new TextField("Hello World!", <default>, <default>, <default>, <default>, 0xFF0000));

This can be solved by allowing users to pass in dynamic objects containing which properties they want to set beyond the default value:

var this.addChild(new TextField({text:"Hello World!", textColor:0xFF0000}));

The MAJOR disadvantage to this is then there is no built in type checking, and you have to manually throw runtime type errors without the IDE or debugger automatically checking that the correct types were passed in.


Questions for discussion:

  1. Why do most of Adobe’s components (and many classes, unless they depend on a value in order to work) have no additional options in the constructor?
  2. Is it a bad idea (design wise) allowing users to pass in a large amount of ‘non-critical’ properties already in the constructor?
  3. Are there any more positive/negative aspects of either allowing or requiring data in the constructor?