Clean Code_Note6_Decrease the Number of Parameters
一個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 | using System; using System.Collections.Generic; using System.IO; namespace CleanUpMethods { public class DecreaseNumberOfParameters { public DecreaseNumberOfParameters(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) || string.IsNullOrEmpty(telephone)) { 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, telephone }; writer.WriteLine(aPersonRecod); } } } } |
通常建議可以利用新建一個DTO Class (Data Transfer Object)
PersonRecord.cs
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 | using System; using System.Collections.Generic; using System.IO; namespace CleanUpMethods { public class PersonRecord { public string Name { get; set; } public string HomeAddress { get; set; } public string Country { get; set; } public string Email { get; set; } public string FileLocation { get; set; } } public class DecreaseNumberOfParameters { public DecreaseNumberOfParameters(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) || string.IsNullOrEmpty(telephone)) { 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, telephone }; writer.WriteLine(aPersonRecod); } } } } |
參數經過化簡為傳入一個物件後就比較容易維護了
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 | using System; using System.Collections.Generic; using System.IO; namespace CleanUpMethods { public class PersonRecord { public string Name { get; set; } public string HomeAddress { get; set; } public string Country { get; set; } public string Email { get; set; } public string FileLocation { get; set; } } public class DecreaseNumberOfParameters { public DecreaseNumberOfParameters(string name, string homeAddress, string country, string email, string telephone, string fileLocation) { PersonRecord personRecord = new PersonRecord { Name = name, HomeAddress = homeAddress, Email = email, Country = country, FileLocation = fileLocation }; SaveHomeAddress(telephone, personRecord); } public void SaveHomeAddress(string telephone, PersonRecord personRecord) { if (string.IsNullOrEmpty(personRecord.Name) || string.IsNullOrEmpty(personRecord.HomeAddress) || string.IsNullOrEmpty(personRecord.FileLocation) || string.IsNullOrEmpty(telephone)) { Console.WriteLine("Input parameters are empty"); } else { using FileStream fileStream = new FileStream(personRecord.FileLocation, FileMode.Append); using StreamWriter writer = new StreamWriter(fileStream); List<string> aPersonRecod = new List<string> { personRecord.Name, personRecord.HomeAddress, personRecord.Country, personRecord.Email, telephone }; writer.WriteLine(aPersonRecod); } } } } |
另外還有一種情況是一個object能傳到底就傳到底
不要中途又變成很多參數傳入
一個實際案例RegisterUser方法傳入的是一個UserModel納在當中又有去呼叫到
AuthService物件中的CreateUser這裡其實就是可以直接改為model傳入的部分
不需要用這麼多參數分開
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 | using System; namespace CleanUpMethods { public class PreserveWholeObject { public interface Database { void SaveUser(string userName, string password, string email); } public class UserModel { public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public string ConfirmPassword { get; set; } } public class User { public string UserName { get; set; } public string Email { get; set; } } public class AuthService { private static Database _database; public static User CreateUser(string userName, string password, string email) { _database.SaveUser(userName, password, email); return new User { Email = email, UserName = password }; } } public User RegisterUser(UserModel model) { User newUser = null; try { newUser = AuthService.CreateUser(model.UserName, model.Password, model.Email); } catch(InsufficientMemoryException) { throw; } catch(Exception ex) { LogError(ex); } return newUser; } #region Other methods private void LogError(Exception ex) { throw new NotImplementedException(); } #endregion } } |
經調整後最終一個程式
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 | using System; namespace CleanUpMethods { public class PreserveWholeObject { public interface Database { void SaveUser(string userName, string password, string email); } public class UserModel { public string UserName { get; set; } public string Email { get; set; } public string Password { get; set; } public string ConfirmPassword { get; set; } } public class User { public string UserName { get; set; } public string Email { get; set; } } public class AuthService { private static Database _database; public static User CreateUser(UserModel model) { _database.SaveUser(model.UserName, model.Password, model.Email); return new User { Email = model.Email, UserName = model.UserName }; } } public User RegisterUser(UserModel model) { User newUser = null; try { newUser = AuthService.CreateUser(model); } catch(InsufficientMemoryException) { throw; } catch(Exception ex) { LogError(ex); } return newUser; } #region Other methods private void LogError(Exception ex) { throw new NotImplementedException(); } #endregion } } |
留言
張貼留言