码迷,mamicode.com
首页 > 其他好文 > 详细

减少if语句的嵌套层次,提高代码的可读性

时间:2015-01-25 12:35:28      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

不合理示例:
if v_type = -1 then
  异常处理2
else if v_type = -2 then
  异常处理2
else if v_type = 1 then
   if v_size = -1 then
      异常处理31
   else if v_size = -2 then
      异常处理31
   else if v_size = 1 then
      if v_color = -1 then
           异常处理321
      else if v_color =-2 then
           异常处理322
      else if v_color = 1 then
         正常业务处理
      else
       异常处理339
      end if;
   else
      异常处理39
   end if;     
else
  异常处理9
end if;


以上代码如果缩进合理时,还免强能看,如没控制缩进则是无法阅读的。
改进为:


//判断类别
if v_type = -1 then
  异常处理2
else if v_type = -2 then
  异常处理2
else if v_type = 1 then
else
  异常处理9
end if;


//判断大小
if v_size = -1 then
  异常处理31
else if v_size = -2 then
  异常处理31
else if v_size = 1 then
else
  异常处理39
end if;     
/

/判断颜色
if v_color = -1 then
     异常处理321
else if v_color =-2 then
     异常处理322
else if v_color = 1 then
   正常业务处理
else
 异常处理339
end if;

减少if语句的嵌套层次,提高代码的可读性

标签:

原文地址:http://blog.csdn.net/sxdtzhp/article/details/43112917

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!