Created
August 19, 2021 19:03
-
-
Save jeftarmascarenhas/aabbee742b0d894a0b62ed500ce5c4e2 to your computer and use it in GitHub Desktop.
Refactoring Extract Method(Solution 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LongMethod { | |
quantity: number = 10 | |
itemPrice: number = 34.0 | |
calculateTotal() { | |
const basePrice = this.quantity * this.itemPrice | |
if (basePrice > 500) { | |
return basePrice * 0.95 | |
} | |
return basePrice * 0.98 | |
} | |
} | |
const longMethodExample = new LongMethod() | |
console.log(longMethodExample.calculateTotal()) | |
class ExtractMethod { | |
quantity: number = 10 | |
itemPrice: number = 34.0 | |
calculateTotal() { | |
if (this.basePrice() > 500) { | |
return this.basePrice() * 0.95 | |
} | |
return this.basePrice() * 0.98 | |
} | |
basePrice(): number { | |
return this.quantity * this.itemPrice | |
} | |
} | |
const extractMethodExample = new LongMethod() | |
console.log(extractMethodExample.calculateTotal()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment