一键导入
code-cleanup-methodology
Systematic approach to clean up and organize Android project code after multiple feature implementations
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Systematic approach to clean up and organize Android project code after multiple feature implementations
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Guidance for creating effective modular skills for Claude. Use when creating or refining skills to ensure they are concise, follow progressive disclosure patterns, and provide appropriate degrees of freedom.
Guidelines for ensuring smooth and race-condition-free theme transitions in Android, specifically regarding icon tinting and state management.
Best practices for effective human-AI pair programming and communication
Clarify touch-target vs visual size when users ask to resize buttons without changing icons.
Choose whether the outer container acts as the touch proxy or remains a layout placeholder to control hit targets.
Best practices for 60fps custom View rendering with zero allocation in onDraw
| name | Code Cleanup Methodology |
| description | Systematic approach to clean up and organize Android project code after multiple feature implementations |
Last Verified: 2026-01-23 Applicable SDK: Android 14+ (API 34+) Dependencies: code-quality-audit, best-practice-check
Systematic approach to clean up and organize Android project code after multiple feature implementations.
触发条件(满足任一即应整理 / triggers: any of below):
目标 / Goal: 识别需要清理的内容 (identify cleanup targets)
# 搜索注释掉的代码块
grep -r "^[ ]*// [a-zA-Z]" app/src/main/java --include="*.kt" | grep -v "^[ ]*//" > /tmp/comments.txt
识别标准 / Identification:
// private var xxx -> Moved to ... - 迁移说明注释 (migration note)// override fun xxx() {} - Removed - 删除函数注释 (removed function)// stopSecondsTimer() -> Not needed - 过时调用注释 (obsolete call)处理方式 / Action:
// ❌ 不良示例 - 过多空行
fun functionA() { }
fun functionB() { }
// ✅ 良好示例 - 适度空行
fun functionA() { }
fun functionB() { }
规则 / Rules:
需清理的导入 / Clean up imports:
import com.example.* / wildcard)工具:
# Android Studio: Code > Optimize Imports (Ctrl+Alt+O / Cmd+Opt+O)
./gradlew lintKotlin # Kotlin lint 检查
识别模式 / Patterns to flag:
// ❌ 冗余的条件判断
if (enabled) {
function(enabled = true)
} else {
function(enabled = false)
}
// ✅ 简化版本
function(enabled = enabled)
常见冗余 / Common cases:
单一职责原则 (SRP):
// ❌ 违反 SRP - 函数做了太多事
fun setupUI() {
initViews()
loadData()
setupListeners()
applyTheme()
validatePermissions()
}
// ✅ 符合 SRP - 分离关注点
fun setupUI() {
initViews()
setupListeners()
}
fun loadInitialData() {
loadData()
validatePermissions()
}
检查清单 / Checklist:
文件大小阈值 / File size thresholds:
拆分策略:
// ❌ 巨型 Activity (800+ 行)
class FullscreenClockActivity : Activity {
// UI setup
// Data binding
// Network calls
// State management
// ...
}
// ✅ 拆分为 Controllers
class FullscreenClockActivity : Activity {
private lateinit var uiController: UIStateController
private lateinit var dataController: DataController
private lateinit var networkController: NetworkController
}
命名约定:
| 类型 | 约定 | 示例 |
|---|---|---|
| 变量 | camelCase | settingsManager |
| 常量 | UPPER_SNAKE | BRIGHTNESS_MAX |
| 私有变量 | camelCase | _internalState (可选前缀) |
| 函数 | camelCase, 动词开头 | updateTime(), applyTheme() |
| 类 | PascalCase | SettingsCoordinator |
| 接口 | PascalCase, 不加 I 前缀 | SettingsProvider |
检查清单 / Checklist:
data, temp, x)(meaningful names)is/has/should 开头 (boolean prefix rule)需要 KDoc 的地方:
/**
* 管理 UI 状态和可见性逻辑。
*
* 处理 Zen Mode、秒表显示、交互状态的优先级系统。
*
* @property binding Activity 的 ViewBinding
* @property viewModel 共享的 ViewModel
*/
class UIStateController(
private val binding: ActivityMainBinding,
private val viewModel: FullscreenClockViewModel
) {
/**
* 更新秒表的可见性。
*
* 优先级系统:
* 1. 显示秒表 - 永久可见
* 2. 显示齿轮和主题切换 - 基于交互状态
* 3. 灯光按钮 - 当灯光开启时豁免隐藏
*/
fun updateSecondsVisibility() { }
}
规则 / Rules:
// Set the value for setValue())(avoid redundant comments)好的注释 / Good inline comments:
// Priority: Seconds mode takes absolute precedence - force light off
forceTurnOffLight()
// Decouple physics from layout: Visual size (64dp) != Layout size (96dp for glow)
outerRadius = (visualDiameter / 2f) - (PADDING_DP * density)
坏的注释(应删除)/ Bad comments to delete:
// Set the manager ❌ - 重复代码
settingsManager = AppSettingsManager(this)
// Call the function ❌ - 无意义
updateTime()
// TODO: Fix this later ❌ - 不明确的 TODO
TODO 注释规范 / TODO style guide:
// ✅ 良好的 TODO
// TODO(username, 2026-01-22): 当 Android 15 发布后,迁移到新的 Permission API
// See: https://developer.android.com/reference/...
// ❌ 不良的 TODO
// TODO: fix
检查未使用的依赖 / Detect unused deps:
./gradlew app:dependencies > dependencies.txt
# 手动审查是否有未使用的库
常见冗余依赖 / Common redundancies:
implementation 而非 testImplementation (test libs in wrong config)Kotlin 导入顺序 / Import order:
使用 Android Studio:
Settings > Editor > Code Style > Kotlin > Imports
- ✅ Use single name import
- ✅ Sort imports alphabetically
常见泄漏模式 / Typical leaks:
// ❌ 未取消的 Timer
private var lightTimer: CountDownTimer? = null
// ✅ 在 onDestroy 中清理
override fun onDestroy() {
lightTimer?.cancel()
lightTimer = null
}
检查清单 / Checklist:
onDestroy 中置空 (null listeners in onDestroy)viewModelScope / lifecycleScope (use scoped coroutines)onPause 中停止 (stop animations onPause)onDraw 中的零分配:
// ❌ 在 onDraw 中创建对象
override fun onDraw(canvas: Canvas) {
val paint = Paint() // 每帧分配!
canvas.drawCircle(x, y, r, paint)
}
// ✅ 重用对象
private val paint = Paint()
override fun onDraw(canvas: Canvas) {
canvas.drawCircle(x, y, r, paint)
}
参考:Android High-Performance Custom View Skill
# 完整构建
./gradlew clean build
# 仅编译检查
./gradlew assembleDebug
# Lint 检查
./gradlew lintDebug
整理提交最佳实践:
# 分离功能和整理
git add -p # 交互式选择
# 提交信息模板
refactor: cleanup FullscreenClockActivity
- Remove dead code and legacy comments
- Organize imports
- Extract helper methods for better readability
Affected files:
- FullscreenClockActivity.kt (-50 lines)
- UIStateController.kt (formatting)
提交粒度 / Commit granularity:
使用此清单确保全面整理:
println / Log.d 调试语句 (remove debug logs)./gradlew assembleDebug 成功 (build passes)./gradlew lintDebug 无严重警告 (lint clean)#!/bin/bash
# cleanup-check.sh - 代码整理检查脚本
echo "🔍 检查注释掉的代码..."
grep -r "^[ ]*//.*->" app/src/main/java --include="*.kt" | wc -l
echo "🔍 检查过长的文件..."
find app/src/main/java -name "*.kt" -exec wc -l {} \; | awk '$1 > 500 {print $2 " (" $1 " lines)"}'
echo "🔍 检查过长的函数..."
# 需要更复杂的 AST 解析,建议使用 detekt
echo "✅ 运行 Lint..."
./gradlew lintDebug
echo "✅ 构建检查..."
./gradlew assembleDebug
在 app/build.gradle.kts 添加:
plugins {
id("io.gitlab.arturbosch.detekt") version "1.23.0"
}
detekt {
config.setFrom(files("$rootDir/config/detekt/detekt.yml"))
}
| 项目阶段 | 整理频率 | 耗时估算 |
|---|---|---|
| 快速开发期 | 每 5 个功能 | 30-60 分钟 |
| 稳定迭代期 | 每个 Sprint | 1-2 小时 |
| 维护期 | 按需 | 15-30 分钟 |
Q: 整理时应该删除所有注释吗? A: 不应该。保留以下注释:
Q: 如何判断代码是否应该删除? A: 遵循 3 个月规则:
Q: 整理会不会引入新 bug? A: 降低风险的方法:
Q: 团队协作时如何协调整理? A: 最佳实践:
最后提醒: 代码整理是持续的过程,不是一次性任务。保持定期整理的习惯,项目会更健康、更易维护!