2

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?

3
  • 1
    How many of those parameters do you usually or in average need? When it isn't information you need by the time of object construction, you should rather make separate calls for those properties you really need. Commented Nov 11, 2010 at 8:31
  • well, i only actually need the first. the remaining parameters basically stylise the object. when i first started the class i though it would be a good idea to allow the user of the class to construct with all the different styles settings, but now i'm thinking that's no longer ideal since the signature is so long. Commented Nov 11, 2010 at 9:47
  • It's a good question, I find having multi-levels of inheritance has a way of increasing the arguments supplied to the constructor. I hate it but there is no obvious solution to me. I was hoping someone might have listed a design pattern to help. Commented Nov 11, 2010 at 12:56

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

7 Comments

that's a smart idea to supply the constructor with an optional object of optional parameters. thanks!
Be warned, it can be really easy to misspell names. It will still compile without error, just not function like you expect. You will have to NULL check before setting the values, which is extra work. This is what parameters are for. "which parameter means what?" view the method signature, most editors show you this as you type. By accepting an anonymous object a user wont know what arguments the method expects, an editor wont show you that. It sounds to me like you're trying to fix a problem with your editor by fudging your code. File a bug report with the company that makes your editor.
Also, if you SWC this class then the method signature will be all anyone has to work with, which won't tell them anything about what the method expects. Pretty much rendering the parameter useless. Unless you document it, but then who wants to look up the documentation to find out something as basic as a list of accepted paramaters? Especially since we already have a way of specifying that built right in to the language. @Les File a grievance with Adobe over not labeling parameters.
@Joony - all strong points. i'm using Flash CS5. i was actually just trying to build an argument error check for the object of parameters and realized it's not possible. could probably do something with Dictionary, but that would add unnecessary overhead and take just as long to code as setting the parameters after construction. being able to pass setters with a ...rest would be great, but not possible. perhaps the best option is to just not accept properties during instantiation. it will be distributed as a documented .swc, but i think more developers have laptops than 27" desktops.
@Joony i'm not disagreeing with you, but I think there are also some advantages. If you are looking at your code at a later time, or someone else is looking at your code, you won't see the "method signature". Also checking for null values is extra work, but you do this once and saves you time every time you use the class. Overloading the constructor is probably the cleanest solution, but I forgot if AS3 supports this .. ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.