發表文章

目前顯示的是有「Clean Code」標籤的文章

Clean Code_筆記(微軟C#篇)

圖片
  Clean Code_Note1_Eliminate poor names in code Clean Code_Note2_Really Need All comments? Clean Code_Note3_Eliminate Long Methods Clean Code_Note4_Remove Duplicated Code Clean Code_Note5_Safely Remove Unneeded Method Parameters Clean Code_Note6_Decrease the Number of Parameters Clean Code_Note7_Remove Duplicated If Statements_Simplify If Statements_Replace Nested If Statements With Guard Clauses Clean Code_Note8_Eliminate Null Checks Clean Code_Note9_Replace Switch Statement With Subclasses

Clean Code_Note9_Replace Switch Statement With Subclasses

  switch case 應該在寫程式時很常用到 那往往也會讓程式碼看起來比較有結構性但也可能因為太長導致不好維護,甚至很多重複性值的程式。 以下是一個案例,用 switch 來根據類型控制邏輯: 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 namespace CleanUpConditionals { public class ReplaceSwitchStatementWithSubclasses { public ReplaceSwitchStatementWithSubclasses () { var lowLevelManager = new Manager(Manager.LOW_LEVEL, 30000 ); decimal salary = lowLevelManager.GetMonthlySalary( 0 ); } class Manager { public const int LOW_LEVEL = 0 ; public const int MIDDLE_LEVEL = 1 ; public const int TOP_LEVEL = 2 ; private int _managerLevel; private decimal _baseSalary; public Manager ( int level, decimal baseSalary) { _managerLevel = level; _baseSalary = baseSalary; } ...

Clean Code_Note8_Eliminate Null Checks

  在一段程式中當觀察到物件中充斥很多 method重覆查檢非null的判斷時 就可能是沒有從源頭下手的情境 以下為一個範例 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 74 75 76 77 78 79 using System ; using System.Collections.Generic ; namespace CleanUpConditionals { public class EliminateNullChecks { public class BattleRobot { public int Team { get ; set ; } public int Health { get ; set ; } public int XCoordinate { get ; set ; } public int YCoordinate { get ; set ; } #region Methods public void Attack ( int x, int y) { //shoot missiles implementation omitted... } public List<Tuple< int , int >> GetPossibleMoves() { //some complex implementation omitted......

Clean Code_Note7_Remove Duplicated If Statements_Simplify If Statements_Replace Nested If Statements With Guard Clauses

圖片
  Remove Duplicated If Statements 程式中若出現多段重複的if判斷區塊 可以觀察是否為雷同的判斷情境(回傳值固定之類的) 以下為一個範例 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 using System ; namespace CleanUpConditionals { public class RemoveDuplicatedIfStatements { public static bool IsFibonnaciNumber ( int number) { if (number == 0 ) { return true ; } if (number == 1 ) { return true ; } if (number == 2 ) { return true ; } double fi = ( 1 + Math.Sqrt( 5 )) / 2.0 ; int n = ( int )Math.Floor(Math.Log(number * Math.Sqrt( 5 ) + 0.5 , fi)); int actualFibonacciNumber = ( int )Math.Floor(Math.Pow(fi, n) / Math.Sqrt( 5 ) + 0.5 ); return actualFibonacciNumber == number; } } } 事實上一個只要符合其中一種條件就返回true的情境 那可合併一個if即可並間接把這段實作抽離成額外一個met...

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 empt...

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)) { ...