经验分享:PHP编程的5个良好习惯(三)

开发 后端
本文介绍的是PHP编程的几个良好习惯,分为两篇为大家介绍,希望对你有帮助,一起来看吧!

学习良好的编程习惯能够提高代码质量和效率。像其他语言一样,开发人员可以用 PHP 编写出各种质量级别的代码。根据具体的情况,一般的开发人员往往比优秀的开发人员的效率低 10%~20%。优秀的开发人员的效率更高,因为他们拥有丰富的经验和良好的编程习惯。不良的编程习惯将会影响到效率。本文通过展示一些良好的编程习惯,帮助您成为更优秀的程序员。

接上一篇,经验分享:PHP编程的5个良好习惯(二)

5. 切忌使用复制粘贴

您可以从其他地方将代码复制粘贴到自己的代码编辑器,但这样做有利也有弊。好的一面是,从一个示例或模板中复制代码能够避免很多错误。不好的一面是,这容易带来大量的类似编程方式。

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

不良习惯:类似的代码段

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

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

  1. <?php  
  2. /**  
  3. * Counts the number of messages found in the array of  
  4. * ResultMessage with the getSeverity() value of "Error"  
  5. * @param $messages An array of ResultMessage  
  6. * @return unknown_type  
  7. */ 
  8. function countErrors($messages)  
  9. {  
  10. $matchingCount = 0;  
  11. foreach($messages as $m) {  
  12. if ($m->getSeverity() == "Error") {  
  13. $matchingCount++;  
  14. }  
  15. }  
  16. return $matchingCount;  
  17. }  
  18. /**  
  19. * Counts the number of messages found in the array of  
  20. * ResultMessage with the getSeverity() value of "Warning"  
  21. *  
  22. * @param $messages An array of ResultMessage  
  23. * @return unknown_type  
  24. */ 
  25. function countWarnings($messages)  
  26. {  
  27. $matchingCount = 0;  
  28. foreach($messages as $m) {  
  29. if ($m->getSeverity() == "Warning") {  
  30. $matchingCount++;  
  31. }  
  32. }  
  33. return $matchingCount;  
  34. }  
  35. /**  
  36. * Counts the number of messages found in the array of  
  37. * ResultMessage with the getSeverity() value of "Information"  
  38. *  
  39. * @param $messages An array of ResultMessage  
  40. * @return unknown_type  
  41. */ 
  42. function countInformation($messages)  
  43. {  
  44. $matchingCount = 0;  
  45. foreach($messages as $m) {  
  46. if ($m->getSeverity() == "Information") {  
  47. $matchingCount++;  
  48. }  
  49. }  
  50. return $matchingCount;  
  51. }  
  52. $messages = array(new ResultMessage("Error""This is an error!"),  
  53. new ResultMessage("Warning""This is a warning!"),  
  54. new ResultMessage("Error""This is another error!"));  
  55. $errs = countErrors($messages);  
  56. echo("There are " . $errs . " errors in the result.\n");  
  57. 63.?> 

复制代码良好习惯:带参数的可重用函数

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

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

  1. <?php  
  2. /*  
  3. * Counts the messages with the given severity in the array  
  4. * of messages.  
  5. * @param $messages An array of ResultMessage  
  6. * @return int Count of messages matching $withSeverity  
  7. */ 
  8. function countMessages($messages$withSeverity)  
  9. {  
  10. $matchingCount = 0;  
  11. foreach($messages as $m) {  
  12. if ($m->getSeverity() == $withSeverity) {  
  13. $matchingCount++;  
  14. }  
  15. }  
  16. return $matchingCount;  
  17. }  
  18. /**  
  19. * Counts the number of messages found in the array of  
  20. * ResultMessage with the getSeverity() value of "Error"  
  21. * @param $messages An array of ResultMessage  
  22. * @return unknown_type  
  23. */ 
  24. function countErrors($messages)  
  25. {  
  26. return countMessages($messages"Errors");  
  27. }  
  28. /**  
  29. * Counts the number of messages found in the array of  
  30. * ResultMessage with the getSeverity() value of "Warning"  
  31. * @param $messages An array of ResultMessage  
  32. * @return unknown_type  
  33. */ 
  34. function countWarnings($messages)  
  35. {  
  36. return countMessages($messages"Warning");}  
  37. /**  
  38. * Counts the number of messages found in the array of  
  39. * ResultMessage with the getSeverity() value of "Warning"  
  40. *  
  41. * @param $messages An array of ResultMessage  
  42. * @return unknown_type  
  43. */ 
  44. function countInformation($messages)  
  45. {  
  46. return countMessages($messages"Information");  
  47. }  
  48. $messages = array(new ResultMessage("Error""This is an error!"),  
  49. new ResultMessage("Warning""This is a warning!"),  
  50. new ResultMessage("Error""This is another error!"));  
  51. $errs = countErrors($messages);  
  52. echo("There are " . $errs . " errors in the result.\n");  
  53. ?> 

结束语

如果您在编写 PHP 代码的过程中养成本文讨论的良好习惯,您将能够构建易读、易理解、易维护的代码。使用这种方式构建的易维护代码将降低调试、修复和扩展代码所面临的风险。

使用良好的名称和更短的方法能够提高代码的可读性。注释代码的目的有利于代码理解和扩展。适当地处理错误会使代码更加健壮。***,停止使用复制粘贴,保持代码干净,提高可重用性。

 

到这,五个良好的习惯都给大家介绍完了。希望对你有帮助。

【编辑推荐】

  1. 提高PHP速度的几种办法
  2. PHP爱好者请坚定你们的信念!
  3. 警惕 PHP程序员最易犯10种错误
  4. 让PHP网站跑的更快 如何优化PHP
  5. 简单说说PHP优化
责任编辑:于铁 来源: 大家论坛
相关推荐

2011-07-07 15:36:51

PHP

2011-07-07 15:26:28

PHP编程习惯

2009-01-03 14:34:49

ibmdwPHP

2009-01-03 10:40:41

PHP编程代码

2011-07-14 22:04:16

VC++

2010-04-08 11:17:06

Unix操作系统

2022-04-08 14:38:43

程序员习惯终端

2010-06-11 14:35:18

UML序列图

2011-04-13 10:16:41

编程习惯

2011-03-29 12:41:49

编程

2020-04-22 10:35:07

编程学习技术

2011-07-15 15:10:37

PHP

2011-03-24 09:25:54

程序员编程

2022-10-08 10:42:20

Linux虚拟机

2010-09-02 12:54:30

CSS

2020-11-02 13:03:28

MySQLSQL索引

2024-02-26 08:13:51

MySQLSQL性能

2021-08-17 09:55:50

pandas 8indexPython

2009-10-20 09:42:16

VB.NET编程

2015-11-06 14:54:10

程序员习惯
点赞
收藏

51CTO技术栈公众号