多维数组的变量命名 (PHP)

发布于 2024-10-01 04:57:08 字数 940 浏览 1 评论 0原文

我很难找到一个命名多维 PHP 数组的系统,以便通过查看变量名称,您可以得到有关多维数组结构的提示。

一个虚构的例子:

$task = array('who'=>'John', 'what'=>'wash the dishes', 'priority'=>10);
$calendar[$year][$month][$day][$task_id] = $task;

在这种情况下,我将其命名为“日历”,因为整个结构整体上有一个简单的含义,但在大多数情况下,与单个单词甚至现实生活中的概念没有如此直接的联系。另外,我在这里使用日期部分(年、月、日)作为示例,但通常我的键是整数数据库记录 ID。

所以我想要一个命名系统来描述这种关系:

"year X month X day -> list of tasks"

更一般地说:

"key1 x key2 x key3 x ... x key n -> items"

一种可能的命名约定,使用单词 by 和下划线作为键之间的分隔符:

$tasks_by_year_month_day = array(...); // items_by_key1_key2_key3

这样,保持相同的约定,我会写:

$tasks_by_month_day = $tasks_by_year_month_day['2010'];

或者

$november2010Tasks_by_day = $tasks_by_year_month_day['2010']['nov'];

是否有标准或更清晰的方式来命名此类数组?

谢谢。

I'm having a hard time trying to figure out a system of naming multi-dimensional PHP arrays, so that by looking at the variable name, you get a hint about the structure of the multi-dimensional array.

A fictional example:

$task = array('who'=>'John', 'what'=>'wash the dishes', 'priority'=>10);
$calendar[$year][$month][$day][$task_id] = $task;

In this case I named it "calendar" because the whole structure has a simple meaning as a whole, but in most cases there is no such direct connection to a single word or even a real-life concept. Also, I used date parts here (year, month, day) for exemplification, but normally my keys are integer database record ids.

So I would like a system of naming to describe this relation:

"year X month X day -> list of tasks"

more generally:

"key1 x key2 x key3 x ... x key n -> items"

A possible naming convention, using the word by and underscores as separators between keys:

$tasks_by_year_month_day = array(...); // items_by_key1_key2_key3

so that, keeping the same convention, I would write:

$tasks_by_month_day = $tasks_by_year_month_day['2010'];

or

$november2010Tasks_by_day = $tasks_by_year_month_day['2010']['nov'];

Is there a standard or cleaner way of naming this kind of arrays?

Thank you.

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

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

发布评论

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

评论(2

烟花易冷人易散 2024-10-08 04:57:08

尽可能使用描述性变量名称可以让人们更轻松地理解代码。然而,总有一天,名称会变得太长,可读性会受到影响。

请记住,变量名称的目的是使程序更易于理解。如果存在不相关的数据信息,则不应包含它。例如:

function getTasksByDate($year, $month, $day) {
    $tasks = getTasks();
    return $tasks[$year][$month][$day];
}

在这种情况下,谁愿意将 $tasks 重命名为 $tasks_by_year_month_day?索引顺序是什么非常清楚。此外,这个例子提出了抽象数据类型的想法。

使用抽象数据类型,您可以公开对任务结构进行操作的具有高度描述性的函数名称。这意味着您真正需要关心索引顺序的唯一情况是在公开 ADT 的函数中。此时,您可以在文件顶部注释索引顺序。

简而言之,您不能希望在变量名称中包含所有信息。大多数时候,也没有人想要每一件。尝试利用当前上下文来缩短变量名称并阐明您的总体目标。

Having descriptive variable names when possible allows someone to understand the code more easily. However, there definitely comes the time when names become too long and the readability suffers.

Remember that the objective of variable names is to make the program easier to understand. If there is information about the data that isn't relevant, you shouldn't include it. For example:

function getTasksByDate($year, $month, $day) {
    $tasks = getTasks();
    return $tasks[$year][$month][$day];
}

In this case, who cares to rename $tasks to $tasks_by_year_month_day? It is more than clear what the indexing order is. Moreover, this example suggests the idea of an abstract data type.

With an abstract data type, you can expose greatly descriptive function names that operate on the task structure. This means that the only case where you really have to care about the order of indexing is in the functions which expose the ADT. At that point, you can comment the index order at the top of the file.

In short, you cannot hope to include every piece of information in your variable name. Most of the time no one wants every piece either. Try leveraging the current context to shorten your variable names and clarify your overall goal.

稀香 2024-10-08 04:57:08

这可能是个人/公司的决定。我经常将它们命名为 $lut_a_to_b (通过 a 查找 b 的表)。它有助于为寿命超过几行的变量编写带有示例结构的注释。

It's probably a personal/company decision. I often name them $lut_a_to_b (lookup table for b by a). It helps to write comments with example structures for variables that live longer than a few lines.

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