跳转至

AI辅助重构与优化

🔧 利用AI进行代码重构和性能优化,提升代码质量和运行效率

📖 本章概述

代码重构和性能优化是提升软件质量的重要手段。本章将介绍如何利用AI工具识别重构机会、执行重构操作,以及进行性能分析和优化。

学习目标

  • 掌握AI辅助代码重构的方法
  • 学会识别和优化性能瓶颈
  • 理解重构的安全性和测试保障
  • 建立持续优化的工作流

🔄 代码重构

重构原则

Text Only
┌─────────────────────────────────────────────────────────────┐
│                    重构核心原则                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 小步前进                                                 │
│     - 每次只做一个小改动                                     │
│     - 频繁测试验证                                          │
│     - 保持代码可工作状态                                    │
│                                                             │
│  2. 测试保障                                                 │
│     - 重构前确保有测试                                      │
│     - 重构后运行测试                                        │
│     - 添加新测试覆盖变更                                    │
│                                                             │
│  3. 保持行为不变                                             │
│     - 重构不改变功能                                        │
│     - 只改善内部结构                                        │
│     - 接口保持兼容                                          │
│                                                             │
│  4. 消除重复                                                 │
│     - DRY原则(Don't Repeat Yourself)                      │
│     - 提取公共逻辑                                          │
│     - 使用设计模式                                          │
│                                                             │
└─────────────────────────────────────────────────────────────┘

常见重构场景

1. 提取函数/方法

Python
# ❌ 重构前:长函数,难以理解
def process_order(order):
    # 验证订单
    if not order.get('items'):
        return {'error': 'No items'}
    if not order.get('customer'):
        return {'error': 'No customer'}

    # 计算总价
    total = 0
    for item in order['items']:
        total += item['price'] * item['quantity']

    # 应用折扣
    if order['customer']['membership'] == 'vip':
        total *= 0.9
    elif order['customer']['membership'] == 'gold':
        total *= 0.95

    # 创建订单记录
    order_record = {
        'id': generate_id(),
        'customer_id': order['customer']['id'],
        'items': order['items'],
        'total': total,
        'status': 'pending',
        'created_at': datetime.now()
    }

    # 保存到数据库
    db.orders.insert(order_record)

    # 发送通知
    send_email(order['customer']['email'], 'Order Created', f'Order #{order_record["id"]}')

    return order_record

# ✅ 重构后:职责清晰,易于维护
def process_order(order: dict) -> dict:
    """处理订单主流程"""
    validate_order(order)
    total = calculate_total(order)
    order_record = create_order_record(order, total)
    save_order(order_record)
    notify_customer(order['customer'], order_record)
    return order_record

def validate_order(order: dict) -> None:
    """验证订单数据"""
    if not order.get('items'):
        raise ValidationError('No items in order')
    if not order.get('customer'):
        raise ValidationError('No customer information')

def calculate_total(order: dict) -> float:
    """计算订单总价"""
    subtotal = sum(
        item['price'] * item['quantity'] 
        for item in order['items']
    )
    return apply_discount(subtotal, order['customer']['membership'])

def apply_discount(amount: float, membership: str) -> float:
    """应用会员折扣"""
    discounts = {'vip': 0.9, 'gold': 0.95}
    return amount * discounts.get(membership, 1.0)

def create_order_record(order: dict, total: float) -> dict:
    """创建订单记录"""
    return {
        'id': generate_id(),
        'customer_id': order['customer']['id'],
        'items': order['items'],
        'total': total,
        'status': 'pending',
        'created_at': datetime.now()
    }

def save_order(order_record: dict) -> None:
    """保存订单到数据库"""
    db.orders.insert(order_record)

def notify_customer(customer: dict, order_record: dict) -> None:
    """发送订单通知"""
    send_email(
        customer['email'],
        'Order Created',
        f'Order #{order_record["id"]}'
    )

2. 消除重复代码

JavaScript
// ❌ 重构前:重复的验证逻辑
function createUser(userData) {
    if (!userData.email) throw new Error('Email required');
    if (!userData.password) throw new Error('Password required');
    if (userData.password.length < 8) throw new Error('Password too short');
    // ... 创建用户
}

function updateUser(userId, userData) {
    if (userData.email !== undefined && !userData.email) {
        throw new Error('Email required');
    }
    if (userData.password !== undefined) {
        if (!userData.password) throw new Error('Password required');
        if (userData.password.length < 8) throw new Error('Password too short');
    }
    // ... 更新用户
}

// ✅ 重构后:提取公共验证
function validateUserData(userData, isUpdate = false) {
    const { email, password } = userData;

    if (!isUpdate || email !== undefined) {
        if (!email) throw new Error('Email required');
    }

    if (!isUpdate || password !== undefined) {
        if (password !== undefined) {
            if (!password) throw new Error('Password required');
            if (password.length < 8) throw new Error('Password too short');
        }
    }
}

function createUser(userData) {
    validateUserData(userData);
    // ... 创建用户
}

function updateUser(userId, userData) {
    validateUserData(userData, true);
    // ... 更新用户
}

3. 简化条件逻辑

Python
# ❌ 重构前:复杂的嵌套条件
def get_shipping_cost(order):
    if order['total'] > 100:
        if order['customer']['membership'] == 'vip':
            return 0
        else:
            if order['destination'] == 'domestic':
                return 5
            else:
                return 15
    else:
        if order['customer']['membership'] == 'vip':
            if order['destination'] == 'domestic':
                return 0
            else:
                return 10
        else:
            if order['destination'] == 'domestic':
                return 10
            else:
                return 20

# ✅ 重构后:使用策略模式
def get_shipping_cost(order: dict) -> float:
    """计算运费"""
    rules = [
        VipFreeShipping(),      # VIP免运费
        HighValueDiscount(),    # 高额订单折扣
        DomesticShipping(),     # 国内运费
        InternationalShipping() # 国际运费
    ]

    for rule in rules:
        if rule.applies(order):
            return rule.calculate(order)

    return DEFAULT_SHIPPING

class VipFreeShipping:
    def applies(self, order):
        return order['customer']['membership'] == 'vip'

    def calculate(self, order):
        return 0 if order['total'] > 100 else (
            0 if order['destination'] == 'domestic' else 10
        )

AI辅助重构提示词

通用重构模板:

Markdown
请重构以下代码:

```[语言]
[原始代码]

重构目标: - [ ] 提高可读性 - [ ] 减少重复 - [ ] 改善命名 - [ ] 简化逻辑 - [ ] 添加类型注解

约束: - 保持功能不变 - 保持接口兼容 - 遵循[代码规范]

请提供: 1. 重构后的代码 2. 重构说明 3. 需要的测试更新

Text Only
**提取函数模板:**
```markdown
请将以下代码中可以提取为独立函数的部分提取出来:

[代码]

要求:
- 每个函数只做一件事
- 函数名清晰表达意图
- 添加类型注解和文档字符串
- 保持原有功能不变

⚡ 性能优化

性能分析维度

Text Only
┌─────────────────────────────────────────────────────────────┐
│                    性能优化维度                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  时间复杂度                                                  │
│  ├── 算法选择(O(n) vs O(n²))                              │
│  ├── 循环优化                                               │
│  └── 递归优化                                               │
│                                                             │
│  空间复杂度                                                  │
│  ├── 内存使用                                               │
│  ├── 数据结构选择                                           │
│  └── 缓存策略                                               │
│                                                             │
│  I/O性能                                                     │
│  ├── 数据库查询                                             │
│  ├── 网络请求                                               │
│  ├── 文件操作                                               │
│  └── 批量处理                                               │
│                                                             │
│  并发性能                                                    │
│  ├── 多线程/多进程                                          │
│  ├── 异步处理                                               │
│  ├── 连接池                                                 │
│  └── 负载均衡                                               │
│                                                             │
└─────────────────────────────────────────────────────────────┘

常见优化场景

1. 算法优化

Python
# ❌ 优化前:O(n²) 复杂度
def find_duplicates(items):
    duplicates = []
    for i in range(len(items)):
        for j in range(i + 1, len(items)):
            if items[i] == items[j] and items[i] not in duplicates:
                duplicates.append(items[i])
    return duplicates

# ✅ 优化后:O(n) 复杂度
def find_duplicates(items: list) -> list:
    """查找重复元素"""
    seen = set()
    duplicates = set()

    for item in items:
        if item in seen:
            duplicates.add(item)
        else:
            seen.add(item)

    return list(duplicates)

2. 数据库查询优化

Python
# ❌ 优化前:N+1查询问题
def get_users_with_orders(user_ids):
    users = User.query.filter(User.id.in_(user_ids)).all()
    result = []
    for user in users:
        orders = Order.query.filter_by(user_id=user.id).all()
        result.append({
            'user': user,
            'orders': orders
        })
    return result

# ✅ 优化后:使用JOIN或预加载
def get_users_with_orders(user_ids: list) -> list:
    """获取用户及其订单(优化版)"""
    # 方案1:使用JOIN
    results = db.session.query(User, Order)\
        .outerjoin(Order, User.id == Order.user_id)\
        .filter(User.id.in_(user_ids))\
        .all()

    # 方案2:使用预加载
    users = User.query\
        .options(joinedload(User.orders))\
        .filter(User.id.in_(user_ids))\
        .all()

    return [{'user': u, 'orders': u.orders} for u in users]

3. 缓存优化

Python
# ❌ 优化前:每次都查询数据库
def get_user_permissions(user_id):
    user = User.query.get(user_id)
    roles = Role.query.filter(Role.id.in_(user.role_ids)).all()
    permissions = []
    for role in roles:
        perms = Permission.query.filter_by(role_id=role.id).all()
        permissions.extend(perms)
    return list(set(permissions))

# ✅ 优化后:使用缓存
from functools import lru_cache
from datetime import timedelta
from cache import redis_cache

def get_user_permissions(user_id: int) -> list:
    """获取用户权限(带缓存)"""
    # 尝试从Redis缓存获取
    cache_key = f'user_permissions:{user_id}'
    cached = redis_cache.get(cache_key)
    if cached:
        return cached

    # 缓存未命中,查询数据库
    permissions = _fetch_user_permissions(user_id)

    # 存入缓存,5分钟过期
    redis_cache.set(cache_key, permissions, expire=timedelta(minutes=5))

    return permissions

def _fetch_user_permissions(user_id: int) -> list:
    """从数据库获取权限"""
    user = User.query.get(user_id)
    # ... 查询逻辑
    return permissions

4. 异步优化

Python
# ❌ 优化前:同步请求,总时间 = 所有请求时间之和
import requests

def fetch_all_users(user_ids):
    results = []
    for user_id in user_ids:
        response = requests.get(f'https://api.example.com/users/{user_id}')
        results.append(response.json())
    return results
# 100个用户,每个请求0.5秒 = 50秒

# ✅ 优化后:异步并发请求
import aiohttp
import asyncio

async def fetch_user(session, user_id):
    async with session.get(f'https://api.example.com/users/{user_id}') as response:
        return await response.json()

async def fetch_all_users(user_ids: list) -> list:
    """并发获取所有用户"""
    async with aiohttp.ClientSession() as session:
        tasks = [fetch_user(session, user_id) for user_id in user_ids]
        return await asyncio.gather(*tasks)
# 100个用户,并发请求 ≈ 1秒

AI辅助优化提示词

性能分析模板:

Markdown
请分析以下代码的性能问题:

```[语言]
[代码]

分析维度: 1. 时间复杂度分析 2. 空间复杂度分析 3. I/O操作效率 4. 潜在瓶颈

请提供: 1. 当前性能问题列表 2. 优化建议 3. 优化后的代码 4. 预期性能提升

Text Only
**数据库优化模板:**
```markdown
请优化以下数据库查询:

[SQL或ORM代码]

当前问题:
- [描述问题,如慢查询、N+1等]

数据库:[MySQL/PostgreSQL/MongoDB等]
数据量:[估计的数据规模]

请提供:
1. 问题分析
2. 优化方案(索引、查询重写等)
3. 优化后的代码

🛡️ 安全重构

重构安全检查清单

Markdown
## 重构前检查

- [ ] 有足够的测试覆盖
- [ ] 测试全部通过
- [ ] 理解代码的当前行为
- [ ] 识别依赖关系
- [ ] 创建备份/分支

## 重构中检查

- [ ] 小步修改
- [ ] 每步都运行测试
- [ ] 保持功能不变
- [ ] 更新相关文档
- [ ] 提交有意义的commit

## 重构后检查

- [ ] 所有测试通过
- [ ] 代码审查
- [ ] 性能没有退化
- [ ] 文档已更新
- [ ] 没有引入新依赖(或已评估)

AI辅助安全重构

Markdown
请帮我安全地重构以下代码:

[代码]

重构步骤:
1. 首先,帮我识别这段代码的所有依赖和影响范围
2. 建议需要的测试用例
3. 提供分步重构计划
4. 每步重构后的验证方法

约束:
- 必须保持向后兼容
- 不能破坏现有功能
- 需要渐进式重构

💡 最佳实践

重构时机

Text Only
┌─────────────────────────────────────────────────────────────┐
│                    何时进行重构                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ✅ 适合重构的情况                                           │
│  ├── 添加新功能前(清理相关代码)                            │
│  ├── 修复Bug时(改善代码结构)                              │
│  ├── 代码审查发现问题                                        │
│  ├── 性能不满足要求                                          │
│  ├── 代码难以理解                                            │
│  └── 重复代码出现                                            │
│                                                             │
│  ❌ 不适合重构的情况                                         │
│  ├── 临近发布截止日期                                        │
│  ├── 没有测试覆盖                                            │
│  ├── 不理解代码逻辑                                          │
│  ├── 重构成本高于重写                                        │
│  └── 紧急Bug修复                                             │
│                                                             │
└─────────────────────────────────────────────────────────────┘

优化优先级

Text Only
1. 正确性 > 性能
   - 先确保功能正确,再考虑优化

2. 可读性 > 微优化
   - 清晰的代码比快一点点更重要

3. 测量 > 猜测
   - 用profiler找到真正的瓶颈

4. 大局 > 局部
   - 优化最慢的部分,而不是所有部分

🎯 实战案例

案例1:重构遗留代码

场景: 重构一个复杂的订单处理函数

步骤:

  1. AI分析代码结构

    Markdown
    请分析这个函数的结构,识别可以提取的子功能:
    [粘贴长函数代码]
    

  2. 生成测试用例

    Markdown
    为这个函数生成完整的测试用例,确保重构后行为不变
    

  3. 分步重构

    Markdown
    请帮我分步重构这个函数:
    - 第一步:提取验证逻辑
    - 第二步:提取计算逻辑
    - 第三步:提取持久化逻辑
    - 第四步:提取通知逻辑
    

  4. 验证重构

    Markdown
    比较重构前后的代码,确认功能等价
    

案例2:性能优化

场景: 优化慢查询API

分析:

Markdown
请分析这个API的性能问题:

[API代码]
[数据库schema]
[查询日志]

数据量:100万用户,平均每个用户100条订单
当前响应时间:5-10秒
目标响应时间:<500ms

优化方案:

Markdown
基于分析,提供优化方案:
1. 数据库层面优化(索引、查询重写)
2. 应用层面优化(缓存、批量处理)
3. 架构层面优化(读写分离、分片)

📝 学习检查点

完成本章学习后,请确认你已掌握:

基础能力

  • 能够识别需要重构的代码
  • 掌握基本的重构技巧
  • 理解重构的安全原则

中级能力

  • 能够使用AI辅助重构
  • 掌握性能分析方法
  • 能够进行安全重构

高级能力

  • 能够设计重构策略
  • 掌握多种优化技巧
  • 能够处理复杂遗留代码

🔗 相关资源


下一章: 08-AI辅助文档与注释 - 学习文档自动生成技巧