odoo_coding_guidelines

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Odoo Coding Guidelines

Odoo 编码指南

Use this skill when the user asks to review, check, write, or validate Odoo code against official coding standards. Source: https://www.odoo.com/documentation/19.0/contributing/development/coding_guidelines.html
Load sub-files based on what is being reviewed:
  • Python files, models, fields, methods → read python.md
  • XML views, data files, templates → read xml.md
  • JavaScript files → read javascript.md
  • CSS / SCSS files → read css.md
  • Full code review or checklist requested → read checklist.md
  • Module structure, file naming, directory layout → read structure.md
For mixed reviews (e.g. a whole module), load all relevant files.

当用户要求依据官方编码标准审查、检查、编写或验证 Odoo 代码时,使用此 skill。 来源:https://www.odoo.com/documentation/19.0/contributing/development/coding_guidelines.html
根据审查内容加载子文件:
  • Python 文件、模型、字段、方法 → 阅读 python.md
  • XML 视图、数据文件、模板 → 阅读 xml.md
  • JavaScript 文件 → 阅读 javascript.md
  • CSS / SCSS 文件 → 阅读 css.md
  • 需要完整代码审查或检查清单 → 阅读 checklist.md
  • 模块结构、文件命名、目录布局 → 阅读 structure.md
对于混合审查(例如整个模块),请加载所有相关文件。

General Principles

通用原则

Language

语言

  • All code must be in English: variable names, field names, method names, model names, comments, commit messages, XML IDs, and string literals used as technical identifiers.
  • 所有代码必须使用英语:变量名、字段名、方法名、模型名、注释、提交信息、XML ID,以及用作技术标识符的字符串字面量。

Comments

注释

  • Comments must explain WHY, never WHAT. The code itself should be readable enough to convey what it does.
  • If a comment is needed to explain what a block of code does, refactor the code until it is self-explanatory, then remove the comment.
  • Acceptable comments: hidden constraints, non-obvious invariants, workarounds for specific bugs, behavior that would surprise a reader.
  • 注释必须解释原因,绝不能解释行为。代码本身应具有足够的可读性,让读者能够了解其功能。
  • 如果需要通过注释解释某个代码块的行为,请重构代码,直至代码含义清晰,然后删除该注释。
  • 可接受的注释包括:隐藏的限制条件、不明显的隐含不变量、针对特定 bug 的变通方案,以及可能令读者感到意外的行为。

Naming

命名

  • Never truncate names to save keystrokes. Autocomplete makes long names free — short names cost readability.
    python
    # ❌
    def comp_tot_amt(self): ...
    inv_dt = fields.Date(...)
    
    # ✅
    def _compute_total_amount(self): ...
    invoice_date = fields.Date(...)
  • 绝不要为了节省输入时间而缩写名称。自动补全让长名称几乎不产生额外成本,而短名称会损害可读性。
    python
    # ❌
    def comp_tot_amt(self): ...
    inv_dt = fields.Date(...)
    
    # ✅
    def _compute_total_amount(self): ...
    invoice_date = fields.Date(...)