Loading...
Loading...
Compare original and translation side by side
// Before:
function processOrder(order) {
// 50 lines of code
}
// After:
function processOrder(order) {
validateOrder(order);
calculateTotals(order);
saveOrder(order);
sendConfirmation(order);
}// 重构前:
function processOrder(order) {
// 50行代码
}
// 重构后:
function processOrder(order) {
validateOrder(order);
calculateTotals(order);
saveOrder(order);
sendConfirmation(order);
}// Before:
class UserService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
class AdminService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
// After:
class EmailValidator {
async validate(email) {
if (!email || !email.includes('@')) return false;
return email.split('@')[1].length > 0;
}
}// 重构前:
class UserService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
class AdminService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
// 重构后:
class EmailValidator {
async validate(email) {
if (!email || !email.includes('@')) return false;
return email.split('@')[1].length > 0;
}
}// Before:
class User {
// Authentication
// Profile management
// Notifications
// Reporting
}
// After:
class User { /* Core user data */ }
class UserAuth { /* Authentication */ }
class UserProfile { /* Profile management */ }
class UserNotifier { /* Notifications */ }// 重构前:
class User {
// 认证功能
// 资料管理
// 通知功能
// 报表功能
}
// 重构后:
class User { /* 核心用户数据 */ }
class UserAuth { /* 认证功能 */ }
class UserProfile { /* 资料管理 */ }
class UserNotifier { /* 通知功能 */ }// Before:
function createUser(name, email, age, address, phone, role) { ... }
// After:
function createUser(user: UserData) { ... }
interface UserData {
name: string;
email: string;
age: number;
address: string;
phone: string;
role: string;
}// 重构前:
function createUser(name, email, age, address, phone, role) { ... }
// 重构后:
function createUser(user: UserData) { ... }
interface UserData {
name: string;
email: string;
age: number;
address: string;
phone: string;
role: string;
}// Before:
class Order {
calculatePrice(customer) {
const discount = customer.getDiscountLevel();
// ...
}
}
// After:
class Customer {
calculatePriceForOrder(order) {
const discount = this.discountLevel;
// ...
}
}// 重构前:
class Order {
calculatePrice(customer) {
const discount = customer.getDiscountLevel();
// ...
}
}
// 重构后:
class Customer {
calculatePriceForOrder(order) {
const discount = this.discountLevel;
// ...
}
}// Before:
function drawShape(x, y, width, height) { ... }
function moveShape(x, y, width, height, dx, dy) { ... }
// After:
class Rectangle {
constructor(x, y, width, height) { ... }
}
function drawShape(rect: Rectangle) { ... }// 重构前:
function drawShape(x, y, width, height) { ... }
function moveShape(x, y, width, height, dx, dy) { ... }
// 重构后:
class Rectangle {
constructor(x, y, width, height) { ... }
}
function drawShape(rect: Rectangle) { ... }// Before:
function createUser(name, email, phone) { ... }
// After:
class Email {
constructor(value) {
if (!this.isValid(value)) throw new Error('Invalid email');
this.value = value;
}
// ...
}// 重构前:
function createUser(name, email, phone) { ... }
// 重构后:
class Email {
constructor(value) {
if (!this.isValid(value)) throw new Error('Invalid email');
this.value = value;
}
// ...
}// Before:
function calculatePay(employee) {
switch (employee.type) {
case 'engineer': return employee.salary * 1.2;
case 'manager': return employee.salary * 1.5;
case 'sales': return employee.salary * 1.1;
}
}
// After:
interface Employee {
calculatePay(): number;
}
class Engineer implements Employee {
calculatePay() { return this.salary * 1.2; }
}// 重构前:
function calculatePay(employee) {
switch (employee.type) {
case 'engineer': return employee.salary * 1.2;
case 'manager': return employee.salary * 1.5;
case 'sales': return employee.salary * 1.1;
}
}
// 重构后:
interface Employee {
calculatePay(): number;
}
class Engineer implements Employee {
calculatePay() { return this.salary * 1.2; }
}// Before:
class User {
calculateRefund() {
this.tempRefundAmount = 0;
// complex calculation
return this.tempRefundAmount;
}
}
// After:
class RefundCalculator {
calculate(user) {
// ...
}
}// 重构前:
class User {
calculateRefund() {
this.tempRefundAmount = 0;
// 复杂计算逻辑
return this.tempRefundAmount;
}
}
// 重构后:
class RefundCalculator {
calculate(user) {
// ...
}
}// Before:
// Calculate the total price including discounts
// and tax based on user location
function calc(u, i) {
let t = 0;
// discount logic
if (u.vip) t *= 0.9;
// tax logic
if (u.state === 'CA') t *= 1.08;
return t;
}
// After:
function calculateTotalPrice(user: User, items: Item[]): number {
let total = items.sum(i => i.price);
if (user.isVIP) {
total = applyVIPDiscount(total);
}
return applyTax(total, user.state);
}// 重构前:
// 计算包含折扣和基于用户所在地的税费的总价
function calc(u, i) {
let t = 0;
// 折扣逻辑
if (u.vip) t *= 0.9;
// 税费逻辑
if (u.state === 'CA') t *= 1.08;
return t;
}
// 重构后:
function calculateTotalPrice(user: User, items: Item[]): number {
let total = items.sum(i => i.price);
if (user.isVIP) {
total = applyVIPDiscount(total);
}
return applyTax(total, user.state);
}references/smells.mdreferences/techniques.mdreferences/checklist.mdreferences/smells.mdreferences/techniques.mdreferences/checklist.md