在 PHP 中你使用复数还是单数来命名你的数组?

发布于 2024-07-11 15:39:21 字数 1431 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

梦萦几度 2024-07-18 15:39:21

我用复数形式。 然后我可以做类似的事情:

$name = $names[1];

I use the plural form. Then I can do something like:

$name = $names[1];
荒路情人 2024-07-18 15:39:21

名称应始终传达尽可能多的信息,以防读者不熟悉类型声明。 因此,数组或集合应该以复数形式命名。

我个人认为 $name[1] 具有误导性,因为它的意思是“名称的第一个元素”,这在英语中没有意义。

Name should always convey as much information as possible in case a reader is not familiar with the type declaration. An array or collection should therefore be named in the plural.

I personally find $name[1] to be misleading, since it means "the 1st element of name" which doesn't make English sense.

ヅ她的身影、若隐若现 2024-07-18 15:39:21

我通常在最后给它一些东西,比如list,所以它会是

nameList

否则,我把它变成复数。

I usually give it something on the end like list so it would be

nameList

Otherwise, I make it plural.

情徒 2024-07-18 15:39:21

复数。

sort(name)
sort(names)

显然,这里只有复数才有意义。

然后,在这里:

name[1]
names[1]

在这种情况下两者都是有意义的。

因此,在引用整个集合引用集合中的一项时,复数是唯一有意义的。

Plural.

sort(name)
sort(names)

Clearly, only plural makes sense here.

And then, here:

name[1]
names[1]

Both would make sense in this context.

Therefore, plural is the only one that makes sense when referencing the whole collection and when referencing one item from the collection.

违心° 2024-07-18 15:39:21

我总是会选择

appleList 
appleArray
nameAppleDict

正确的命名约定,这将为其他人阅读代码节省大量时间。 因为他们不必返回并检查变量类型来理解它。

有一个像这样的变量名:

apples 

有时可能会令人困惑(列表、数组还是集合?)

I would always go for

appleList 
appleArray
nameAppleDict

By having the naming convention done right, it will save a ton of time for someone else to read the code. Since they don't have to go back and check the variable type to understand it.

Having a variable name like:

apples 

could be confusing sometimes (list, array or set?)

我一直都在从未离去 2024-07-18 15:39:21

对我来说是复数。

由于上面给出的所有原因,并且因为我工作的约定约定(我贡献创建的)需要数组/列表/向量等的复数形式。

尽管复数命名在某些情况下可能会导致一些异常,但大多数情况是它提供了增强的清晰度并且代码更容易扫描阅读,而不会产生令人烦恼的感觉,即当您返回以将大脑从任何绊倒的东西中解脱出来时,您的大脑会抓住一个奇怪的结构并中断流程。

Plural for me.

For all the reasons given above and because the agreed conventions where I work (that I contributed to creating) require plurals for arrays / lists / vectors etc.

Although plural naming can cause some anomalies in some cases the majority case is that it provides enhanced clarity and code that is easier to scan read without that annoying feeling of your mind catching on a strange construction and interrupting the flow while you go back to unsnag your brain from whatever tripped it up.

街角卖回忆 2024-07-18 15:39:21

复数虽然在学校教你做单数,所以你可以说:

value[0] = 42;

更有意义

values[0] = 42

事实上,如果你仔细想想,这确实比:如果你不相信我,大声说出来 。 不管怎样,我确实使用复数,这样我就可以很容易地分辨出我何时扫描代码。 这似乎也是人们现在使用的标准。

Plural although the teach you to do it singular in school so you can say:

value[0] = 42;

and really if you think about it that does make more sense than:

values[0] = 42

say it out loud if you don't believe me. Regardless I do use plurals so that I can easily tell when I am scanning through code. That also seems to be the standard that people are using these days.

隱形的亼 2024-07-18 15:39:21

其他人说的是:复数。

在 PHP 中更明目张胆:

$name = 'Bobby';
echo $name[1];

会显示 o。 :-)

我必须承认几年前我问过自己同样的问题,但是在访问一个成员时,显示数组或集合的复数性质比英语含义更重要......

What the others said: plural.

It is even more flagrant in PHP:

$name = 'Bobby';
echo $name[1];

will display o. :-)

I must admit I asked myself the same question some years ago, but showing the plural nature of the array or collection was more important than English meaning when accessing one member...

悍妇囚夫 2024-07-18 15:39:21

总是复数。 对于可以容纳多个元素的任何其他数据类型的列表也是如此。

Always plural. Same for lists of any other datatype that can hold more than one element.

月下凄凉 2024-07-18 15:39:21

总是复数。 这样我做的时候就不会有任何混乱...

for each (string person in people)
{
    //code
}

Always plural. That way there isn't any confusion when I do...

for each (string person in people)
{
    //code
}
黯淡〆 2024-07-18 15:39:21

我通常使用复数形式,或者有时与这里引用的方式相同,将 List 添加到名称中......

I normally use the plural form, or sometimes the same way as cited up here, adding List to the name...

木格 2024-07-18 15:39:21

我使用很多不同的语言工作,没有考虑的一件事是具有不止数组的语言。 即人:人; 人:字典。 people 不一定是数组,它可能是另一种类型并导致错误。 此外,在某些语言中,不同的类型在不同的操作中会表现得更好,或者可能有不同的可用方法。

这就是为什么现在在所有语言中,我都会用名词单数来命名,后跟类型,例如 personArray 或 person_arr(如果您愿意)。 如果相关的话,我通常还会在开头包含范围界定。 变量名称应该足够明确,以便您不需要自动完成或 ctrl+f 来知道它是什么。

I work in a lot of different languages, one thing that isn't considered is languages which have more than array. i.e person:Person; people:Dictionary. people is not necessarily an array, it could be of another type and cause an error. Also, in some languages the different types will perform better at different operations or possibly have different methods available to them.

Which is why these days in all languages I make the names with the noun singular followed by the type such as personArray or person_arr if you prefer. I generally also include the scoping at the beginning if relevant. Variable names should be explicit enough that you don't need auto complete or ctrl+f to know what it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文