最佳实践:如何构建阵列 - 标准和命名约定 [英] Best Practice: How to Structure Arrays - Standards and Naming Conventions

查看:95
本文介绍了最佳实践:如何构建阵列 - 标准和命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是多维数组结构的最佳实践是什么元素持有迭代器VS细节元素方面?

What is the best practice in multidimensional array structure in terms of what elements hold the iterator vs the detail elements?

我的大部分编程经验(我主要是为了好玩)来自于谷歌以下教程,所以我提前道歉,如果这似乎是一个非常愚蠢的问题 - 但我想开始提高我的$ C $角

The majority of my programming experience (and I do mainly do it for fun) comes from following tutorials on google, so I apologize in advance if this seems an exceptionally daft question - but I do want to start improving my code.

每当我有需要作出一个多维数组,我的命名一直放在柜台的第一个元素。

Whenever I have needed to make a multidimensional array, my naming has always placed the counter in the first element.

例如,如果我有一个维数组如下:

For example, if I have a single dimensional array as follows:

$myArray['year']=2012;
$myArray['month']='July';
$myArray['measure']=3;
// and so on.

不过,如果我想使同一阵列保持历史我想补充另一个维度和格式化的少数业主如下:

However, if I wanted to make that same array keep a few owners of history I would add another dimension and format it as follows:

$myArray[$owner]['year']=2012;
$myArray[$owner]['month']='July';
$myArray[$owner]['measure']=3;

编辑:要确保我的例子不是倒胃口或领导在正确的方向,我基本上按照这个结构:

To make sure that my example isn't off-putting or leading in the right direction, I am basically following this structure:

$myArray[rowOfData][columnOfData]

现在,我的问题是有关接受约定。我应该改为执行以下操作?

Now, my question is about accepted convention. Should I instead be doing the following?

$myArray['year'][$owner]=2012;
$myArray['month'][$owner]='July';
$myArray['measure'][$owner]=3;

编辑:使用从上面的编辑,它应该是:

using that edit from above, should it be:

$myArray[columnOfData][rowOfData]

我已搜查有关数组命名习惯,只是不停的按文章争论是否命名数组作为复数与否。我一直在给它们命名方式似乎更合乎逻辑,我认为它遵循类似于一个结构的对象更好地即对象 - > secondaryLevel->详细而是所有我知道,我一直在做它的屁股,这一切的时间。当我越来越成节目,我会preFER改变我的习惯,如果他们错了。

I have searched about array naming conventions, but keep hitting articles arguing about whether to name arrays as plurals or not. The way I have been naming them seems to be more logical and I think it follows a structure that resembles an object better i.e. object->secondaryLevel->detail but for all I know I have been doing it ass-about all this time. As I getting more and more into programming, I would prefer to change my habits if they are wrong.

有没有一个公认的标准,或只是任何事情都会发生与数组?如果你在看别人写的code,什么格式会期待?我得到任何有意义的结构/是直观的被接受。

Is there an accepted standard or is it just anything goes with arrays? If you were looking at code written by someone else, what format would be expecting? I get that any structure that makes sense/is intuitive is accepted.

也从一个角度迭代点,它下面的一个更为直观:

Also from an iteration point of view, which one of the following is more intuitive?:

for($i=0;$i<$someNumber;$i++)
{
    echo $myArray[$i]['year'];
    // OR
    echo $myArray['year'][$owner];
}

编辑:我确实有这个帖子标记为C#和Java,因为我想要得到的只是PHP程序员之外的一些意见。我认为作为数组在这么多不同的语言中使用,这本来是很好的得到一些程序员在输入各种汉语语言。

I did have this post tagged as c# and Java because I wanted to get some opinions outside of just PHP programmers. I think that as arrays are used in so many different languages, it would have been good to get some input from programmers in various langauges.

推荐答案

