在 Manus 中运行任何 Skill
一键导入
一键导入
一键在 Manus 中运行任何 Skill
开始使用code-quality
星标8
分支1
更新时间2025年11月8日 04:35
自动检查代码质量问题,提供改进建议
安装
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
SKILL.md
readonly菜单
自动检查代码质量问题,提供改进建议
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | code-quality |
| description | 自动检查代码质量问题,提供改进建议 |
| allowed-tools | ["fs_read","bash_run"] |
| triggers | [{"type":"keyword","keywords":["质量","quality","重构","refactor","优化","optimize"]},{"type":"context","condition":"during /review"},{"type":"context","condition":"during /analyze"}] |
本技能提供自动化的代码质量检查,识别常见问题并提供改进建议。
衡量代码中独立路径的数量。
阈值建议:
降低复杂度的方法:
// ❌ 高复杂度
func ProcessOrder(order Order) error {
if order.Status == "pending" {
if order.Amount > 1000 {
if order.User.IsVIP {
// 复杂的逻辑
} else {
// 另一个复杂逻辑
}
} else {
// 更多逻辑
}
} else if order.Status == "confirmed" {
// 更多嵌套
}
return nil
}
// ✅ 降低复杂度:提取方法
func ProcessOrder(order Order) error {
if order.Status == "pending" {
return processPendingOrder(order)
}
if order.Status == "confirmed" {
return processConfirmedOrder(order)
}
return nil
}
func processPendingOrder(order Order) error {
if order.Amount > 1000 {
return processLargeOrder(order)
}
return processSmallOrder(order)
}
衡量代码的理解难度。
降低认知复杂度:
// ❌ 高认知复杂度
func ValidateUser(user User) error {
if user.Name != "" {
if user.Email != "" {
if user.Age >= 18 {
return nil
} else {
return errors.New("age must be >= 18")
}
} else {
return errors.New("email required")
}
} else {
return errors.New("name required")
}
}
// ✅ 低认知复杂度:使用卫语句
func ValidateUser(user User) error {
if user.Name == "" {
return errors.New("name required")
}
if user.Email == "" {
return errors.New("email required")
}
if user.Age < 18 {
return errors.New("age must be >= 18")
}
return nil
}
// ❌ 重复代码
func CreateUserOrder(user User, items []Item) error {
total := 0.0
for _, item := range items {
total += item.Price * float64(item.Quantity)
}
discount := total * 0.1
final := total - discount
return saveOrder(user.ID, final)
}
func CreateGuestOrder(email string, items []Item) error {
total := 0.0
for _, item := range items {
total += item.Price * float64(item.Quantity)
}
discount := total * 0.1
final := total - discount
return saveGuestOrder(email, final)
}
// ✅ 提取公共逻辑
func calculateOrderTotal(items []Item) float64 {
total := 0.0
for _, item := range items {
total += item.Price * float64(item.Quantity)
}
discount := total * 0.1
return total - discount
}
func CreateUserOrder(user User, items []Item) error {
total := calculateOrderTotal(items)
return saveOrder(user.ID, total)
}
func CreateGuestOrder(email string, items []Item) error {
total := calculateOrderTotal(items)
return saveGuestOrder(email, total)
}
改进方法:
// ❌ 参数过多
func CreateUser(name, email, phone, address, city, country, zipCode string, age int) error {
// ...
}
// ✅ 使用对象封装
type UserInfo struct {
Name string
Email string
Phone string
Address Address
Age int
}
type Address struct {
Street string
City string
Country string
ZipCode string
}
func CreateUser(info UserInfo) error {
// ...
}
// ❌ 糟糕的命名
var d int // 什么的 d?
var data []byte // 太通用
var x User // 无意义
// ✅ 好的命名
var daysSinceLastLogin int
var requestBody []byte
var currentUser User
// ❌ 不清晰
func proc() error { ... }
func handle(d Data) { ... }
// ✅ 清晰表意
func ProcessPayment() error { ... }
func HandleUserRegistration(data RegistrationData) { ... }
// ❌ 魔术数字
if user.LoginAttempts > 5 { ... }
time.Sleep(300 * time.Second)
// ✅ 命名常量
const MaxLoginAttempts = 5
const DefaultTimeout = 300 * time.Second
if user.LoginAttempts > MaxLoginAttempts { ... }
time.Sleep(DefaultTimeout)
// ✅ 解释为什么
// 使用指数退避避免在服务重启时造成雷鸣般的请求
func retryWithBackoff() { ... }
// 解释业务规则
// 根据税法规定,超过 1000 元的订单需要额外审核
if order.Amount > 1000 { ... }
// ❌ 重复代码内容
// 设置名称为 John
name := "John"
// 循环遍历用户
for _, user := range users { ... }
// ❌ 忽略错误
file, _ := os.Open("config.json")
// ❌ 丢失上下文
return err
// ✅ 良好的错误处理
file, err := os.Open("config.json")
if err != nil {
return fmt.Errorf("failed to open config file: %w", err)
}
defer file.Close()
// ✅ 使用 defer 确保关闭
file, err := os.Open("data.txt")
if err != nil {
return err
}
defer file.Close()
// ✅ 使用 sync.WaitGroup
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
// 处理
}(i)
}
wg.Wait()
// ❌ 紧耦合
type UserService struct {
db *sql.DB // 直接依赖具体实现
}
// ✅ 依赖注入
type UserRepository interface {
FindByID(id string) (*User, error)
Save(user *User) error
}
type UserService struct {
repo UserRepository // 依赖抽象
}
在审查代码时,自动检查以下项目:
当识别到质量问题时,提供以下格式的建议:
### 代码质量问题: [问题类型]
**位置**: [文件名:行号]
**问题**: [描述问题]
**严重程度**: P0 / P1 / P2 / P3
**影响**: [说明影响]
**建议修改**:
[提供具体的改进代码]
**原因**: [解释为什么要这样改]