脚本中有太多的elseif语句


too many elseif statements within a script

我刚刚写了一个php脚本,我不得不使用许多elseif语句(即大约300左右);这使得脚本非常冗长,并降低了代码的可读性,从而使代码调试变得相当艰巨。我的问题是在这种情况下你如何处理长时间的自拍?除了使用elseif,你有什么建议吗?我很想听听。

我在下面找到了我的php脚本的一个小摘录,我检查每一行特定的单词并处理它们。

while (! feof($readfile)) {
    //Read in the file line by line    
    $line = fgets($readfile, 4096);
    if (preg_match("/gold/", $line)) {
        //Call the user-defined function
        $line = myfunction($line, "gld");
        if(is_writable($file2)) { //Confirm that the file is writable.
             file_put_contents($file2, $line . PHP_EOL, FILE_APPEND);
             // Write the data
        }
    } elseif (preg_match("/men/", $line)) {
        //Call the user-defined function
        $line = myfunction($line, "mdo");
        if(is_writable($file2)) { //Confirm that the file is writable.
            file_put_contents($file2, $line . PHP_EOL, FILE_APPEND);
            // Write the data
        }
    } elseif (preg_match("/sac/", $line)) {
        //Call the user-defined function
        $line = myfunction($line, "sac");
           if(is_writable($file2)) { //Confirm that the file is writable.
               file_put_contents($file2, $line . PHP_EOL, FILE_APPEND);
               // Write the data
           }
    } else {
          echo "No match: " . $line;
    }

使用foreach(数组存储每个模式)可以使代码更短,更易读。代码没有经过测试,但是如果有任何错误,您应该了解如何修复它。

$patterns = array(
    array('/gold/', 'gld'),
    array('/men/', 'mdo'),
    array('/sac/', 'sac')
);
while (!feof($readfile)) {
    $match = false;
    foreach($patterns as $pattern) {
        if(preg_match($pattern[0])) {
            $line = myfunction($line, $pattern[1]);
            if(is_writable($file2)) {
                file_put_contents($file2, $line . PHP_EOL, FILE_APPEND);
            }
            $match = true;
            break;
        }
    }
    if($match === false) {
        echo "No match: " . $line;
    }
}

除了高效的编码,您还可以拥有可读的编码风格。我的意思是你要写干净的代码,例如,你总是在你的if中使用{},或者你总是在你的if中使用:,并使用良好的缩进。

在你的IDE中,你可以为自己定义一个样式并重新格式化你的代码。当然你需要一个整洁的样式。

我建议你搜索高效PHP。

好运

这基本上是@Karl已经发布的一个变体。假设要匹配的模式是简单的字符串,使用preg_match是多余的:

$rules = array(
    'gold' => 'gld',
    'men'  => 'mdo',
    'sac'  => 'sac',
);
while (!feof($readfile)) {
    $chunk = fgets($readfile, 4096);
    $result = null;
    foreach ($rules as $k => $v) {
        if (strpos($chunk, $k) !== false) {
            $result = myfunction($chunk, $v);
            break;
        }
    }
    if (! is_null($result))
        echo 'No match: ' . $chunk;
    elseif (is_writable($file2))
        file_put_contents($file2, $result . PHP_EOL, FILE_APPEND);
}

@Karl的答案使用DRY原则更有效,但是如果您只是在其他情况下寻找替代方案,而您发现自己被elseif所淹没,也许您会发现switch()更干净,更容易遵循。

我同意彼此嵌套的self会让人困惑。在开关中,最后一个"else"结果将是"default:" case开关。

    switch($line){
     case(preg_match("/gold/", $line)):
        //Call the user-defined function
        $line = myfunction($line, "gld");
        if(is_writable($file2)) { //Confirm that the file is writable.
             file_put_contents($file2, $line . PHP_EOL, FILE_APPEND);
             // Write the data
        break;
     case(preg_match("/men/", $line)) :
          //....   rest of cases and logic below

}