C # code refactoring talk

Refactoring

Based on the reconstruction (Refactoring) that does not change the existing functionality of the software, improved by adjusting the program code of software quality, performance, design patterns and architecture makes the program more reasonable and improve the scalability and maintainability of the software. 

First, why should reconstruct (Refactoring)

 1, · sustained partial correction and improve the software design.

2, · make the code easier for people to understand.

3 · Help find hidden code defects.

4 · In the long run, help to improve programming efficiency.

Second, when to begin reconstruction (Refactoring)

1, repeated code-code is present.

2, · long oversized classes and methods.

3, · pull a hair and body modifications required action.

4, need too much communication between-classes.

5, · excessive information chain coupling.

6, * imperfect design.

7. Lack of required notices.

Third, how to reconstruct (Refactoring)

The so-called reconstruction is "to maintain external software functions the same, readjust its internal structure."
Rename: renamed, classes, functions, and other members of the name can be changed.

It changed the name of its easy to understand the functional use:

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="numlist"></param>
        /// <returns></returns>
        protected int hename(string ID, Dictionary<int, string> numlist)
        {
            int i = numlist.Count();
            List<string> onelist = (from entity in numlist where entity.Value == ID select entity.Value).ToList();
            numlist.Add(i + 1, ID);
            return onelist.Count();
        }

//修改为
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="numlist"></param>
        /// <returns></returns>
        protected int GetSameNameCount(string ID, Dictionary<int, string> numlist)
        {
            int i = numlist.Count();
            List<string> samelist = (from entity in numlist where entity.Value == ID select entity.Value).ToList();
            numlist.Add(i + 1, ID);
            return samelist.Count();
        }

Extract Method: The sealed to a piece of code in a new function.

If there is a piece of code in the method can be re-used to encapsulate a method, as follows:

       /// <summary>
       /// 
       /// </summary>
       /// <param name="dtFormatedData"></param>
       /// <param name="ReplaceColumn"></param>
       /// <returns></returns>
        private DataTable ReplaceCloumnString(DataTable dtFormatedData, List<string> ReplaceColumn)
        {
            Dictionary<string, string> newfirstReplaces = new Dictionary<string, string>();
            newfirstReplaces = needfirstReplaces;
            foreach (KeyValuePair<string, string> newitem in firstReplaces)
            {
                if (!newfirstReplaces.ContainsKey(newitem.Key))
                {
                    newfirstReplaces.Add(newitem.Key, newitem.Value);
                }
            }
            DataTable dtFormatedDataCopy = dtFormatedData.Clone();
            foreach (DataRow dr in dtFormatedData.Rows)
            {
                foreach (KeyValuePair<string, string> item in newfirstReplaces)
                {
                    foreach (string needColumn in ReplaceColumn)
                    {
                        if (dr[needColumn].ToString().Contains(item.Key))
                        {
                            string ContentStr = dr[needColumn].ToString();
                            dr[needColumn] = ReplaceStr(ContentStr, item.Key, item.Value);
                        }
                    }
                }
                DataRow drNewRow = dtFormatedDataCopy.NewRow();
                drNewRow.ItemArray = dr.ItemArray;
                dtFormatedDataCopy.Rows.Add(drNewRow);
            }
            return dtFormatedDataCopy;
        }
///修改为
           /// <summary>
       /// 
       /// </summary>
       /// <param name="dtFormatedData"></param>
       /// <param name="ReplaceColumn"></param>
       /// <returns></returns>
        private DataTable ReplaceCloumnString(DataTable dtFormatedData, List<string> ReplaceColumn)
        {
            Dictionary<string, string> newfirstReplaces = GetReplaceDictionary();
            DataTable dtFormatedDataCopy = dtFormatedData.Clone();
            foreach (DataRow dr in dtFormatedData.Rows)
            {
                foreach (KeyValuePair<string, string> item in newfirstReplaces)
                {
                    foreach (string needColumn in ReplaceColumn)
                    {
                        if (dr[needColumn].ToString().Contains(item.Key))
                        {
                            string ContentStr = dr[needColumn].ToString();
                            dr[needColumn] = ReplaceStr(ContentStr, item.Key, item.Value);
                        }
                    }
                }
                DataRow drNewRow = dtFormatedDataCopy.NewRow();
                drNewRow.ItemArray = dr.ItemArray;
                dtFormatedDataCopy.Rows.Add(drNewRow);
            }
            return dtFormatedDataCopy;
        }
        private Dictionary<string, string> GetReplaceDictionary()
        {
            Dictionary<string, string> newfirstReplaces = new Dictionary<string, string>();
            newfirstReplaces = needfirstReplaces;
            foreach (KeyValuePair<string, string> newitem in firstReplaces)
            {
                if (!newfirstReplaces.ContainsKey(newitem.Key))
                {
                    newfirstReplaces.Add(newitem.Key, newitem.Value);
                }
            }
            return newfirstReplaces;
        }

