重构之卫语句

如果代码嵌套层次过多,阅读内层逻辑时需要仔细查看外层代码信息,增加了理解代码逻辑难度。通过使用卫语句,可以有效减少代码嵌套层次,逻辑会变得更清晰。

先看一段嵌套层次比较多的代码:

然后看一下使用卫语句优化后的效果:

        /// <summary>
        /// 获取当前用户的两个绑定城市(城市列表)
        /// </summary>
        /// <returns></returns>
        public IList<CityForGoodsVM> GetUserTwoCity()
        {
            try
            {
                //局部变量的定义与初始化
                CityForGoodsVM householdCity = new CityForGoodsVM();
                CityForGoodsVM residentCity = new CityForGoodsVM();
                IList<CityForGoodsVM> list = new List<CityForGoodsVM>();
                //判断当前用户是否登录
                if (IsLogined())
                {
                    //获取当前用户
                    CurrentUser user = GetCurrentUser();
                    if (user == null)
                    {
                        list = null;
                    }
                    else
                    {
                        //获取户籍城市
                        householdCity = GetUserHouseholdCity(user.CustomerID);
                        //获取居住城市
                        residentCity = GetUserResidentCity(user.CustomerID);

                        if (householdCity == null && residentCity == null)
                        {
                            //若电商中并没有该用户数据
                            list = null;
                        }
                        else
                        {
                            //判断户籍城市和居住城市是否相同
                            if (householdCity.Code == residentCity.Code)
                            {
                                //相同,只添加其一
                                if (!string.IsNullOrEmpty(householdCity.Code))
                                {
                                    list.Add(householdCity);
                                }
                                else
                                {
                                    list = null;
                                }
                            }
                            else
                            {
                                //不同,添加不为空的
                                if (!string.IsNullOrEmpty(householdCity.Code))
                                {
                                    list.Add(householdCity);
                                }
                                if (!string.IsNullOrEmpty(residentCity.Code))
                                {
                                    list.Add(residentCity);
                                }
                            }
                        }
                    }
                }
                else
                {
                    //若用户未登录
                    list = null;
                }
                //遍历城市
                if (list != null)
                {
                    foreach (var item in list)
                    {
                        //若当前城市未开通售卖没有提车热线
                        if (string.IsNullOrEmpty(item.Hotline))
                        {
                            item.Hotline = "666";
                        }
                        //1为已经开通售卖2为未开通售卖
                        if (item.Initial != "1")
                        {
                            item.Initial = "2";
                        }
                    }
                }

                return list;

            }
            catch (Exception ex)
            {

                throw new Exception(ExceptionCode.GetUserTwoCityErrorCode, "获取当前用户的两个绑定城市异常", ex, TeldExceptionLevel.Error);
            }
        }
View Code

 

再看一下使用卫语句优化后的效果:

        /// <summary>
        /// 获取当前用户的两个绑定城市(城市列表)
        /// </summary>
        /// <returns></returns>
        public IList<CityForGoodsVM> GetUserTwoCity2()
        {
            try
            {
                //局部变量的定义与初始化
                CityForGoodsVM householdCity = new CityForGoodsVM();
                CityForGoodsVM residentCity = new CityForGoodsVM();

                if (IsLogined())
                {
                    return null;
                }

                //获取当前用户
                CurrentUser user = GetCurrentUser();
                if (user == null)
                {
                    return null;
                }

                //获取户籍城市
                householdCity = GetUserHouseholdCity(user.CustomerID);
                //获取居住城市
                residentCity = GetUserResidentCity(user.CustomerID);

                if (householdCity == null && residentCity == null)
                {
                    //若电商中并没有该用户数据
                    return null;
                }
                IList<CityForGoodsVM> list = new List<CityForGoodsVM>();
                //判断户籍城市和居住城市是否相同
                if (householdCity.Code == residentCity.Code)
                {
                    if (string.IsNullOrEmpty(householdCity.Code))
                    {
                        return null;
                    }
                    //相同,只添加其一
                    list.Add(householdCity);
                }
                else
                {
                    //不同,添加不为空的
                    if (!string.IsNullOrEmpty(householdCity.Code))
                    {
                        list.Add(householdCity);
                    }
                    if (!string.IsNullOrEmpty(residentCity.Code))
                    {
                        list.Add(residentCity);
                    }
                }

                //遍历城市
                foreach (var item in list)
                {
                    //若当前城市未开通售卖没有提车热线
                    if (string.IsNullOrEmpty(item.Hotline))
                    {
                        item.Hotline = "4001-300-001";
                    }
                    //1为已经开通售卖2为未开通售卖
                    if (item.Initial != "1")
                    {
                        item.Initial = "2";
                    }
                }

                return list;

            }
            catch (Exception ex)
            {

                throw new Exception(ExceptionCode.GetUserTwoCityErrorCode, "获取当前用户的两个绑定城市异常", ex, TeldExceptionLevel.Error);
            }
        }
View Code

 

优化后的嵌套层次较少,逻辑也更为清晰一些

posted on 2022-11-19 14:56  sishuiruoshan  阅读(68)  评论(0编辑  收藏  举报

导航