Is it a good practice to declare an array in arguments?

validationError([elem1,elem2],type,shiftNo);

or

var arr = [elem1,elem2];
validationError(arr,type,shiftNo);

What I want to ask is the approach of 1 calling a function that is considered bad (does it also have any consequences). and in this regard, this is a bad approach for declaring strings, objects, and functions inside arguments.

+4
source share
4 answers

Performance is not a problem, not a language like JS, Ruby or whatnot. Therefore, we can only think about code readability. And this case is not strongly related to JS, so my examples will be.

move = ["E2", "E4"];
if chessboard.valid(move, player) {
  ...
}

: " (E2 E4), ...", , . , , ( , ):

if chessboard.valid(["E2", "E4"], player) {
  ...
}

? valid ? , , ? , , , . , . , , chessboard:

if chessboard.valid_move(["E2", "E4"], player) {
  ...
}

, API, - .

, :

  • , .
  • , ( ), .
  • , 1 2.
+7

. Javascript, ( ). ( ) , .

+2

, , , .

@jAndy , , .

+2

arr ? , 2 - , , . ... - , , , .

, 2 - , 1. 2 , , , - , ( ). , , , , .

The only negatives that I see will generate an absolutely insignificant amount of overhead, and now you have 2 lines of code instead of 1. But I think that it does not matter, the tiny potential benefits of option 2 outweigh the tiny negatives of option 1.

+2
source

All Articles