------------------------------
Encapsulate Field,: will turn into a field attribute

as follows:

  public string =null;
///转换为
  public string FieldName { get; set; }

Extract Interface: a property or function will turn into an interface, whereby the current property or function of the interface becomes.

 public class FolderBE
    {
       public string FolderOne
       { get; set; }
       public DateTime? CreateFolderOne
       { get; set; }
       public string FolderTwo 
       { get; set; }
       public DateTime? CreateFolderTwo 
       { get; set; }
       public string FolderThree
       { get; set; }
       public DateTime? CreateFolderThree
       { get; set; }
       public string FolderFour
       { get; set; }
       public DateTime? CreateFolderFour
       { get; set; }
       public string FolderFive 
       { get; set; }
       public DateTime? CreateFolderFive
       { get; set; }
    }

        public List<SingleResultBE> HavelistResult = new List<SingleResultBE>();

        private bool CheckFolderExist(FolderBE folder, int item)
        {
            List<FolderBE> HavelistFolder = new List<FolderBE>();
            if (item == 1)
            {
                HavelistFolder = (from entity in listFolder
                                  where entity.FolderOne == folder.FolderOne &&
                                  string.IsNullOrEmpty(entity.FolderTwo)
                                  select entity).ToList();
            }
            else if (item == 2)
            {
                HavelistFolder = (from entity in listFolder
                                  where entity.FolderOne == folder.FolderOne &&
                                  entity.FolderTwo == folder.FolderTwo && string.IsNullOrEmpty(entity.FolderThree)
                                  select entity).ToList();
            }
            else if (item == 3)
            {
                HavelistFolder = (from entity in listFolder
                                  where entity.FolderOne == folder.FolderOne &&
                                  entity.FolderTwo == folder.FolderTwo && entity.FolderThree == folder.FolderThree
                                  select entity).ToList();
            }
            else if (item == 4)
            {
                HavelistFolder = (from entity in listFolder
                                  where entity.FolderOne == folder.FolderOne &&
                                  entity.FolderTwo == folder.FolderTwo && entity.FolderThree == folder.FolderThree &&
                                  entity.FolderFour == folder.FolderFour
                                  select entity).ToList();
            }
            else if (item == 5)
            {
                HavelistFolder = (from entity in listFolder
                                  where entity.FolderOne == folder.FolderOne &&
                                  entity.FolderTwo == folder.FolderTwo && entity.FolderThree == folder.FolderThree &&
                                  entity.FolderFour == folder.FolderFour && entity.FolderFive == folder.FolderFive
                                  select entity).ToList();
            }
            return HavelistFolder.Count == 0;
        }

Promote Local Variable to Parameter: will be upgraded to a local variable function parameters

As
Reorder Parameters: Sort function parameters

A little
Remove Parameters: delete a function parameter, but the use of this parameter within the function will not be deleted.
As, as a global variable with
reconstruction function (chapter6)

the Extract Method,
provided: indirect variables have been processed by another reconstruction method.
Objective: to reduce the particle size to increase multiplexing function; strengthening code clarity.
Goal: name of the function can be a good expression of the function to achieve "functional." Rather than trying to do.

Inline Method

is the reverse of Extract Method. It is because of these reverse process, you can rest assured that bold reconstructed.

Prerequisite: Inline function is not polymorphic. I can not express the multi-state case after Inline.
Objective: to remove non-essential indirect. Or a group of tissue for recombinant unreasonable premise function.
Goal: to remove unnecessary functions or excessive delegation.

Extraction method is one of the most common remodeling. When a method or methods seem too need some code comments in order to understand its purpose, it can be considered to extract them out as a separate method.

Reproduced in: https: //www.cnblogs.com/springyangwc/archive/2011/03/15/1985069.html

Guess you like

Origin blog.csdn.net/weixin_34221332/article/details/93340861