What is a Parameters Object
It is a simple object with assigned properties that can be used as the single argument of a function.
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:
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):
myData.name = "Pippo";
myData.height = 180;
myData.weight = 70;
myData.age = 35;
myData.sign = "Leo";
Condensed version is:
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
- 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)
- 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.
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...