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:

  1. <?php
  2. $ar = array(
  3. array("10", 11, 100, 100, "a"),
  4. array( 1, 2, "2", 3, 1)
  5. );
  6. array_multisort($ar[0], SORT_ASC, SORT_STRING,
  7. $ar[1], SORT_NUMERIC, SORT_DESC);
  8. var_dump($ar);
  9. ?>
  10. 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:

  1. function makeYummyLunch ( $cupsOfCoffee, $sugarlumps, $milk, $typeOfBagel, $eggs)
  2. {
  3. /* secret recipe for making great lunch omitted */
  4. }
  5. 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:

  1. makeYummyLunch ( 2, 2, null, 'creamcheese', 2);
  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:

  1. var lunch = { }; // create object literal
  2. lunch.cupsOfCoffee= 2;
  3. lunch.sugarlumps= 2;
  4. lunch.milk= null;
  5. lunch.typeOfBagel= 'creamcheese';
  6. lunch.eggs= 2;
  7. Download this code: /code/simplify_function_arguments4.txt

You then modify the function to accept only the one object...

  1. function makeYummyLunch ($lunch)
  2. {
  3. /* secret recipe for making great lunch omitted */
  4. }
  5. Download this code: /code/simplify_function_arguments5.txt

... and call it like this:

  1. makeYummyLunch (lunch);
  2. 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:

  1. $lunch = array ();
  2. $lunch['cupsOfCoffee']= 2;
  3. $lunch['sugarLumps']= 2;
  4. $lunch['milk']= null;
  5. $lunch['typeOfBagel']= 'creamcheese';
  6. $lunch['eggs']= 2;
  7. 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.

Back to top

Filed under Javascript, PHP

Comments:

  1. 02 February 2007 (04:01 PM) by Alex Kraskramp

    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.

  2. 02 February 2007 (04:07 PM) by Harmen Janssen

    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 :)

  3. 20 July 2008 (05:46 PM) by Jez

    shouldn't "lunch.cupsOfCoffee= 2" be "lunch->cupsOfCoffee= 2" and so on?

    this comment has been quoted by Harmen Janssen

  4. 21 July 2008 (09:12 AM) by Harmen Janssen

    Jez wrote:

    shouldn't "lunch.cupsOfCoffee= 2" be "lunch->cupsOfCoffee= 2" and so on?

    Nope, in Javascript, properties of an object are defined through the dot-syntax.

    You're referring to the syntax used in PHP :)

Leave a comment

RE: Simplify function arguments

Note to spammers: rel="nofollow" will be added to links. If I consider your comment spam, your IP-address might get blocked.

HTML not allowed. URLS will be auto-linked. Maximum length is 1250 characters.

I understand this is inconvenient, but the spam I'm receiving on this website is driving me nuts. Please forgive me and cope with it until I find a better solution.

Mandatory fields are marked by an asterisk (*).

Increase textarea-size Decrease textarea-size

Back to top

Preferences

These settings will be saved for your convenience.