Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad practice to access the arguments object to every function that takes parameters?

Tags:

javascript

Let's say I have 300 functions in one script. Is it bad to access the arguments object within each function as opposed to passing in named parameters?

Edit: Okay a lot of you are asking why I would want to do this, it's really a theoretical question but since you asked is there any reason why i would want to do this, other than variable parameters?

like image 1000
Mr. Smee Avatar asked Nov 08 '12 23:11

Mr. Smee


People also ask

Can we use object as a function argument?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

Do functions take parameters or arguments?

Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.

What happens if a caller passes too many parameters to a function?

It depends on the language. In C++, passing too few or too many arguments will result to an error. Unlike in JavaScript, passing few arguments will make other arguments without value undefined. Passing many arguments will have no effect to the function, as long as the parameters are filled in.

Do you pass parameters or arguments?

Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address. This rule applies to all scalar values, structures, and unions passed as arguments. Modifying a parameter does not modify the corresponding argument passed by the function call.


1 Answers

It is bad practice to write code that is hard to read and hard for others to understand, maintain or use.

It is good practice to specify named arguments and give them meaningful, useful, descriptive names whenever you can.

Also, you should remember that you can declare named arguments even if all are not required. You can then either test arguments.length to see how many arguments were passed or test the values of the specific named arguments to see if they are undefined or not.

In cases of highly variable arguments, you can also pass an object and let the object self-describe what arguments were passed.

There are specific cases when the arguments object is useful and when using it is the best way to write clear, concise and safe code, but I've never seen a case where all functions declare no named arguments and use only the arguments object. Unless you have some very unusual project, this would generally not be the best way to code for so many functions.

For performance, it appears that accessing named arguments is also faster than accessing the arguments object in all the major browsers. In this jsperf performance test that just sums the first three arguments passed to a test function, using the named arguments was 2-8x faster than using the arguments object. The exact performance difference likely depends upon exactly what the function is attempting to do, but the arguments object definitely looks slower.

If you provide more information on the reason why you want to use the arguments object instead of named arguments or why you think it would be beneficial to use the arguments object, then we might be able to offer more concrete advice.

like image 81
jfriend00 Avatar answered Oct 20 '22 07:10

jfriend00