首页 > 其他 > 详细

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

时间:2015-01-25 12:33:27      阅读:342      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!