Clean Code_Note3_Eliminate Long Methods

 

Long Methods
代表一個方法裡面寫的程式太多行了做太多事了
這個是比較常見的一個狀況



以下這個例子PerformSearch() 就是太長的一段處理
需要進行Extract Method處理

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Linq;

namespace CleanUpMethods
{
    public class SearchSmartphones
    {
        List<string> smartphones = new List<string>()
            {
                "Samsung Galaxy S20", "Pixel 2", "Pixel 3", "Pixel 4", "iPhone XR", "iPhone 12", "iPhone 12 Pro", "iPhone 12 Pro Max" 
            };

        public void PerformSearch()
        {
            bool continueSearch = true;

            while (continueSearch)
            {
                Console.Write("Search for smartphone: ");
                string keyword = Console.ReadLine();

                var results = smartphones.Where(phone => phone.ToLower().Contains(keyword.ToLower()));

                if (results != null)
                {
                    Console.WriteLine("Here are the matched results.\n");

                    foreach (var result in results)
                    {
                        Console.WriteLine(result);
                    }
                }
                else
                {
                    Console.WriteLine("No results found.");
                }

                string continueSearchResponse;
                do
                {
                    Console.Write("\nMake another search (y/n)?: ");
                    continueSearchResponse = Console.ReadLine();

                    if (continueSearchResponse.ToLower() == "n")
                    {
                        continueSearch = false;
                        break;
                    }
                    if (continueSearchResponse.ToLower() != "y")
                    {
                        Console.WriteLine("Invalid response.");
                    }

                } while (continueSearchResponse.ToLower() != "n" && continueSearchResponse.ToLower() != "y");
            }

            Console.Write("Thanks for searching!");

        }
    }
}

在visual studio中藥作方法抽離
可透過Ctrl+r , Ctrl+m

又或者是選取某一段要抽離出來變成額外method的程式段落從刷子圖示做選擇抽離方法


抽離後結果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;

namespace CleanUpMethods
{
    public class SearchSmartphones
    {
        List<string> smartphones = new List<string>()
            {
                "Samsung Galaxy S20", "Pixel 2", "Pixel 3", "Pixel 4", "iPhone XR", "iPhone 12", "iPhone 12 Pro", "iPhone 12 Pro Max" 
            };

        public void PerformSearch()
        {
            bool continueSearch = true;

            while (continueSearch)
            {
                SearchForSmartPhones();

                continueSearch = ShouldContinueWithSearch(continueSearch);
            }

            Console.Write("Thanks for searching!");

        }

        private static bool ShouldContinueWithSearch(bool continueSearch)
        {
            string continueSearchResponse;
            do
            {
                Console.Write("\nMake another search (y/n)?: ");
                continueSearchResponse = Console.ReadLine();

                if (continueSearchResponse.ToLower() == "n")
                {
                    continueSearch = false;
                    break;
                }
                if (continueSearchResponse.ToLower() != "y")
                {
                    Console.WriteLine("Invalid response.");
                }

            } while (continueSearchResponse.ToLower() != "n" && continueSearchResponse.ToLower() != "y");
            return continueSearch;
        }

        private void SearchForSmartPhones()
        {
            Console.Write("Search for smartphone: ");
            string keyword = Console.ReadLine();

            var results = smartphones.Where(phone => phone.ToLower().Contains(keyword.ToLower()));

            if (results != null)
            {
                Console.WriteLine("Here are the matched results.\n");

                foreach (var result in results)
                {
                    Console.WriteLine(result);
                }
            }
            else
            {
                Console.WriteLine("No results found.");
            }
        }
    }
}

這裡關鍵在於選取要抽離方法的程式區域要多少行為單位或者說要選取到捨麼範圍
這個就要實際應用場景是否能夠看出某段是專門做某階段的任務就適合抽離。







留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題