Clean Code_Note4_Remove Duplicated Code

 



Remove Duplicated Code


以下範例是有重複感覺的程式段落例子
Before

 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
using System;

namespace CleanUpMethods
{
    public class RemoveDuplicatedCode
    {
        private decimal _belowTaxSalaryAmount;
        private decimal _taxPercentage;

        public RemoveDuplicatedCode(decimal belowTaxSalaryAmount, decimal taxPercentage)
        {
            _belowTaxSalaryAmount = belowTaxSalaryAmount;
            _taxPercentage = taxPercentage;
        }

        public decimal CheckAndCalculate(decimal higherSalaryAmount, decimal grossSalaryAmount)
        {
            if (grossSalaryAmount < 0)
            {
                throw new ArgumentException("Provided salary amount is not correct.");
            }
            if (higherSalaryAmount < 0)
            {
                throw new ArgumentException("Provided salary amount is not correct.");
            }

            return CalculateNetAmount(grossSalaryAmount, higherSalaryAmount);
        }

        private decimal CalculateNetAmount(decimal grossSalaryAmount, decimal higherSalaryAmount)
        {
            if (_belowTaxSalaryAmount == 0 || _taxPercentage == 0)
            {
                return grossSalaryAmount;
            }

            decimal taxSum = 0m;

            if (grossSalaryAmount <= _belowTaxSalaryAmount)
            {
                return grossSalaryAmount;
            }
            decimal tax = (grossSalaryAmount - _belowTaxSalaryAmount) * (_taxPercentage / 100);
            taxSum += tax;

            decimal taxableSalary = grossSalaryAmount;

            if (taxableSalary > higherSalaryAmount)
            {
                taxableSalary = higherSalaryAmount;
            }
            tax = (taxableSalary - _belowTaxSalaryAmount) * (_taxPercentage / 100);
            taxSum += tax;

            return grossSalaryAmount - taxSum;
        }
    }
}


可以看到這部分主要throw的exeption訊息都一樣差別只在前面的if判斷參數數值傳入有差


抽離後





CalculateNetAmount方法重複部分



也是差別在於第一個參數其他計算公式一樣。

抽離後



完整的After

 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
using System;

namespace CleanUpMethods
{
    public class RemoveDuplicatedCode
    {
        private decimal _belowTaxSalaryAmount;
        private decimal _taxPercentage;

        public RemoveDuplicatedCode(decimal belowTaxSalaryAmount, decimal taxPercentage)
        {
            _belowTaxSalaryAmount = belowTaxSalaryAmount;
            _taxPercentage = taxPercentage;
        }

        public decimal CheckAndCalculate(decimal higherSalaryAmount, decimal grossSalaryAmount)
        {
            CheckAmountIsGreaterThenZero(grossSalaryAmount);
            CheckAmountIsGreaterThenZero(higherSalaryAmount);

            return CalculateNetAmount(grossSalaryAmount, higherSalaryAmount);
        }

        private static void CheckAmountIsGreaterThenZero(decimal salaryAmount)
        {
            if (salaryAmount < 0)
            {
                throw new ArgumentException("Provided salary amount is not correct.");
            }
        }

        private decimal CalculateNetAmount(decimal grossSalaryAmount, decimal higherSalaryAmount)
        {
            if (_belowTaxSalaryAmount == 0 || _taxPercentage == 0)
            {
                return grossSalaryAmount;
            }

            decimal taxSum = 0m;

            if (grossSalaryAmount <= _belowTaxSalaryAmount)
            {
                return grossSalaryAmount;
            }
            decimal tax = CalculateTaxAmount(grossSalaryAmount);
            taxSum += tax;

            decimal taxableSalary = grossSalaryAmount;

            if (taxableSalary > higherSalaryAmount)
            {
                taxableSalary = higherSalaryAmount;
            }
            tax = CalculateTaxAmount(taxableSalary);
            taxSum += tax;

            return grossSalaryAmount - taxSum;
        }

        private decimal CalculateTaxAmount(decimal salaryAmount)
        {
            return (salaryAmount - _belowTaxSalaryAmount) * (_taxPercentage / 100);
        }
    }
}



留言

這個網誌中的熱門文章

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

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

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