clean-code
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDart Coding Guidelines
Dart编码规范
- Write concise, technical Dart code with accurate examples
- Use functional and declarative programming patterns
- Choose descriptive variable names with auxiliary verbs (e.g., ,
isLoading)hasError - Keep functions and classes short (< 200 lines, < 10 public methods)
- Always use English for code and documentation
- Use PascalCase for classes, camelCase for variables and functions, snake_case for files, UPPERCASE for constants
- Use arrow syntax for simple methods and expression bodies for one-line getters/setters
- Avoid nesting blocks; prefer early checks and returns
- Pass and return parameters using RO-RO (Receive Object, Return Object)
- Avoid magic numbers; use well-named constants
- Follow an 100-character line limit and use trailing commas for better formatting
- Follow the lint rules set for this project (and
all_lint_rules.yaml)analysis_options.yaml - Strong Typing: Strictly prohibit . Use
dynamicor explicit types.Object? - Cascade Pattern: Use cascade notation () for cleaner initialization of complex objects where appropriate.
.. - Null Safety: Avoid null assertion operator unless value is guaranteed non-null by control flow. Prefer pattern matching.
! - Switch Expressions: Prefer exhaustive expressions (no
switchneeded in Dart 3).break
- 编写简洁、专业的Dart代码,附带准确示例
- 使用函数式和声明式编程模式
- 选择带有辅助动词的描述性变量名(例如:、
isLoading)hasError - 保持函数和类的体量短小(代码行数<200,公共方法<10个)
- 代码和文档统一使用英文
- 类名使用PascalCase,变量和函数名使用camelCase,文件名使用snake_case,常量使用全大写格式
- 简单方法使用箭头语法,单行getter/setter使用表达式体
- 避免代码块嵌套,优先使用前置检查和提前返回
- 使用RO-RO(Receive Object, Return Object)模式传递和返回参数
- 避免使用魔法数字,使用命名规范的常量
- 遵循100字符的行长度限制,使用尾随逗号优化格式化效果
- 遵守本项目设定的lint规则(和
all_lint_rules.yaml)analysis_options.yaml - 强类型要求:严格禁止使用,使用
dynamic或显式类型Object? - 级联模式:合适场景下使用级联运算符()简化复杂对象的初始化
.. - 空安全:除非控制流能保证值非空,否则避免使用空断言运算符,优先使用模式匹配
! - Switch表达式:优先使用穷尽式表达式(Dart 3中无需使用
switch)break
Code Clarity & Maintenance
代码清晰度与可维护性
- 300-Line Limit: Strictly flag any file exceeding 300 lines (excluding tests) for decomposition into separate files.
- Guard Clauses: Use early returns (e.g., ) to reduce indentation.
if (user == null) return; - Meaningful Naming: Use intention-revealing names. Boolean variables MUST use prefixes like ,
is,has.should - Disposable Lifecycle: ,
TextEditingController,ScrollController,FocusNode,StreamSubscription, etc. MUST beAnimationControllerinitialized inlateand disposed ininitState(). Inline initialization is STRICTLY prohibited to prevent memory leaks and ensure proper lifecycle management.dispose() - No Print Statements: STRICTLY prohibit . Use
print()for all logging.AppLogger - Localization: Zero hardcoded user-facing strings. Use files and
.arb.context.l10n - Reuse: If a widget or code block is used multiple times, extract it into a common widget or utility. Place shared widgets in .
core/views/widgets - Library Organization: Group related classes within the same library file. For large libraries, export smaller private libraries from a single top-level library.
- 300行限制:任何非测试文件代码行数超过300行时必须标记,拆分到不同文件中
- 卫语句:使用提前返回(例如:)减少缩进层级
if (user == null) return; - 有意义的命名:使用能体现意图的名称,布尔变量必须使用、
is、has等前缀should - 可回收资源生命周期:、
TextEditingController、ScrollController、FocusNode、StreamSubscription等必须在AnimationController中声明为initState()初始化,并在late中销毁。严格禁止行内初始化,避免内存泄漏,确保生命周期管理合规dispose() - 禁止使用Print语句:严格禁止使用,所有日志输出使用
print()AppLogger - 国际化:面向用户的字符串零硬编码,使用文件和
.arb实现context.l10n - 代码复用:如果某个widget或代码块被多次使用,将其抽取为公共widget或工具函数,公共widget放在目录下
core/views/widgets - 库组织规范:将相关类放在同一个库文件中,大型库可从单个顶层库导出小型私有库
Anti-Patterns
反模式
- No Heavy Work in : Move sorting, filtering, and expensive computations to
build()orinitState(). Never compute in every rebuild.didUpdateWidget() - No Nested ScrollViews in the same direction. Use with Slivers instead.
CustomScrollView - No Unnecessary Containers: Prefer ,
SizedBox,PaddingoverDecoratedBox.Container - Widget Extraction: STRICTLY prohibit private methods that return widgets. Extract into separate widget classes.
_build*()
- 禁止在中执行重负载操作:将排序、筛选和高开销计算逻辑移到
build()或initState()中,禁止在每次重绘时执行计算didUpdateWidget() - 禁止同方向嵌套ScrollViews:改用搭载Slivers的实现
CustomScrollView - 避免不必要的Container:优先使用、
SizedBox、Padding代替DecoratedBoxContainer - Widget抽取规则:严格禁止编写返回widget的私有方法,抽取为独立的widget类
_build*()
Documentation
文档规范
- No Redundant Comments: Do not explain WHAT code does. Explain WHY (intent).
- Simple & Informative: Comments MUST be concise, informative, and understandable. Avoid "wall of text".
- No History Comments: STRICTLY prohibit comments like "fixed X" or "updated Y". Git history is the source of truth.
- Follow official documentation for best practices.
- Document public classes and methods with clear descriptions, parameters, return values, and a code snippet taken from the codebase
- Comment briefly only complex logic and non-obvious code decisions
- 禁止冗余注释:不要解释代码做了什么,解释为什么这么做(设计意图)
- 简洁且信息明确:注释必须简洁、信息充足、易于理解,避免大段文字堆砌
- 禁止记录修改历史的注释:严格禁止类似“修复了X问题”、“更新了Y功能”的注释,Git历史是唯一可信的修改记录
- 遵循官方文档的最佳实践
- 公共类和方法需要编写清晰的说明文档,包含参数、返回值说明,以及取自代码库的代码示例
- 仅对复杂逻辑和非显而易见的代码决策做简要注释