您的问题是主观的,在每个人都可以有不同的方法来你所说的情况,你是明智的,即使问的问题;如何以最佳方式命名变量,类等等。不幸的是,我花了比我愿意承认确定最佳的变量名有意义和满足要求更多的时间。我的最终目标是编写code这是'自我记录。通过编写自文档code,你会发现它是非常容易的添加功能或修复的缺陷,因为它们产生。

Your question is subjective, in that everyone may have a different approach to the situation you stated, and you are wise to even ask the question; How best to name your variables, classes, etc. Sad to say, I spend more time than I care to admit determining the best variable names that make sense and satisfy the requirements. My ultimate goal is to write code which is 'self documenting'. By writing self-documenting code you will find that it is much easier to add features or fix defects as they arise.

在我来发现,这些做法的工作最适合我的岁月:

Over the years I have come to find that these practices work best for me:

阵列:始终复数

我这样做,所以环控制结构使更多的语义感,更容易的工作。

I do this so loop control structures make more semantic sense, and are easier to work with.

// With a plural array it's easy to access a single element
foreach ($students as $student) {}

// Makes more sense semantically
do {} (while (count($students) > 0);

对象的阵列>深多维数组

在你的榜样,你的阵列开始吹起来是3元深多维数组,和正确的,因为罗比的code段是,它表明了它需要遍历多维数组的复杂性。相反,我建议创建对象,其可以被添加到阵列。请注意,以下code为示范而已,我总是用访问。

In your example your arrays started blowing up to be 3 element deep multi-dimensional arrays, and as correct as Robbie's code snippet is, it demonstrates the complexity it takes to iterate over multi-dimensional arrays. Instead, I would suggest creating objects, which can be added to an array. Note that the following code is demonstrative only, I always use accessors.

class Owner
{
    public $year;
    public $measure;
    public $month;
}

// Demonstrative hydration 
for ($i = 1 ; $i <= 3 ; $i++) {

    $owner = new Owner();

    $owner->year = 2012;
    $owner->measure = $i;
    $owner->month = rand(1,12);

    $owners[] = $owner;
}

现在,你只需要遍历一个平面数组,以获得您所需要的数据:

Now, you only need to iterate over a flat array to gain access to the data you need:

foreach ($owners as $owner) {
    var_dump(sprintf('%d.%d: %d', $owner->month, $owner->year, $owner->measure));
}

这个数组对象方法的最酷的是它会多么容易添加增强,如果你想添加的所有者名称是什么?没问题,只需将成员变量添加到您的类,并修改您的水化位:

The cool thing about this array of objects approach is how easy it will be to add enhancements, what if you want to add an owner name? No problem, simply add the member variable to your class and modify your hydration a bit:

class Owner
{
    public $year;
    public $measure;
    public $month;
    public $name;
}

$names = array('Lars', 'James', 'Kirk', 'Robert');

// Demonstrative hydration 
for ($i = 1 ; $i <= 3 ; $i++) {

    $owner = new Owner();

    $owner->year = 2012;
    $owner->measure = $i;
    $owner->month = rand(1,12);
    $owner->name = array_rand($names);

    $owners[] = $owner;
}

foreach ($owners as $owner) {
    var_dump(sprintf('%s: %d.%d: %d', $owner->name, $owner->month, $owner->year, $owner->measure));
}

您必须记住的是,上述code片段都只是建议,如果你更深层多维数组棒,那么你将不得不找出一个元素订货安排,有意义的并与你一起工作,如果你认为你会遇到麻烦的设置半年下来,道路,那么它是最好的,而你有机会实现一个更好的策略。

You have to remember that the above code snippets are just suggestions, if you rather stick with deep multi-dimensional arrays, then you will have to figure out an element ordering arrangement that makes sense to YOU and those you work with, if you think you will have trouble with the setup six months down the road, then it is best to implement a better strategy while you have the chance.

这篇关于最佳实践:如何构建阵列 - 标准和命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