Clearer adobe actionscript AS3 code by using objects as function parameters

Classical implementation of a Adobe flash actionscript function could be something like this:

function doSquareThing(x,y,width,height,fillcolor,bordercolor,callback){
      //Do something with the parameters
}

doSquareThing(10,20,15,15,#cccccc,#000000,squareReady);

Everytime you call the function you have to remember what order the parameters should be. If somebody else is reading the code he/she can’t know what the parameters are without seeing documentation or function definition.

Better way to do the same thing is to use a object as a function parameter. This way you will get much more human readable code. something like this:

function doSquareThing(settings){
      // do something with the parameters: settings.x settings.callback etc.
}
doSquareThing({
   x:10,
   y:20,
   width:15,
   height:15,
   fillcolor:#cccccc,
   bordercolor:#000000,
   callback:squareReady
});

it takes a few more lines, but instantly you know what parameters function call is passing and they can also be in any order.

Leave a Reply