领域专家

重构顾问 AI 智能体

技术债务与代码异味专家

专注于识别代码异味、技术债务和可维护性问题的 AI 智能体, 这些问题会随着代码库的增长而造成更大的麻烦。在问题累积之前发现结构性问题。

重构顾问能发现什么

使代码难以维护、扩展或理解的结构性问题

代码异味

表明代码库设计存在更深层问题的结构性问题

长方法(>50行)大型类(>500行)深层嵌套(>4层)参数列表过长(>5个)

设计问题

使代码难以修改和测试的面向对象设计问题

上帝对象特性嫉妒不恰当的亲密关系基本类型偏执

SOLID 违规

违反基本设计原则,导致代码僵化和脆弱

单一职责开闭原则里氏替换依赖倒置

高复杂度

过于复杂的代码路径,难以理解、测试或安全维护

圈复杂度 >10复杂条件表达式难以跟踪的控制流

代码异味阈值

指示代码需要重构的客观标准

方法长度

> 50 行

方法应该能在一屏内显示

类大小

> 500 行

类应该有单一职责

参数数量

> 5 个参数

考虑使用选项对象

嵌套深度

> 4 层

提取函数或使用提前返回

圈复杂度

> 10

路径太多,无法可靠测试

重复代码块

> 6 行

提取到共享函数

重构模式实战

了解重构顾问如何识别问题并建议具体的应用模式

提取方法模式

之前
function processOrder(order) {
  // 验证订单
  if (!order.items || order.items.length === 0) {
    throw new Error('订单为空')
  }
  if (!order.customer) {
    throw new Error('没有客户')
  }

  // 计算总额
  let subtotal = 0
  for (const item of order.items) {
    subtotal += item.price * item.quantity
  }
  const tax = subtotal * 0.1
  const total = subtotal + tax

  // 应用折扣
  let discount = 0
  if (order.customer.isPremium) {
    discount = total * 0.15
  } else if (subtotal > 100) {
    discount = total * 0.05
  }

  return total - discount
}

方法做了4件事:验证、计算、税费、折扣

之后
function processOrder(order) {
  validateOrder(order)
  const subtotal = calculateSubtotal(order.items)
  const total = applyTax(subtotal)
  return applyDiscount(total, order.customer)
}

function validateOrder(order) {
  if (!order.items?.length) throw new Error('订单为空')
  if (!order.customer) throw new Error('没有客户')
}

function calculateSubtotal(items) {
  return items.reduce((sum, item) =>
    sum + item.price * item.quantity, 0)
}

提取方法:每个函数只做好一件事

用多态替换条件语句

之前
function calculateShipping(order) {
  switch (order.shippingType) {
    case 'express':
      return order.weight * 5.0 + 15
    case 'standard':
      return order.weight * 2.5 + 5
    case 'economy':
      return order.weight * 1.0
    case 'freight':
      return order.weight * 0.5 + 50
    default:
      throw new Error('未知类型')
  }
}

Switch 语句随着每种新配送类型而增长

之后
interface ShippingStrategy {
  calculate(weight: number): number
}

const shippingStrategies: Record<string, ShippingStrategy> = {
  express: { calculate: w => w * 5.0 + 15 },
  standard: { calculate: w => w * 2.5 + 5 },
  economy: { calculate: w => w * 1.0 },
  freight: { calculate: w => w * 0.5 + 50 },
}

function calculateShipping(order) {
  const strategy = shippingStrategies[order.shippingType]
  if (!strategy) throw new Error('未知类型')
  return strategy.calculate(order.weight)
}

策略模式:无需修改现有代码即可添加新类型

消除特性嫉妒

之前
class OrderProcessor {
  calculateTax(order) {
    const subtotal = order.items.reduce((sum, item) =>
      sum + item.price * item.quantity, 0)
    const taxRate = order.customer.region === 'EU'
      ? order.customer.country.vatRate
      : 0.0
    return subtotal * taxRate
  }
}

OrderProcessor 对 Order 和 Customer 的内部了解过多

之后
class Order {
  calculateSubtotal() {
    return this.items.reduce((sum, item) =>
      sum + item.price * item.quantity, 0)
  }

  calculateTax() {
    return this.calculateSubtotal() * this.customer.getTaxRate()
  }
}

class Customer {
  getTaxRate() {
    return this.region === 'EU' ? this.country.vatRate : 0.0
  }
}

将行为移至拥有数据的类

深度分析

重构顾问如何工作

重构顾问不仅仅测量指标——它理解设计模式并建议 能够改善代码结构的具体重构技术。

模式识别

识别代码结构中的代码异味和反模式

重构配方

建议具体模式:提取方法、替换条件语句、移动方法

SOLID 原则

检查是否遵循 SRP、OCP、LSP、ISP 和 DIP

分析流程

1

测量指标

计算方法/类长度和复杂度评分

2

识别模式

检测代码异味和设计反模式

3

检查 SOLID

验证是否遵循设计原则

4

建议重构

推荐具体模式,如提取方法、用多态替换条件语句

优先级排序的发现

专注于会导致维护问题的问题,而不是风格细节

最高

重复逻辑

必须在多处修改的复制粘贴代码

上帝对象

做太多事情、无法测试的类

高复杂度

路径太多、无法可靠测试的函数

较低

风格问题

优先处理结构性问题

技术债务呈指数增长。
重构顾问帮您尽早发现。

在技术债务累积之前
阻止它

让重构顾问在结构性问题累积之前发现它们。 免费试用 14 天,无需信用卡。

代码异味检测
SOLID 违规检查
重构模式建议