当前位置:编程学习 > asp >>

步步为营 .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";
              &nb

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,