Clean Code_Note5_Safely Remove Unneeded Method Parameters

 



Safely Remove Unneeded Method Parameters

一樣通常可能專案中會遇到一個方法裡面要傳進的參數很多個
使外部要呼叫顯得很困難
不僅要注意順序還要一個個慢慢對應
也有可能是傳進的參數根本沒用到的情況
因此就需要對應做檢查

以下面程式為例

 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
using System;
using System.Collections.Generic;
using System.IO;

namespace CleanUpMethods
{
    public class RemoveUnneededMethodParameter
    {
        public RemoveUnneededMethodParameter(string name, string homeAddress, string country, string email, string telephone, string fileLocation)
        {
            SaveHomeAddress(name, homeAddress, email, country, fileLocation, telephone);
        }

        public void SaveHomeAddress(string name, string homeAddress, string country, string email, string fileLocation, string telephone)
        {

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(homeAddress)
                || string.IsNullOrEmpty(fileLocation))
            {
                Console.WriteLine("Input parameters are empty");
            }
            else
            {
                using FileStream fileStream = new FileStream(fileLocation, FileMode.Append);
                using StreamWriter writer = new StreamWriter(fileStream);
                List<string> aPersonRecod = new List<string>
                {
                    name,
                    homeAddress,
                    country,
                    email
                };
                writer.WriteLine(aPersonRecod);
            }
        }
    }
}


若想要快速找到專案中各個method傳入參數有沒有那種屬於根本沒用到的
可以到專案屬性去將warning勾選設置為All
使其可以觸發
此類型的警告訊息








之後再記得調整回來避免程式被其他警告打擾無法編譯成功


針對沒使用到method 我們去點選燈泡圖示
選擇變更method signature





即可同步更改專案中除了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
using System;
using System.Collections.Generic;
using System.IO;

namespace CleanUpMethods
{
    public class RemoveUnneededMethodParameter
    {
        public RemoveUnneededMethodParameter(string name, string homeAddress, string country, string email, string telephone, string fileLocation)
        {
            SaveHomeAddress(name, homeAddress, email, country, fileLocation);
        }

        public void SaveHomeAddress(string name, string homeAddress, string country, string email, string fileLocation)
        {

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(homeAddress)
                || string.IsNullOrEmpty(fileLocation))
            {
                Console.WriteLine("Input parameters are empty");
            }
            else
            {
                using FileStream fileStream = new FileStream(fileLocation, FileMode.Append);
                using StreamWriter writer = new StreamWriter(fileStream);
                List<string> aPersonRecod = new List<string>
                {
                    name,
                    homeAddress,
                    country,
                    email
                };
                writer.WriteLine(aPersonRecod);
            }
        }
    }
}



Ref:
Treating Warnings as Errors in Visual Studio
Remove unused parameter (IDE0060)




留言

這個網誌中的熱門文章

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

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

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