原文地址:http://qinfanpeng.github.io/jekyll/update/2016/12/18/common_refactor_skills_part_two.html
[slide]
[slide]
[slide]
const calculatePrice = (order) => {
return basePrice(order) - quantityDiscount(order) + shipping(order)
}
const quantityDiscount = ({ itemPrice }) => {
return Math.max(0, quantity - 500) * itemPrice * 0.05
}
const shipping = function (order) {
return Math.min(100, basePrice(order) * 0.1)
}
[slide]
const QUANTITY_DISCOUNT_THRESHOLD = 500
const QUANTITY_DISCOUNT_RATE = 500
const SHIPPING_RATE = 0.1
const MIN_SHIPPING = 100
const quantityDiscount = ({ itemPrice }) => {
return Math.max(0, quantity - QUANTITY_DISCOUNT_THRESHOLD) * itemPrice * QUANTITY_DISCOUNT_RATE
}
var shipping = function (order) {
return Math.min(MIN_SHIPPING, basePrice(order) * SHIPPING_RATE)
}
[slide]
[note] 变化越频繁、使用范围越广,更应放到全局性的地方,甚至是配置文件;使用范围、变化越少,则可就近放置(比如当前文件顶部?) [/note]
[slide]
const shipping = function (order) {
return Math.min(100, basePrice(order) * 0.1)
}
determineLegendAlignment(model, ['top', 'topCenter'])
determineLegendAlignment(model, ['left', 'leftMiddle'])
determineLegendAlignment(model, ['right', 'rightMiddle', 'rightBottom'])
determineLegendAlignment(model, ['bottom', 'bottomCenter'])
[slide]
const calculateCharge = (date, quantity) => {
if (SUMMER_START <= date && date <= SUMMER_END) {
return quantity * SUMMER_RATE
} else {
return quantity * WINTER_RATE + WINTER_SERVICE_CHARGE
}
}
[slide]
const calculateCharge = (date, quantity) => {
if (SUMMER_START <= date && date <= SUMMER_END) {
return quantity * SUMMER_RATE
} else {
return quantity * WINTER_RATE + WINTER_SERVICE_CHARGE
}
}
const calculateCharge = (date, quantity) => {
return isInSummer(date) ? summerCharge(date) : winterCharge(date)
}
const isInSummer = (date) => SUMMER_START <= date && date <= SUMMER_END
const summerCharge = (quantity) => quantity * SUMMER_RATE
const winterCharge = (quantity) => quantity * WINTER_RATE + WINTER_SERVICE_CHARGE
[slide]
[magic data-transition="cover-circle"]
const calculatePayAmount = () => {
let result = 0.0
if (isDead()) {
result = deadAmount()
} else {
if (isSeparated()) {
result = separatedAmount()
} else {
if (isRetired()) {
result = retiredAmount()
} else {
result = normalPayAmount()
}
}
}
return result
}
====
单一出口原则
的束缚[/magic]
[slide]
[slide]
const calculatePayAmount = () => {
if (isDead()) return deadAmount()
if (isSeparated()) return separatedAmount()
// ...
// ...
// ...
[slide]
[slide]
[magic data-transition="cover-circle"]
const computeDisabilityAmount = () => {
if (isNotEligibleForDisability()) {
return 0.0
} else {
// compute disability amount
// ...
// ...
}
}
const isNotEligibleForDisability = () => {
return seniority < 0 || monthsDisabled > 12 || isPartTime()
}
====
const computeDisabilityAmount = () => {
if (isNotEligibleForDisability()) return 0.0
// compute disability amount
// compute disability amount
// ...
// ...
}
const isNotEligibleForDisability = () => {
return seniority < 0 || monthsDisabled > 12 || isPartTime()
}
[/magic]
[slide]
[magic data-transition="cover-circle"]
handleCellWithTooltip(fieldValue, field) {
if (this.props.needTooltipFieldNames.includes(field)) {
const tooltipId = uniqueId('fieldValue_')
return (
<span key={tooltipId}>
<div data-tip data-for={tooltipId}>{fieldValue}</div>
<Tooltip id={tooltipId} place='right'>
<div>{fieldValue}</div>
</Tooltip>
</span>
)
} else {
return <span key={uniqueId('cell_')}>{fieldValue}</span>
}
}
====
handleCellWithTooltip(fieldValue, field) {
if (!this.props.needTooltipFieldNames.includes(field)) return <span key={uniqueId('cell_')}>{fieldValue}</span>
const tooltipId = uniqueId('fieldValue_')
return (
<span key={tooltipId}>
<div data-tip data-for={tooltipId}>{fieldValue}</div>
<Tooltip id={tooltipId} place='right'>
<div>{fieldValue}</div>
</Tooltip>
</span>
)
}
[/magic]
[slide]
const calculatePayAmount = () => {
let result = 0.0
if (isDead()) {
result = deadAmount()
} else {
if (isSeparated()) {
result = separatedAmount()
} else {
if (isRetired()) {
result = retiredAmount()
} else {
result = normalPayAmount()
}
}
}
return result
}
const calculatePayAmount = () => {
if (isDead()) return deadAmount()
if (isSeparated()) return separatedAmount()
if (isRetired()) return retiredAmount()
return normalPayAmount()
}
[slide]
[magic data-transition="cover-circle"]
const getAdjustedCapital = () => {
const result = 0
if (capital > 0) {
if (intRate > 0 && duration > 0) {
result = (_income / _duration) * ADJ_FACTOR
}
}
return result
}
====
const getAdjustedCapital = () => {
if (capital <= 0) return 0
if (!(intRate > 0 && duration > 0)) return 0
return (_income / _duration) * ADJ_FACTOR
}
====
const getAdjustedCapital = () => {
if (capital <= 0) return 0
if (intRate <= 0 || duration <= 0)) return 0
return (_income / _duration) * ADJ_FACTOR
}
[/magic]
[slide]
[slide]
const computeDisabilityAmount = () => {
if (seniority < 0) return 0
if (monthsDisabled > 12) return 0
if (isPartTime()) return 0
// compute disability amount
}
[slide]
const computeDisabilityAmount = () => {
if (seniority < 0) return 0
if (monthsDisabled > 12) return 0
if (isPartTime()) return 0
// compute disability amount
}
const computeDisabilityAmount = () => {
if (seniority < 0 || monthsDisabled > 12 || isPartTime()) return 0
// compute disability amount
}
const computeDisabilityAmount = () => {
isNotEligibleForDisability() return 0
// compute disability amount
}
const isNotEligibleForDisability = () => {
return seniority < 0 || monthsDisabled > 12 || isPartTime()
}
[slide]
if (isSpecialDeal()) {
totalPrice = price * SPECIAL_DEAL_DISCOUNT_RATE
send()
} else {
totalPrice = price * NORMAL_DISCOUNT_RATE
send()
}
[slide]
if (isSpecialDeal()) {
totalPrice = price * SPECIAL_DEAL_DISCOUNT_RATE
send()
} else {
totalPrice = price * NORMAL_DISCOUNT_RATE
send()
}
if (isSpecialDeal()) {
totalPrice = price * SPECIAL_DEAL_DISCOUNT_RATE
} else {
totalPrice = price * NORMAL_DISCOUNT_RATE
}
send()
[slide]
handleMenuOnChange(selectedItem) {
if (this.props.length > 1) {
this.setState({
selectedMeasure:{
fieldId: selectedItem.value
}
})
}
else {
this.setState({
selectedMeasure: selectedItem.measure
})
}
}
handleMenuOnChange(selectedItem) {
const selectedMeasure = this.props.measures.length > 1 ? {fieldId: selectedItem.value} : selectedItem.measure
this.setState({ selectedMeasure })
}
[slide]
const checkSecurity = (people) => {
const miscreant = findMiscreant(people)
// doSomethingElse(miscreant)
}
const findMiscreant = (people) => {
let found = false
people.forEach(person => {
if (!found) {
if (person === 'Bob') {
sendAlert()
found = true
}
if (person === 'Jack') {
sendAlert()
found = true
}
}
})
}
[slide]
[magic data-transition="cover-circle"]
const findMiscreant = (people) => {
let found = false
people.forEach(person => {
if (!found) {
if (person === 'Bob') {
sendAlert()
found = true
}
if (person === 'Jack') {
sendAlert()
found = true
}
}
})
}
====
const findMiscreant = (people) => {
people.forEach(person => {
if (person === 'Bob') {
sendAlert()
break;
}
if (person === 'Jack') {
sendAlert()
break;
}
}
})
}
[/magic]
[slide]
[magic data-transition="cover-circle"]
const findMiscreant = (people) => {
people.forEach(person => {
if (person === 'Bob') {
sendAlert()
break;
}
if (person === 'Jack') {
sendAlert()
break;
}
}
})
}
====
const checkSecurity = (people) => {
const miscreant = findMiscreant(people)
if (!isUndefined(miscreant)) sendAlert(miscreant)
// doSomethingElse(miscreant)
}
const findMiscreant = (people) => {
const miscreants = ['Bob', 'Jack']
return find(people, person => miscreants.includes(person))
}
====
const findMiscreantAndSendAlert = (people) => {
people.forEach(person => {
if (!found) {
if (person === 'Bob') {
sendAlert(person)
return person
}
if (person === 'Jack') {
sendAlert(person)
return person
}
}
})
}
[/magic]
[slide]
const SearchCriteria = buildSearchCriteria(5, "0201485672")
const SearchCriteria = buildSearchCriteria(5, "0201485672", false)
buildSearchCriteria(5, "0201485672", false, 'JavaScript : The Good Parts')
const buildSearchCriteria = (authorId, ISBN, includeSoldOut, title) => {
// ...
}
[slide]
[magic data-transition="cover-circle"]
const buildSearchCriteria = ({ authorId, ISBN, includeSoldOut, title }) => {
// ...
}
const SearchCriteria = buildSearchCriteria({
authorId: 5,
ISBN: "0201485672",
includeSoldOut: false,
title: 'JavaScript : The Good Parts'
})
====
const **authorId** = 5
const ISBN = '0201485672'
const includeSoldOut = false
const title = 'JavaScript The Good Parts'
const SearchCriteria = buildSearchCriteria(authorId, ISBN, includeSoldOut, title)
[/magic]
[slide]
[slide]