Skip to content

Instantly share code, notes, and snippets.

@jeftarmascarenhas
Created August 19, 2021 19:03
Show Gist options
  • Save jeftarmascarenhas/aabbee742b0d894a0b62ed500ce5c4e2 to your computer and use it in GitHub Desktop.
Save jeftarmascarenhas/aabbee742b0d894a0b62ed500ce5c4e2 to your computer and use it in GitHub Desktop.
Refactoring Extract Method(Solution 2)
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