经多处比较,发现还算 ThoughtWorks 和 金数据 的小伙伴比较喜欢追求优雅的代码。http://qinfanpeng.github.io/jekyll/update/2016/12/15/common_refactor_skills_part_one.html
[slide]
[slide]
[slide]
const calculatePrice = ({ itemPrice, quantity }) => {
// price is base price - quantity discount + shipping
return itemPrice * quantity -
Math.max(0, quantity - 500) * itemPrice * 0.05 +
Math.min(100, itemPrice * quantity * 0.1)
}
[slide]
const calculatePrice = ({ itemPrice, quantity }) => {
// price is base price - quantity discount + shipping
return itemPrice * quantity -
Math.max(0, quantity - 500) * itemPrice * 0.05 +
Math.min(100, itemPrice * quantity * 0.1)
}
const calculatePrice = ({ itemPrice, quantity }) => {
const basePrice = itemPrice * quantity
const quantityDiscount = Math.max(0, quantity - 500) * itemPrice * 0.05
const shipping = Math.min(100, basePrice * 0.1)
return basePrice - quantityDiscount + shipping
}
[note] Demo 好处?
[slide]
const calculatePrice = ({ itemPrice, quantity }) => {
const basePrice = itemPrice * quantity
const quantityDiscount = Math.max(0, quantity - 500) * itemPrice * 0.05
const shipping = Math.min(100, basePrice * 0.1)
return basePrice - quantityDiscount + shipping
}
[slide]
const calculatePrice = ({ itemPrice, quantity }) => {
const basePrice = itemPrice * quantity
const quantityDiscount = Math.max(0, quantity - 500) * itemPrice * 0.05
const shipping = Math.min(100, basePrice * 0.1)
return basePrice - quantityDiscount + shipping
}
const calculatePrice = (order) => {
return basePrice(order) - quantityDiscount(order) + shipping(order)
}
const basePrice = ({ itemPrice, quantity }) => {
return itemPrice * quantity
}
const quantityDiscount = ({ itemPrice, quantity }) => {
return Math.max(0, quantity - 500) * itemPrice * 0.05
}
var shipping = function (order) {
return Math.min(100, basePrice(order) * 0.1)
}
[note] 好处?
[slide]
[slide]
const isBigDeal = (order) => {
const basePrice = order.basePrice
return basePrice > 1000
}
[slide]
const isBigDeal = (order) => {
const basePrice = order.basePrice
return basePrice > 1000
}
const isBigDeal = (order) => {
return order.basePrice > 1000
}
[slide]
let temp = 2 * height * width
console.log('perimeter: ', temp)
temp = height * width
console.log('area: ', temp)
[slide]
let temp = 2 * height * width
console.log('perimeter: ', temp)
temp = height * width
console.log('area: ', temp)
const perimeter = 2 * height * width
console.log('perimeter: ', perimeter)
const area = height * width
console.log('area: ', area)
[slide]
const users = [
{ user: 'barney', age: 36, active: true },
{ user: 'fred', age: 40, active: false }
]
const activeUsers = []
users.each(user => {
if (user.active) {
activeUsers.push(user)
}
})
Parallelize
[slide]
const users = [
{ user: 'barney', age: 36, active: true },
{ user: 'fred', age: 40, active: false }
]
const activeUsers = []
users.each(user => {
if (user.active) {
activeUsers.push(user)
}
})
const activeUsers = filter(users, user => user.active)
const activeUsers = filter(users, 'active')
[slide]
const users = [
{ user: 'barney', age: 36, active: true },
{ user: 'fred', age: 40, active: false }
]
[magic data-transition="cover-circle"]
let activeUser = null
users.each(user => {
if (user.active) {
activeUser = user
break
}
})
====
let totalAge = 0
users.each(user => {
if (user.active) {
totalAge += user.age
}
})
[/magic]
[slide]
const discount = (inputVal, quantity) => {
if (inputVal > 50) inputVal -= 2
// ...
return inputVal
}
javascript
const aMethod = (aObj) => {
aObj.modifyInSomeWay() // That's ok
aObj = anotherObj // Bad
}
[slide]
const discount = (inputVal, quantity) => {
if (inputVal > 50) inputVal -= 2
// ...
return inputVal
}
const discount = (basePrice, quantity) => {
const finalPrice = basePrice
if (basePrice > 50) finalPrice -= 2
// ...
return finalPrice
}
[slide]
const printOwing = (orders) => {
let outstanding = 0.0
// print banner
console.log('*********************************')
console.log('********* Customer Owes *********')
console.log('*********************************')
for (let i = 0; i < orders.length; i++) {
outstanding += orders[i].amount
}
// print details
console.log('name: ', name)
console.log('amount: ', outstanding)
}
[slide]
const printOwing = (orders) => {
printBanner()
// print details
console.log('name: ', name)
console.log('amount: ', outstanding)
}
const printOwing = (orders) => {
printBanner()
const outstanding = calculateOutstanding(orders)
printDetails(outstanding)
}
const printBanner = () => {
console.log('*********************************')
console.log('********* Customer Owes *********')
console.log('*********************************')
}
var calculateOutstanding = function (orders) {
return sumBy(orders, 'amount')
}
const printDetails = function (outstanding) {
console.log('name: ', name)
console.log('amount: ', outstanding)
}
[slide]
const getRating = () => {
return moreThanFiveNegativeFeedbacks() ? 1 : 2
}
const moreThanFiveNegativeFeedbacks = () => {
return this.negativeFeedbacks.length > 5
}
const getRating = () => {
return this.negativeFeedbacks.length > 5 ? 1 : 2
}
[slide]
[slide]
[slide]