Simplify function arguments
Posted on 30 January 2007 (01:10 PM)
Sometimes functions ask for a lot of arguments (or parameters). Even when using functions you've written yourself, it's sometimes hard to keep track of the order in which the arguments are expected.
In this article I will explain a technique that makes argument-order redundant and might make your programming life easier.
Argue a lot
I'm going to take the build-in PHP function array_multisort as an example. If you take a look at the second example of this function in the PHP manual, you'll see it can expect a horrible lot of arguments:
<?php$ar = array(array("10", 11, 100, 100, "a"),array( 1, 2, "2", 3, 1));array_multisort($ar[0], SORT_ASC, SORT_STRING,$ar[1], SORT_NUMERIC, SORT_DESC);var_dump($ar);?>- Download this code: /code/simplify_function_arguments1.txt
I don't know about you, but I'm the kind of lazy programmer that's not going to learn that argument-order by heart. So every time I want to use array_multisort, I have to look at PHP.net.
This is no problem at all, PHP.net is a great manual, but when you have a lot of functions, or if you work object oriented, you keep looking up source files to figure out in which order your object expects its arguments.
Objectify!
Take a look at this example function for creating a yummy lunch:
function makeYummyLunch ( $cupsOfCoffee, $sugarlumps, $milk, $typeOfBagel, $eggs){/* secret recipe for making great lunch omitted */}- Download this code: /code/simplify_function_arguments2.txt
Personally I like my lunch with 2 cups of coffee, both with 2 lumps of sugar, no milk. Maybe I have a creamcheese bagel and 2 eggs. I would make my yummy lunch like this:
makeYummyLunch ( 2, 2, null, 'creamcheese', 2);- Download this code: /code/simplify_function_arguments3.txt
Without documentation, I could end up with creamcheese flavoured coffee, with 2 spoonfuls of milk and no sugar! The solution is creating an object, which properties represent all the arguments. In Javascript this is really easy:
var lunch = { }; // create object literallunch.cupsOfCoffee= 2;lunch.sugarlumps= 2;lunch.milk= null;lunch.typeOfBagel= 'creamcheese';lunch.eggs= 2;- Download this code: /code/simplify_function_arguments4.txt
You then modify the function to accept only the one object...
function makeYummyLunch ($lunch){/* secret recipe for making great lunch omitted */}- Download this code: /code/simplify_function_arguments5.txt
... and call it like this:
makeYummyLunch (lunch);- Download this code: /code/simplify_function_arguments6.txt
You can then access the arguments like lunch.eggs, lunch.cupsOfCoffee, et cetera.
In PHP you can't create objects that easily, but you can use associative arrays to get the same result:
$lunch = array ();$lunch['cupsOfCoffee']= 2;$lunch['sugarLumps']= 2;$lunch['milk']= null;$lunch['typeOfBagel']= 'creamcheese';$lunch['eggs']= 2;- Download this code: /code/simplify_function_arguments7.txt
Inside the body of the function, you can then access all arguments like $lunch['eggs'] and $lunch['cupsOfCoffee'].
If you simplify your API (Application Programming Interface) like this, you can code faster and more efficient.
Enjoy your lunch.
Filed under Javascript, PHP
- ← previous article: New Dutch accessibility law
- → next article: Why use semantic class names?
Comments:
And now you have to remember what the names of those darn properties of The One Object are, instead of the order of parameters to a function.
To be frank, not much of an improvement, IMHO.
With a proper naming convention this shouldn't be that much of a problem.
For instance when sending stuff to a function for database insertion; you probably know what the names of your fields are, so it's easier to send an associative array than to remember what field in the table comes first.
It makes my life easier, but in the end it's a matter of personal preference, naturally :)
shouldn't "lunch.cupsOfCoffee= 2" be "lunch->cupsOfCoffee= 2" and so on?
this comment has been quoted by Harmen Janssen
Nope, in Javascript, properties of an object are defined through the dot-syntax.
You're referring to the syntax used in PHP :)