简体   繁体   English

在参数中声明一个数组是不好的做法?

[英]Is it a bad practice to declare an Array in arguments?

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

or 要么

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

What I mean to ask is approach 1 of calling the function considered bad ( also does it have any performance ramifications). 我的意思是问题是调用被认为是坏的函数的方法1(它也有任何性能分支)。 and for that matter is it a bad approach to declare strings, object and functions inside arguments. 对于这个问题,在参数内声明字符串,对象和函数是一种糟糕的方法。

Performance is not an issue, not in a language like JS, Ruby or whatnot. 性能不是问题,不是像JS,Ruby或诸如此类的语言。 So all we can do is think about code readability. 所以我们所能做的就是考虑代码的可读性。 And this case is not strongly related to JS, so will be my examples. 这个案例与JS没有很强的关系,所以我的例子也是如此。

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

This clearly states: "if the move (E2 E4) is valid for this chessboard, then..." , you don't even need to look at the docs to know that. 这清楚地表明: “如果移动(E2 E4)对于这个棋盘有效,那么......” ,你甚至不需要查看文档就知道这一点。 If we write that without assigning our array a name, the result looks a little cryptic (still easy to guess, but harder for such a tiny example): 如果我们写这个而不给我们的数组赋一个名字,结果看起来有点神秘(仍然很容易猜到,但对于这么小的例子来说更难):

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

What is this supposed to mean? 这应该是什么意思? What does valid stand for here? valid是什么? Maybe, it's asking whether these cells contain valid player's pieces? 也许,它会问这些单元格是否包含有效的玩家作品? This is a sympthom of a design flaw, more precisely bad naming. 这是一个设计缺陷的症状,更确切地说是错误的命名。 It makes bold assumptions about how the chessboard code will be used. 它对如何使用棋盘代码做出大胆的假设。 We can make it obvious that this array represents a move by renaming the chessboard 's method: 我们可以通过重命名chessboard的方法来表明这个数组代表了一个移动:

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

This is better, but you may not have an API that allows your code to stay so readable without some additional naming. 这样做会更好,但是您可能没有一个API,允许您的代码在没有其他命名的情况下保持可读性。

So, I suggest a rule of thumb: 所以,我建议一个经验法则:

  1. If the array will be used more than once, name it. 如果阵列将被多次使用,请为其命名。
  2. If the meaning of the array is not obvious from where it goes (function name), name it. 如果数组的含义从它的位置(函数名称)不明显,请为其命名。
  3. Don't name it, unless points 1 or 2 apply. 除非第1或第2点适用,否则不要命名。

It doesn't make any difference really. 它确实没有任何区别。 Either way, you create a Javascript Array , which basically is an Object and get a reference in return (which you pass to your method). 无论哪种方式,您创建一个Javascript数组 ,它基本上是一个Object并获得一个返回的引用(您传递给您的方法)。 If you don't need to access that array (or other data) later in your code, the second approach is completely fine. 如果您不需要在代码中稍后访问该数组(或其他数据),则第二种方法完全没问题。

It is subjective, but in my opinion it is better to use the second approach . 这是主观的,但在我看来, 最好使用第二种方法

As @jAndy said there is no difference in the code execution neither the performance of your code, but it is easier to debug and easier to read and understand the second approach. 正如@jAndy所说,代码执行与代码的性能没有区别,但它更容易调试,更容易阅读和理解第二种方法。

Are the contents of arr ever going to get used again? arr的内容是否会再次被使用? If so, then option 2 is definitely the way to go. 如果是这样,那么选项2肯定是要走的路。 If not... something as simple as this is probably just personal opinion. 如果不是......这个简单的东西可能只是个人观点。

Personally, I'd have to say that option 2 is better practice , even though sometimes I'm guilty of using option 1. Option 2 is easier to read, it's easier to follow and it's less likely someone will have to re-read it because they became temporarily confused or lost in flow of thought whilst reading through your code (especially newer programmers). 就我个人而言,我不得不说选项2是更好的做法 ,即使有时我会使用选项1.选项2更容易阅读,更容易遵循,并且不太可能有人必须重新阅读它因为他们在阅读你的代码(特别是新的程序员)时会在思想流中暂时混淆或迷失。 For those reasons it's easier to maintain, you, and potentially future developers working with your code, will likely save time working with it. 由于这些原因,维护起来比较容易,您和未来可能使用代码的开发人员可能会节省使用它的时间。

The only negatives I can see would be generating an absolutely miniscule amount of overhead, and now you have 2 lines of code instead of 1. But I think that's irrelevant, the tiny potential benefits of option 2 outweigh the tiny negatives of option 1. 我能看到的唯一消极因素是产生绝对微不足道的开销,现在你有2行代码而不是1.但我认为这是无关紧要的,选项2的微小潜在好处超过了选项1的微小负面影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM