zoukankan      html  css  js  c++  java
  • 关于自己多次return的改写

     /// <summary>
            /// 修改政务公开目录
            /// </summary>
            /// <param name="tree_id">目录id</param>
            /// <param name="tree_name">目录名称</param>
            /// <param name="p_tree_id">上级目录id</param>
            /// <param name="depart_belong">所属部门分类</param>
            /// <param name="message">返回的提示消息</param>
            /// <returns></returns>
            public bool updateGovernmentOpenTree(long tree_id,string tree_name, long? p_tree_id, string depart_belong, out string message)
            {
                if (!IfCircle(tree_id, p_tree_id))
                {
                    message = "出现目录的循环,请重新操作";
                   return false;
                }
                if (IfParentNoHasChilren(depart_belong, tree_id))
                {
                    message = "此目录下存在子目录的部门分类不包含在此目录中,逻辑错误,请重新操作";
                    return false;
                }
                if (p_tree_id != null)
                {
                    T_GOVERNMENT_AFFAIR_OPEN_TREE gaot = GetOneGovernmentTree(Convert.ToInt64(p_tree_id));
                    if (gaot == null)
                    {
                        message = "不存在此上级目录";
                        return false;
                    }
                    else
                    {
                        if (IfNoHasString(gaot.DEPART_BELONG, depart_belong))//判断包含关系(如果不包含这些部门分类)
                        {
                            message = "上级目录的部门分类不包含此目录的所选分类,请重新操作";
                            return false;
                        }
                    }
                }
                if (IfExistName(tree_name,tree_id))
                {
                    message = "已经存在此政务公开目录名";
                    return false;
                }
                T_GOVERNMENT_AFFAIR_OPEN_TREE governmenttree = db.T_GOVERNMENT_AFFAIR_OPEN_TREE.FirstOrDefault(g => g.TREE_ID == tree_id);
                governmenttree.TREE_NAME = tree_name;
                governmenttree.P_TREE_ID = p_tree_id;
                governmenttree.DEPART_BELONG = depart_belong;
                if (db.SaveSubmit())
                {
                    message = "修改成功";
                    return true;
                }
                else
                {
                    message = "修改失败";
                    return false;
                }
            }
    

     上面的代码多次return,代码可读性差,该应该改写如下:

                bool flag = true;
                if (!IfCircle(tree_id, p_tree_id))
                {
                    message = "出现目录的循环,请重新操作";
                    flag = false;
                }
                else
                {
                    if (IfParentNoHasChilren(depart_belong, tree_id))
                    {
                        message = "此目录下存在子目录的部门分类不包含在此目录中,逻辑错误,请重新操作";
                        flag = false;
                    }
                    else
                    {
                        if (p_tree_id != null)
                        {
                            T_GOVERNMENT_AFFAIR_OPEN_TREE gaot = GetOneGovernmentTree(Convert.ToInt64(p_tree_id));
                            if (gaot == null)
                            {
                                message = "不存在此上级目录";
                                flag = false;
                            }
                            else
                            {
                                if (IfNoHasString(gaot.DEPART_BELONG, depart_belong))//判断包含关系(如果不包含这些部门分类)
                                {
                                    message = "上级目录的部门分类不包含此目录的所选分类,请重新操作";
                                    flag = false;
                                }
                            }
                        }
                        else
                        {
                            if (IfExistName(tree_name, tree_id))
                            {
                                message = "已经存在此政务公开目录名";
                                flag = false;
                            }
                            else
                            {
                                T_GOVERNMENT_AFFAIR_OPEN_TREE governmenttree = db.T_GOVERNMENT_AFFAIR_OPEN_TREE.FirstOrDefault(g => g.TREE_ID == tree_id);
                                governmenttree.TREE_NAME = tree_name;
                                governmenttree.P_TREE_ID = p_tree_id;
                                governmenttree.DEPART_BELONG = depart_belong;
                                if (db.SaveSubmit())
                                {
                                    message = "修改成功";
                                    flag = true;
                                }
                                else
                                {
                                    message = "修改失败";
                                    flag = false;
                                }
                            }
                        }
                    }
                }
                return flag;
    
  • 相关阅读:
    个人阅读笔记05—数据流图
    JVM(三)程序计数器【PC寄存器】
    JVM(三)运行时数据区的概述,线程概述
    JVM(二)表示两个Class对象是否为同一个类;Java程序对类的使用
    我的面试经之JVM(二)双亲委派机制
    我的面试经之JVM(二) ClassLoader补充【获取类的加载器的方式】
    我的面试经之JVM(二)类加载器的分类
    我的面试经之JVM(二)类加载器子系统ClassLoader类的加载过程
    Python小白要了解的模块大概用途(随学习进度更新),import xxxx
    Python常见的模块(知乎转载)
  • 原文地址:https://www.cnblogs.com/wenghaowen/p/2724028.html
Copyright © 2011-2022 走看看