Question

i've designed a custom class (or component?) that extends Sprite whose constructor has 15 parameters. only the first parameter is required, while the remaining 14 have default values assigned. all are necessary.

each of the parameters, except for the first required parameter are actually property setters. the class also contains public setter and getter functions, allowing for property changes at runtime after construction.

i've written the class on my desktop (27" screen) and realized i may have a problem when i was using the class on my 13" laptop - the code hinting was extending past the edges of the screen as it was too long.

is it normal/best practice to include optional setters as parameters in the constructor when setter functions are available, or should setters always be separate from the constructor?

Was it helpful?

Solution

In my opinion it comes down to ease of use at the class instantiating part of the code. If you have 14 parameters that you either all set or all skip then it's probably the best solution.

var defaultRectangle:Rectangle = new Rectangle(); //square of 1 x 1)    
var customRectangle:Rectangle = new Rectangle(0,0,  2,0  2,2,  0,2);

But if some of the 14 parameters are optional, it becomes a bit hard to read, then I think either the use of separate getter/setters is more readable, or a paramater object (mimicking named parameters)

//which parameter means what?
var girlfriend:Girl = new Girl("Blue", 0, 0, "", "", 1.8, 0, "", 140); 

//named parameters 
var girlfriend:Girl = new Girl({eyeColor: "Blue", height:1.8, iq:140});

//setters
var girlfriend:Girl = new Girl();
girlfriend.eyeColor = "Blue";
girlfriend.height = 1.8;
girlfriend.iq = 140;

I personally try to use Models as much as possible when working with "Views" So if your object is a Sprite and thus a View. Maybe the best approach could be:

var friend:Person = new Person({name: "Ford Prefect"});
var profileView:ProfileView = new ProfileView(friend);
addChild(profileView);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top