243 lines
5.7 KiB
Markdown
243 lines
5.7 KiB
Markdown
|
|
# Clean Code 优化 - 最近提交的文件
|
|||
|
|
|
|||
|
|
You are running a development task using the **clean-code skill** to optimize recently modified files.
|
|||
|
|
|
|||
|
|
## 任务目标
|
|||
|
|
|
|||
|
|
使用 Clean Code 原则优化最近提交的前后端文件,提升代码质量和可维护性。
|
|||
|
|
|
|||
|
|
## 参考文件
|
|||
|
|
|
|||
|
|
- **Clean Code 技能**: `.agents/skills/clean-code/SKILL.md` (必读)
|
|||
|
|
- **Flutter 风格指南**: Effective Dart
|
|||
|
|
- **Java 风格指南**: 阿里巴巴 Java 开发手册
|
|||
|
|
|
|||
|
|
## 需要优化的文件
|
|||
|
|
|
|||
|
|
### Flutter 前端文件 (10个)
|
|||
|
|
|
|||
|
|
**核心文件**:
|
|||
|
|
1. `flutter_monisuo/lib/core/theme/app_color_scheme.dart` - 颜色系统
|
|||
|
|
2. `flutter_monisuo/lib/ui/pages/home/home_page.dart` - 首页
|
|||
|
|
3. `flutter_monisuo/lib/ui/pages/main/main_page.dart` - 主页面
|
|||
|
|
4. `flutter_monisuo/lib/ui/pages/market/market_page.dart` - 行情页面
|
|||
|
|
5. `flutter_monisuo/lib/ui/pages/trade/trade_page.dart` - 交易页面
|
|||
|
|
6. `flutter_monisuo/lib/ui/pages/asset/asset_page.dart` - 资产页面
|
|||
|
|
7. `flutter_monisuo/lib/ui/pages/mine/mine_page.dart` - 我的页面
|
|||
|
|
|
|||
|
|
**组件文件**:
|
|||
|
|
8. `flutter_monisuo/lib/ui/components/asset_card.dart` - 资产卡片
|
|||
|
|
9. `flutter_monisuo/lib/ui/components/glass_panel.dart` - 毛玻璃面板
|
|||
|
|
10. `flutter_monisuo/lib/ui/components/neon_glow.dart` - 霓虹光效
|
|||
|
|
|
|||
|
|
### Java 后端文件 (可选)
|
|||
|
|
|
|||
|
|
如果发现 Java 文件需要优化,也可以一并处理。
|
|||
|
|
|
|||
|
|
## 优化重点
|
|||
|
|
|
|||
|
|
### 1. 有意义的命名
|
|||
|
|
```dart
|
|||
|
|
// ❌ 坏
|
|||
|
|
final d = DateTime.now();
|
|||
|
|
final c = colorScheme;
|
|||
|
|
|
|||
|
|
// ✅ 好
|
|||
|
|
final currentDate = DateTime.now();
|
|||
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2. 函数优化
|
|||
|
|
```dart
|
|||
|
|
// ❌ 坏 - 函数太长
|
|||
|
|
Widget build(BuildContext context) {
|
|||
|
|
// 100+ 行代码...
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ✅ 好 - 拆分为小函数
|
|||
|
|
Widget build(BuildContext context) {
|
|||
|
|
return Column(
|
|||
|
|
children: [
|
|||
|
|
_buildHeader(context),
|
|||
|
|
_buildContent(context),
|
|||
|
|
_buildFooter(context),
|
|||
|
|
],
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Widget _buildHeader(BuildContext context) { ... }
|
|||
|
|
Widget _buildContent(BuildContext context) { ... }
|
|||
|
|
Widget _buildFooter(BuildContext context) { ... }
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3. 减少嵌套
|
|||
|
|
```dart
|
|||
|
|
// ❌ 坏 - 嵌套太深
|
|||
|
|
if (user != null) {
|
|||
|
|
if (user.isActive) {
|
|||
|
|
if (user.hasPermission) {
|
|||
|
|
// ...
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ✅ 好 - 提前返回
|
|||
|
|
if (user == null) return;
|
|||
|
|
if (!user.isActive) return;
|
|||
|
|
if (!user.hasPermission) return;
|
|||
|
|
|
|||
|
|
// ...
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4. 提取常量
|
|||
|
|
```dart
|
|||
|
|
// ❌ 坏 - 魔法数字
|
|||
|
|
padding: EdgeInsets.all(16),
|
|||
|
|
borderRadius: BorderRadius.circular(12),
|
|||
|
|
|
|||
|
|
// ✅ 好 - 使用常量
|
|||
|
|
padding: EdgeInsets.all(AppSpacing.md),
|
|||
|
|
borderRadius: BorderRadius.circular(AppRadius.lg),
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 5. 移除重复代码
|
|||
|
|
```dart
|
|||
|
|
// ❌ 坏 - 重复代码
|
|||
|
|
Text('首页', style: TextStyle(fontSize: 12, color: colorScheme.primary)),
|
|||
|
|
Text('行情', style: TextStyle(fontSize: 12, color: colorScheme.primary)),
|
|||
|
|
|
|||
|
|
// ✅ 好 - 提取为函数
|
|||
|
|
Text('首页', style: _labelStyle(context)),
|
|||
|
|
Text('行情', style: _labelStyle(context)),
|
|||
|
|
|
|||
|
|
TextStyle _labelStyle(BuildContext context) {
|
|||
|
|
return TextStyle(
|
|||
|
|
fontSize: 12,
|
|||
|
|
color: Theme.of(context).colorScheme.primary,
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 6. 添加必要注释
|
|||
|
|
```dart
|
|||
|
|
// ❌ 坏 - 多余注释
|
|||
|
|
// 设置文字颜色
|
|||
|
|
color: colorScheme.primary
|
|||
|
|
|
|||
|
|
// ✅ 好 - 解释为什么
|
|||
|
|
// 使用 primary 颜色而不是 onSurface,确保在浅色主题下也有足够对比度
|
|||
|
|
color: colorScheme.primary
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 执行规则
|
|||
|
|
|
|||
|
|
1. **逐个文件优化** - 不要批量修改
|
|||
|
|
2. **保持功能不变** - 只优化代码结构,不改变逻辑
|
|||
|
|
3. **保持中文字符** - 不要修改任何中文文字
|
|||
|
|
4. **使用 AppSpacing 和 AppRadius** - 替换硬编码的数字
|
|||
|
|
5. **拆分大函数** - 函数超过 50 行就拆分
|
|||
|
|
6. **减少嵌套层级** - 不超过 3 层嵌套
|
|||
|
|
7. **提取重复代码** - DRY 原则
|
|||
|
|
|
|||
|
|
## 优化流程
|
|||
|
|
|
|||
|
|
### Phase 1: Flutter 核心页面 (60分钟)
|
|||
|
|
|
|||
|
|
**优先级 P0**:
|
|||
|
|
1. `home_page.dart` - 首页
|
|||
|
|
2. `main_page.dart` - 主页面
|
|||
|
|
3. `mine_page.dart` - 我的页面
|
|||
|
|
|
|||
|
|
**优先级 P1**:
|
|||
|
|
4. `market_page.dart` - 行情页面
|
|||
|
|
5. `trade_page.dart` - 交易页面
|
|||
|
|
6. `asset_page.dart` - 资产页面
|
|||
|
|
|
|||
|
|
**每个文件优化步骤**:
|
|||
|
|
- 检查函数长度,拆分大函数
|
|||
|
|
- 检查嵌套层级,减少嵌套
|
|||
|
|
- 提取重复代码
|
|||
|
|
- 使用 AppSpacing/AppRadius 替换魔法数字
|
|||
|
|
- 优化命名
|
|||
|
|
- 添加必要注释
|
|||
|
|
|
|||
|
|
### Phase 2: 组件文件 (30分钟)
|
|||
|
|
|
|||
|
|
7. `asset_card.dart`
|
|||
|
|
8. `glass_panel.dart`
|
|||
|
|
9. `neon_glow.dart`
|
|||
|
|
10. `app_color_scheme.dart`
|
|||
|
|
|
|||
|
|
### Phase 3: 测试验证 (15分钟)
|
|||
|
|
|
|||
|
|
11. **运行代码分析**
|
|||
|
|
```bash
|
|||
|
|
flutter analyze
|
|||
|
|
```
|
|||
|
|
- 确保 0 errors
|
|||
|
|
|
|||
|
|
12. **测试应用**
|
|||
|
|
```bash
|
|||
|
|
flutter run -d chrome
|
|||
|
|
```
|
|||
|
|
- 验证所有功能正常
|
|||
|
|
- 验证主题切换正常
|
|||
|
|
|
|||
|
|
### Phase 4: 提交代码 (10分钟)
|
|||
|
|
|
|||
|
|
13. **提交优化后的代码**
|
|||
|
|
```bash
|
|||
|
|
git add .
|
|||
|
|
git commit -m "refactor: 使用 Clean Code 原则优化代码质量
|
|||
|
|
|
|||
|
|
- 拆分大函数为小函数
|
|||
|
|
- 减少嵌套层级
|
|||
|
|
- 提取重复代码
|
|||
|
|
- 使用 AppSpacing/AppRadius 替换魔法数字
|
|||
|
|
- 优化命名和添加注释
|
|||
|
|
- 提升代码可读性和可维护性"
|
|||
|
|
|
|||
|
|
git push
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## Clean Code 检查清单
|
|||
|
|
|
|||
|
|
每个文件优化后,检查以下项:
|
|||
|
|
|
|||
|
|
- [ ] 函数长度 < 50 行
|
|||
|
|
- [ ] 嵌套层级 ≤ 3 层
|
|||
|
|
- [ ] 没有魔法数字
|
|||
|
|
- [ ] 没有重复代码
|
|||
|
|
- [ ] 命名清晰有意义
|
|||
|
|
- [ ] 添加了必要注释
|
|||
|
|
- [ ] 单一职责原则
|
|||
|
|
- [ ] DRY 原则
|
|||
|
|
|
|||
|
|
## 注意事项
|
|||
|
|
|
|||
|
|
### ⚠️ 必须保持
|
|||
|
|
- ✅ 所有功能不变
|
|||
|
|
- ✅ 所有中文字符不变
|
|||
|
|
- ✅ 所有 API 调用不变
|
|||
|
|
- ✅ 主题切换功能正常
|
|||
|
|
|
|||
|
|
### ❌ 禁止操作
|
|||
|
|
- ❌ 修改业务逻辑
|
|||
|
|
- ❌ 修改 API 接口
|
|||
|
|
- ❌ 修改数据模型
|
|||
|
|
- ❌ 删除现有功能
|
|||
|
|
- ❌ 修改中文文字
|
|||
|
|
|
|||
|
|
## 完成标准
|
|||
|
|
|
|||
|
|
- ✅ 所有文件优化完成
|
|||
|
|
- ✅ flutter analyze 0 errors
|
|||
|
|
- ✅ 所有功能正常
|
|||
|
|
- ✅ 代码可读性提升
|
|||
|
|
- ✅ 代码可维护性提升
|
|||
|
|
- ✅ 所有中文文字保持不变
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
**开始执行吧!让代码更干净、更优雅!** 🧹✨
|