使用二维阵列的清晰度值得搜索所需的额外计算时间吗



我从csv中提取数据并将其上传到db,对于某些列,只应输入某些值(否则会破坏系统的其余部分(然而,用户不需要写出完整的输入;y";而不是";是";

我写了一个函数,它接受一个可能的输入数组以及正确的输出。然而,目前的数组和函数是这样的:

$this->acceptedInputMap = array (
'yes' => 'yes',
'y' => 'yes',
'true' => 'yes',
'no' => 'no',
'n' => 'no',
'false' => 'no',
'unknown' => 'unknown',
'unk' => 'unknown',
'' => 'unknown'
);

//these are in the parents class------------------------------------
protected function useAcceptedinput()
{
foreach ($this->columnData as $key => $element) {
if ($this->isExpectedInput($element)) {
$this->useMappedInput($key, $element);
} else {
$this->useColumnDefault($key);
}
}
}
protected function isExpectedInput($element)
{
return array_key_exists(strtolower($element), $this->acceptedInputMap);
}
protected function useColumnDefault($key)
{
$this->columnData[$key] = $this->defaultValue;
}
protected function useMappedInput($key, $element)
{
$this->columnData[$key] = $this->acceptedInputMap[strtolower($element)];
}

我想将其更改为使用不同的结构:

$this->acceptedInputMap = array (
'yes' => array(
'yes',
'y',
'true'
),
'no' => array(
'no',
'n',
'false'
),
'unknown' => array(
'unknown',
'unk',
''
),
);

这对未来的开发人员来说要清楚得多,并且可以更容易地添加更多可接受的输入。这也允许我让父级存储一些常见的加速输入,例如";是";,可以为每个需要它的列下拉。

然而,这是一个2d数组,试图在第二维度中找到值以映射到正确的输入在计算上更为密集。这个改变值吗?此外,总的来说,在让下一个开发更容易与更快之间,你在哪里划分界限?

您可以通过添加注释或空行来组织原始数组,使其更具可读性,而不会牺牲性能:

$this->acceptedInputMap = array (
// yes --------
'yes' => 'yes',
'y' => 'yes',
'true' => 'yes',
// no ----------
'no' => 'no',
'n' => 'no',
'false' => 'no',
// unknown -----
'unknown' => 'unknown',
'unk' => 'unknown',
'' => 'unknown'
);

最新更新