Files
android-client/docs/conventions.md
2026-09-04 17:54:27 +08:00

38 lines
2.0 KiB
Markdown
Raw Permalink 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.
# 编码规范
## Kotlin
- 遵循官方 Kotlin 风格:四空格缩进、类型 `UpperCamelCase`、成员 `lowerCamelCase`、常量 `SCREAMING_SNAKE_CASE`。
- 优先不可变:用 `val`、`data class` 的 `copy()`,避免可变共享状态。
- 严格空安全,不用 `!!` 除非有充分理由。
- 用密封类(sealed class)表达状态 / 事件 / 结果。
- 协程:数据操作在 `Dispatchers.IO`,UI 在 `Dispatchers.Main`;用 `StateFlow` 暴露 UI 状态(单向数据流)。
## Compose
- 屏幕组合函数命名 `<Feature>Screen`,接收 `modifier: Modifier = Modifier` 作为首个参数。
- `@Composable` 函数名用 `PascalCase`;被 `@Preview` 或测试使用的纯函数可豁免下划线命名(见 `.editorconfig`)。
- 复用组件放 `core:designsystem` 或 `core:ui`,不复制到 feature。
## 命名
- Activity/Application 以 `Activity`/`Application` 结尾;ViewModel 以 `ViewModel` 结尾;UseCase 以 `UseCase` 结尾;Repository 实现以 `RepositoryImpl` 结尾(接口在 `core:domain` 用 `Repository`)。
- 资源用 `snake_case`;字符串、颜色、尺寸统一在 `core:designsystem` 管理。
- 测试类以 `<Subject>Test` 结尾。
## 版本与依赖
- 依赖版本只写在 `gradle/libs.versions.toml`,模块里用 `libs.*` 访问器引用,禁止硬编码版本。
- 新增依赖先确认是否已有等价项,避免重复引入。
## Git
- Commit 格式 `type(scope): description`,type 如 `feat`/`fix`/`refactor`/`docs`/`build`/`test`/`chore`;一次提交一个变更集。
- 提交前跑 `./gradlew spotlessApply` 与 `bash scripts/verify.sh`。
- 分支流采用简化版 GitFlow:feature 从 `dev` 拉、merge 回 `dev`,`dev` 稳定后经确认再进 `main`。详见 [gitflow.md](gitflow.md)(决策记录 ADR-0005)。
## 文档
- 重要决策写入 `docs/decisions/`(ADR),行为与文档在同一次变更里一起更新。
- 模块级"为什么这样设计"写在该目录的 `DESIGN_NOTES.md`(即时上下文)。