Programmer Tips‎ > ‎

Parameter Order

posted Sep 4, 2013, 3:35 PM by Tyler Akins
When functions are created, they often start out as little baby functions that don't take parameters or only accept a couple.  Then they grow to include additional functionality or they need to pass additional data to other routines and their signature grows.  When this happens, the parameter order is often overlooked.

Let's take a function in JavaScript that might seem a bit weird.  Imagine it uses an AJAX service to retrieve a web page.  It might send additional data and take a timeout.

function getWebPage(method, uri, timeout, additionalData, ajaxService) { ... }

This might seem good, but there are a few problems.

Optional At The End

The above example might have seemed good because you felt the most important things are listed first.  You can't do a web request without a method or URI, right?  The rest probably just don't matter as much.  Unfortunately, they actually matter more.

It is a good practice to place optional parameters at the end.  That way you don't have function calls like this:

callMyFunction('some parameter', undefined, 'another needed parameter');

That's just ugly.  Plus the code to do automatic insertion of default values in the middle of a list of arguments often looks terrible.  Don't get me wrong; there are times when that makes sense, but consider those times carefully.  It's not a good habit.

Back to our getWebPage function.  Moving the optional additionalData parameter and the timeout value to the end gives us the following:

function getWebPage(method, uri, ajaxService, timeout, additionalData) { ... }

Order Optionals Parameters

Optional parameters should be arranged by the likelihood of them being used.  It is far more likely for a timeout to not be specified than additionalData.  Perhaps you would always want the browser's default timeout, but you do want to POST a lot of information.  Let's reorder those optional parameters one more time.

function getWebPage(method, uri, ajaxService, additionalData, timeout) { ... }

Order Mandatory Parameters

Mandatory parameters should be ordered by how likely they are to change from invocation to invocation.  Let's say that our ajaxService never changes for the life of our application.  We might only want to change it for tests.  With JavaScript we can bind the default AJAX service, reducing the number of arguments.  Move that ajaxService up front and here's an example of using bind.

var getWebPage;
function getWebPageOriginal(ajaxService, method, uri, additionalData, timeout) { ... }
getWebPage = getWebPageOriginal.bind(null, myDefaultAjaxService);
getWebPage.get = getWebPage.bind(null, 'GET'); = getWebPage.bind(null, 'GET');


If you pay attention to parameter order and keep it in a sane format, you will make things easier for other developers to use your functions.  There's a lot of other tips, like not having several parameters and using descriptive names for variables, but those are well discussed on other sites.