您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页步步为营 .NET 代码重构学习笔记 十一

步步为营 .NET 代码重构学习笔记 十一

来源:爱玩科技网

步步为营 .NET 代码重构学习笔记系列

 

一、Remove Control Flag(移除控制标记)

动机(Motivation)

以break语句或return语句取代控制标记。

示例

        public void CheckSecurity(string[] people)
        {
            string found = string.Empty;
            for (int i = 0; i < people.Length; i++)
            {
                if (found.Equals(""))
                {
                    if (people[i].Equals("Don"))
                    {
                        found = "Don";
                    }
                    if (people[i].Equals("John"))
                    {
                        found = "John";
                    }
                }
            }
            SomeDataCode(found);
        }

改为

        public void CheckSecurity(string[] people)
        {
            string found = string.Empty;
            for (int i = 0; i < people.Length; i++)
            {
                if (found.Equals(""))
                {
                    if (people[i].Equals("Don"))
                    {
                        found = "Don";
                        break;
                    }
                    if (people[i].Equals("John"))
                    {
                        found = "John";
                        break;
                    }
                }
            }
            SomeDataCode(found);
        }

示例

        public string FindPeople(string[] people)
        {
            string found = string.Empty;
            for (int i = 0; i < people.Length; i++)
            {
                if (found.Equals(""))
                {
                    if (people[i].Equals("Don"))
                    {
                        found = "Don";                      
                    }
                    if (people[i].Equals("John"))
                    {
                        found = "John";                      
                    }
                }
            }
            return found;
        }

改为

        public string FindPeople(string[] people)
        {
            string found = string.Empty;
            for (int i = 0; i < people.Length; i++)
            {
                if (found.Equals(""))
                {
                    if (people[i].Equals("Don"))
                    {
                        return "Don";
                    }
                    if (people[i].Equals("John"))
                    {
                        return "John";
                    }
                }
            }
            return string.Empty;
        }

二、Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件式)

动机(Motivation)

使用卫语句(guard clauses)表现所有特殊情况。

示例

        public double GetPayAmount()
        {
            double result;
            if (IsDead)
                result = DeadAmount();
            else
            {
                if (IsSeparated)
                    result = SeparatedAmount();
                else
                {
                    if (IsRetired)
                        result = RetiredPayAmount();
                    else
                        result = NormalPayAmount();
                }
            }
            return result;
        }

改为

        public double GetPayAmount()
        {
            if (IsDead)
                return DeadAmount();
            if (IsSeparated)
                return SeparatedAmount();
            if (IsRetired)
                return RetiredPayAmount();
            return NormalPayAmount();
        }

三、Introduce Null Object (引入Null对象)

动机(Motivation)

将null value(无效值)替换为null object(无效物)

示例

            if (customer == null)
                plan = BillingPlan.Basic();
            else
                plan = customer.GetPlan();

改为

        public double GetPayAmount()
        {
            if (customer.IsNull())
                plan = BillingPlan.Basic();
            else
                plan = customer.GetPlan();
        }

四、Rename Method(重新命名函数)

动机(Motivation)

修改函数名称让它人容易理解它的作用

示例

        public int Getinvcdtlmt()
        {
            return 10;
        }

改为

        public int GetInvoiceAbleCreditLimit()
        {
            return 10;
        }

五、Separate Query from Modifier(将查询函数和修改函数分离)

动机(Motivation)

建立两个不同的函数,其中一个负责查询,另一个负责修改。

示例

        public string FindPeople(string[] people)
        {
            string found = string.Empty;
            for (int i = 0; i < people.Length; i++)
            {
                if (found.Equals(""))
                {
                    if (people[i].Equals("Don"))
                    {
                        SendMail();
                        return "Don";
                    }
                    if (people[i].Equals("John"))
                    {
                        SendMail();
                        return "John";
                    }
                }
            }
            return string.Empty;
        }

改为

        public string FindPeople(string[] people)
        {
            string found = FindPeopleOne(people);
            SendMailToPeople(found);
        }

        public string FindPeopleOne(string[] people)
        {
            string found = string.Empty;
            for (int i = 0; i < people.Length; i++)
            {
                if (found.Equals(""))
                {
                    if (people[i].Equals("Don"))
                    {
                        return "Don";
                    }
                    if (people[i].Equals("John"))
                    {
                        return "John";
                    }
                }
            }
            return string.Empty;
        }

        public void SendMailToPeople(string people)
        {
            if (!string.IsNullOrEmpty(people))
                SendMail();
        }

六、Parameterize Method(令函数携带参数)

动机(Motivation)

建立单一函数,以参数表达那些不同的值

示例

        public double TenPercentRaise()
        {
            return salary * 1.1;
        }

        public double FivePercentRaise()
        {
            return salary * 1.05;
        }

改为

        public double Raise(double factor)
        {
            return salary * factor;
        }

七、Replace Parameter with Explicit Methods(以明确函数取代参数)

动机(Motivation)

针对该参数的每一个可能值,建立一个函数。

示例

        private int _height;
        private int _width;

        public void SetValue(string name, int value)
        {
            if (name.Equals("height"))
            {
                _height = value;
                return;
            }
            if (name.Equals("width"))
            {
                _width = value;
                return;
            }
        }

改为

        private int _height;
        private int _width;

        public int Height
        {
            set { _height = value; }
        }

        public int Width
        {
            set { _width = value; }
        }

转载于:https://www.cnblogs.com/springyangwc/archive/2011/06/01/2067443.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务