良好的编程习惯,PHP编程的五个良好习惯

摘要:像其他语言一样,开发人员可以用 模板中复制代码能够避免很多错误。不好的一面是,这容易带来大量的类似编程方式。

一定要注意,不要将代码从应用程序的一部分复制粘贴到另一部分。如果您采用这种方式,请停止这个不良的习惯,然后考虑将这段代码重写为可重用的。一般而言,将代码放置到一个地方便于日后的维护,因为这样只需在一个地方更改代码。

不良习惯:类似的代码段

清单 9 给出了几个几乎一样的方法,只是其中的值不同而已。有一些工具可以帮助找到复制粘贴过来的代码(参见 参考资料)。

清单 9. 不良习惯:类似的代码段



<?php /** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Error" * * @param $messages An array of ResultMessage * @return unknown_type */ function countErrors($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Error") { $matchingCount++; } } return $matchingCount; } /** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Warning" * * @param $messages An array of ResultMessage * @return unknown_type */ function countWarnings($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Warning") { $matchingCount++; } } return $matchingCount; } /** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Information" * * @param $messages An array of ResultMessage * @return unknown_type */ function countInformation($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Information") { $matchingCount++; } } return $matchingCount; } $messages = array(new ResultMessage("Error", "This is an error!"), new ResultMessage("Warning", "This is a warning!"), new ResultMessage("Error", "This is another error!")); $errs = countErrors($messages); echo("There are " . $errs . " errors in the result.n"); ?>






良好习惯:带参数的可重用函数

清单 10 展示了修改后的代码,它将复制的代码放到一个方法中。另一个方法也进行了更改,它现在将任务委托给新的方法。构建通用的方法需要花时间设计,并且这样做使您能停下来思考,而不是本能地使用复制粘贴。但有必要进行更改时,对通用的方法投入的时间将得到回报。

清单 10. 良好习惯:带参数的可重用函数



<?php /* * Counts the messages with the given severity in the array * of messages. * * @param $messages An array of ResultMessage * @return int Count of messages matching $withSeverity */ function countMessages($messages, $withSeverity) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == $withSeverity) { $matchingCount++; } } return $matchingCount; } /** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Error" * * @param $messages An array of ResultMessage * @return unknown_type */ function countErrors($messages) { return countMessages($messages, "Errors"); } /** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Warning" * * @param $messages An array of ResultMessage * @return unknown_type */ function countWarnings($messages) { return countMessages($messages, "Warning"); } /** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Warning" * * @param $messages An array of ResultMessage * @return unknown_type */ function countInformation($messages) { return countMessages($messages, "Information"); } $messages = array(new ResultMessage("Error", "This is an error!"), new ResultMessage("Warning", "This is a warning!"), new ResultMessage("Error", "This is another error!")); $errs = countErrors($messages); echo("There are " . $errs . " errors in the result.n"); ?>






结束语

如果您在编写 PHP 代码的过程中养成本文讨论的良好习惯,您将能够构建易读、易理解、易维护的代码。使用这种方式构建的易维护代码将降低调试、修复和扩展代码所面临的风险。
使用良好的名称和更短的方法能够提高代码的可读性。注释代码的目的有利于代码理解和扩展。适当地处理错误会使代码更加健壮。最后,停止使用复制粘贴,保持代码干净,提高可重用性。

Tags:  良好的生活习惯 良好的睡眠习惯 良好习惯 养成良好习惯 良好的学习习惯 良好的编程习惯

延伸阅读

最新评论

发表评论