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:
- common arguments are left most.
- remaining arguments are based on importance
- 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!
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.