Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What conventions exist for ordering arguments in methods?

A colleague and I are discussing best practices regarding ordering method parameters. The goal is to establish a standard in our organization to improve readability and productivity by giving our methods common signatures. We are merely establishing guidelines for the recent grads we are hiring.

Example (userId is always passed in to audit the calls):

GetOrders(string userId, int customerId); GetOrders(string userId, int[] orderIds); GetCustomer(string userId, int customerId);

My argument is the following:

  1. common arguments are left most.
  2. remaining arguments are based on importance
  3. optional (nullable) arguments last.

His argument is essentially the opposite.

I'm not asking for a right or wrong answer here, nor a discussion. I just want to see what standards exist already.

Thanks!

like image 778
Mike Becatti Avatar asked Oct 16 '08 14:10

Mike Becatti


2 Answers

I'd go with the ordering of input,output,optional.

Optional should go at the end to me because most languages allow you to specify a default value for optional arguments to avoid having to include them. The provision of that is that they have to be the last argument(s) otherwise you can't drop them.

That's assuming you can't have named arguments though. If you can have them, I'd always suggest using them for clarity and order becomes a moot point.

like image 177
workmad3 Avatar answered Oct 06 '22 21:10

workmad3


I try to make all methods that use similar parameters use them in the same order.

For the choice for a single method, I go by importance. Optional items last.

like image 34
BlackWasp Avatar answered Oct 06 '22 19:10

BlackWasp