Files
NewStock/.kilocode/rules/requirement_review.md

184 lines
5.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 代码修改需求审查规则
本规则定义了代码修改前的需求审查流程,确保修改工作符合项目需求,避免引入不必要的变更。
## 规则目的
- 规范代码修改流程,确保修改符合需求
- 避免在修改过程中引入与任务无关的功能变更
- 提高代码修改的可控性和可追溯性
## 修改前检查
### 需求文件检查
在修改任何代码文件前,必须执行以下检查:
1. **检查需求md文件**
- 检查目标模块目录下是否存在需求md文件
- 匹配模式:`*.md``requirement*.md`
- 常见命名:`requirement.md``requirement_xxx.md``模块名_requirement.md`
2. **阅读并理解需求**
- 如果存在需求md文件必须先阅读并理解其内容
- 明确任务目标和修改范围
- 记录关键需求点
3. **不存在需求文件**
- 如果不存在需求md文件继续正常修改流程
- 仍需与用户确认修改范围和目标
### 检查示例
```python
import os
def check_requirement_file(module_path):
"""检查模块目录下是否存在需求md文件"""
md_files = [f for f in os.listdir(module_path)
if f.endswith('.md') or f.lower().startswith('requirement')]
if md_files:
print(f"发现需求文件: {md_files}")
return True
return False
# 推荐做法
def before_modify_code(module_path):
"""修改代码前的检查流程"""
if check_requirement_file(module_path):
print("请先阅读需求md文件并理解需求内容")
# 读取并分析需求文件
# 确认修改范围和目标
else:
print("未发现需求文件,继续正常流程")
```
## 修改范围控制
### 严格遵守需求范围
- **只修改任务要求的功能相关代码**
- **禁止修改与任务要求无关的功能代码**
- **禁止在修改过程中引入意外的功能变更**
### 范围控制原则
| 场景 | 正确做法 | 错误做法 |
|------|----------|----------|
| 修改函数A | 只修改函数A相关的代码 | 同时修改函数B、C |
| 添加新功能 | 在指定位置添加 | 在其他地方添加辅助功能 |
| 修复bug | 只修复目标bug | 顺便优化其他代码 |
| 重构代码 | 只重构必要部分 | 大规模重构无关模块 |
### 示例说明
```python
# 任务要求:修复 get_factor 函数中的除零错误
# 正确做法:只修复除零问题,不修改其他内容
def get_factor(df, window=5):
epsilon = 1e-8 # 防止除零
result = df["close"] / (df["high"] - df["low"] + epsilon)
return result
# 错误做法:在修复过程中引入了不必要的变更
def get_factor(df, window=5):
# 不应该添加日志功能(除非需求要求)
logger.info(f"Calculating factor with window={window}")
epsilon = 1e-8
result = df["close"] / (df["high"] - df["low"] + epsilon)
# 不应该修改返回值类型(除非需求要求)
return {"factor": result, "window": window}
```
### 变更影响评估
在修改过程中,如果发现可能影响其他功能的改动:
1. **暂停当前修改**
2. **评估影响范围**
3. **与用户确认**:说明影响和变更原因
4. **获得许可**:确认是否继续修改
```python
# 发现可能影响其他功能的示例
def modify_function():
"""修改函数时发现可能影响其他功能"""
potential_impact = True
if potential_impact:
# 暂停修改,与用户确认
print("发现可能影响其他功能的变更,请确认是否继续")
# 等待用户确认后再执行修改
```
## 例外情况
以下情况可以进行小幅调整,但需在提交前说明:
### 1. 语法错误和拼写问题
```python
# 允许的修复
def caculate_factor(): # 拼写错误
return "calculated" # 修正为 calculate
# 允许
def calculate_factor():
return "calculated"
```
### 2. 代码格式化调整
```python
# 为了代码格式化一致性可以进行格式调整
# 之前
def example_function(df,parameter1,parameter2):
result=df["col"]/(df["high"]-df["low"]+epsilon)
return result
# 之后(格式调整)
def example_function(df, parameter1, parameter2):
result = df["col"] / (df["high"] - df["low"] + epsilon)
return result
```
### 3. 明显的性能优化
```python
# 明显的性能优化可以进行
# 优化前
def slow_calculation(df):
result = []
for i in range(len(df)):
result.append(df["close"].iloc[i] * 2)
return result
# 优化后
def fast_calculation(df):
result = df["close"] * 2
return result
```
## 提交前检查清单
在提交代码修改前,确认以下事项:
- [ ] 修改内容符合需求文件要求(如果存在)
- [ ] 没有引入与任务无关的功能变更
- [ ] 没有破坏现有功能
- [ ] 格式调整已说明
- [ ] 性能优化已说明
- [ ] 语法错误已修复
## 相关规则文件
- [rules_index.md](rules_index.md) - 规则索引
- [interaction_rules.md](interaction_rules.md) - 交互限制规则
- [formatting.md](formatting.md) - 代码格式化规则