Parameters Obejct vs. Parameters

When building a Class or a function, should I use the traditional list of comma-separated arguments? or a single object containing all needed properties? In this post I explain the differences between a list and an object, and why I often prefer the second approach.

What is a Parameters Object

It is a simple object with assigned properties that can be used as the single argument of a function.

myFunction(object);

The traditional way...

Lets imagine I have to set my personal data, the order is: name, age, weight, height, blood type, zodiac sign. I can omit the parameters I don't know.
Lets set my data with the traditional approach, using a list of parameters:

setData("Pippo", 35, 70, 180, undefined, "Leo");

It's ok, I just set my personal data. But when I update it, I have to remember the exact sequence of parameters. And furthermore, if I only want to put name and zodiac sign, I have to fill the function with a long list of undefined.
In this example, updating could be easy because is quite easy to guess that 35 cannot be my height nor my weight, but in real life, with pixels, positioning, prercents and booleans, could be really confusing.

The Parameters Object

The same function, assigned with parameters object (verbose):

var myData = new Object();
myData.name = "Pippo";
myData.height = 180;
myData.weight = 70;
myData.age = 35;
myData.sign = "Leo";
setData(myData);

Condensed version is:

setData({name:"Pippo",age:35,height:180,weight:70,sign:"Leo"});

Besides the elegance of the code, properties are clearly shown and much easier to update in the future.
Blood type is not specified, and I do not need to set an "undefined" in order to catch the next slot in the parameters list.
Furthermore, I can store Parameters Objects and use them later modifying only one parameter, or store a list of defaults...

Cons and pros of Parameters Objects

Pros

  • Properties are easier to recognize because they have names
  • Undefined properties do not need to be specified
  • Objects can be stored and reused
  • Collections of pre-defined objects or defaults can be stored
  • Programming a Class, it's simpler to manage a sigle parameters object than a list of parameters (with a single for loop all parameters can become class properties)

Cons

  • Properties cannot be casted to a data type
  • When compiling, cannot check for errors on wrong data type
  • When setting objects (not on update) remembering names of parameters can be as tedious as remembering order

When it's better to use Parameters Objects

I experienced an improvement in classes initialization which require more than 2 or 3 parameters. Also in feedback and events called in listeners, instead of remembering at which position is the parameter I need to retrieve, I can easily get an object, and loop though it's properties. Obviously, the improvement is in user-defined parameters, and not in parameters passing between functions internal to a class.

When is better to use traditional parameters lists

Parameters lists are more convenient when building private internal methods in classes, since once they are finalized usually there is no update involved. Funrthermore, parameters can be assigned to a data type (String, Number, Boolean, etc.) and creating a class this kind of compile checks are vital.

Conclusion

There is not a rule or check list to get to a final decision. The choice between these 2 approaches must be taken according to the single task, and of course on your personal taste and coding style...

After I started to use parameters objects and got used to it, I created my own naming standard and standard functions to set the parameters as instance properties, setting defaults and checking for datatypes, etc. I would never go back to lists again :)

I noticed that others are starting to have this approach too, and if you have some suggestions or standards please add your comments and enlight me with your ideas...

namaste
Pippo

 Leave a comment / Read Comments

Flash, ActionScript, OOP, object oriented programming, freelance flash developer, actionscript developer, flash programmer, flasher, rich internet applications developer, ria, flash games, flash 3D, web applications, ActionScript 2.0, free actionscript classes, swf, free flash downloads, free flash components, flash tutorials, actionscript tutorials, flash developer beijing, programmatore flash, sviluppatore flash, sviluppatore flash italia, sviluppatore flash roma, sviluppatore flash italiano, freelance flash developer, Flash, ActionScript, actionscript, OOP, object oriented programming, freelance, actionscripter, programmer, freelance flasher, actionscript developer, rich internet applications, ria, flash games, 3D, web applications, interface developer, ActionScript 2.0, classes, swf, useability, usability, free stuff, free code, free classes, Flash Components, Flash Developer, Flash Games developer, Flash MX 2004, Flash 8, Flash Developer in Italy, Italian Flash developer, Flash Sviluppatore roma, Flash Developer Beijing, flash developer china, flash Developer Bangkok