0

I saw this question: Multiple arguments vs. options object and the consensus seemed to be the object argument list was a good design pattern for long argument lists or lists including optional args.

I was wondering what the advantages/disadvantages of using it for functions with only 2 or 3 argument were, i.e., why would you ever use a traditional argument list?

function normalArgs(arg1, arg2, arg3) {
   // order of args in function call must be correct
}


function objectArgs(args) {
   // use args.arg1, args.arg2, args.arg3
   // significantly reduces errors involving out of order/missing arguments
}

Is the creation of the object literal significant overhead or something?

Community
  • 1
  • 1
alh
  • 2,569
  • 6
  • 29
  • 42

2 Answers2

1

As of my experience, It was always safe to have argument object when our method is exposed to out side ( as a service or API to outside party or another module which are reusing your APIs). So you will get the benefit of changing the arguments without changing the API signature, which save our time by not rebuilding the stubs for API and etc.

However , parsing each and every every arguments in method is OK when you are not in above mentioned condition.

jayalalk
  • 2,382
  • 3
  • 17
  • 14
0

The top-voted answer in the question you referenced explained it very well. It's always a matter of readability and maintainability.

Extremely explicit code, such as always passing lists of named arguments - while always completely clear - can also detract from readability in simpler situations.

It's a matter of judgement. Always think about the next guy to read it - will he be scratching his head? (And remember, the 'next guy' may be you, weeks, months, or years from now.)

John Visosky
  • 237
  • 1
  • 1