设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

20条常见的编码陷阱 你中枪了没?

2012-11-20 14:18| 发布者: 红黑魂| 查看: 3106| 评论: 0|来自: CSDN

摘要: 不管你现在的编程技能有多么的高超,曾经你也是个亦步亦趋,不断的学习的初学者。在编程这条曲折的道路上,我想你肯定犯过一些低级的错误、遇见过一些普通的编码陷阱。本文作者跨越多个语言,为大家总结了20条常规陷 ...

8.抛出异常,而不是采用盗梦空间式的嵌套(Inception-Style Nesting)

多层次的嵌套是丑陋的、难以维护和不可读的。下面的代码是个简单的例子,但是随着时间的推移会变得更糟:

  1. // anti-pattern  
  2. $error_message = null;  
  3. if ($this->form_validation->run())  
  4. {  
  5.    if ($this->upload->do_upload())  
  6.    {  
  7.       $image = $this->upload->get_info();  
  8.       if ( ! $this->image->create_thumbnail($image['file_name'], 300, 150))  
  9.       {  
  10.          $error_message = 'There was an error creating the thumbnail.';  
  11.       }  
  12.    }  
  13.    else 
  14.    {  
  15.       $error_message = 'There was an error uploading the image.';  
  16.    }  
  17. }  
  18. else 
  19. {  
  20.    $error_message = $this->form_validation->error_string();  
  21. }  
  22. // Show error messages  
  23. if ($error_message !== null)  
  24. {  
  25.    $this->load->view('form'array(  
  26.       'error' => $error_message,  
  27.    ));  
  28. }  
  29. // Save the page  
  30. else 
  31. {  
  32.    $some_data['image'] = $image['file_name'];  
  33.    $this->some_model->save($some_data);  

如此凌乱的代码,是否该整理下呢。建议大家使用异常这个清洁剂:

  1. try  
  2. {  
  3.    if ( ! $this->form_validation->run())  
  4.    {  
  5.       throw new Exception($this->form_validation->error_string());  
  6.    }  
  7.    if ( ! $this->upload->do_upload())  
  8.    {  
  9.       throw new Exception('There was an error uploading the image.');  
  10.    }  
  11.    $image = $this->upload->get_info();  
  12.    if ( ! $this->image->create_thumbnail($image['file_name'], 300, 150))  
  13.    {  
  14.       throw new Exception('There was an error creating the thumbnail.');  
  15.    }  
  16. }  
  17. // Show error messages  
  18. catch (Exception $e)  
  19. {  
  20.    $this->load->view('form'array(  
  21.       'error' => $e->getMessage(),  
  22.    ));  
  23.    // Stop method execution with return, or use exit  
  24.    return;  
  25. }  
  26. // Got this far, must not have any trouble  
  27. $some_data['image'] = $image['file_name'];  
  28. $this->some_model->save($some_data); 

虽然代码行数并未改变,但它拥有更好的可维护性和可读性。尽量保持代码简单。

9.False——Happy方法

Ruby或Python开发者常常关注一些微小的异常,这是相当不错的事情。如果有地方出错就会抛出异常并且你会立即知道问题所在。

在PHP中,特别是使用比较老的框架,如CodeIgniter,与抛出异常相比,它仅仅返回一个flase值,并且把错误字符串分配给其他一些属性。这就驱使你使用get_error()方法。

Exception-happy远远好于false-happy。如果代码里面存在错误(例如不能连上S3下载图片,或者值为空等),然后抛出一个异常,你也可以通过继承Exception类来抛出特定的异常类型,例如:

  1. class CustomException extends Exception {} 

抛出自定义类型异常会让调试变得更加容易。

10.Use Guard Clauses

使用if语句控制函数或方法的执行路径是很常见的事情,如果if条件为true就执行if里面的代码,否则就执行else里面的代码。例如下面这段代码:

  1. function someFunction($param) {  
  2.     if ($param == 'OK') {  
  3.        $this->doSomething();  
  4.        return true;  
  5.     } else {  
  6.        return false;  
  7.     }  

这是很常见的意大利面条式的代码,通过转换条件对上述代码进行优化,不仅可以增加其可读性,看起来还会更加简单,如下:

  1. function someFunction($param) {  
  2.     if ($param != 'OK'return false;  
  3.     $this->doSomething();  
  4.     return true;  

11.使用While进行简单的迭代

使用for进行循环是很常见的事情:

  1. for (var i = 0; i < x; i++) {  
  2.     ...  

当然,for循环也有许多优势,但是对于一些的循环,使用while或许会更好:

  1. var i = x;  
  2. while (i--) {  
  3.     ...  


酷毙
1

雷人

鲜花

鸡蛋

漂亮

刚表态过的朋友 (1 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部