elixir-writing-docs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseElixir Writing Docs
Elixir文档编写指南
Quick Reference
快速参考
| Topic | Reference |
|---|---|
| Doctests: syntax, gotchas, when to use | references/doctests.md |
| Cross-references and linking syntax | references/cross-references.md |
| Admonitions, formatting, tabs | references/admonitions-and-formatting.md |
| 主题 | 参考链接 |
|---|---|
| Doctests:语法、注意事项、适用场景 | references/doctests.md |
| 交叉引用与链接语法 | references/cross-references.md |
| 警示块、格式设置、标签页 | references/admonitions-and-formatting.md |
First-Line Summary Rule
首行摘要规则
ExDoc and tools like extract the first paragraph of and as a summary. Keep the opening line concise and self-contained.
mix docs@moduledoc@docelixir
undefinedExDoc及等工具会提取和的第一段作为摘要。请确保开头行简洁且独立完整。
mix docs@moduledoc@docelixir
undefinedGOOD - first line works as a standalone summary
推荐写法 - 首行可作为独立摘要
@moduledoc """
Handles payment processing through Stripe and local ledger reconciliation.
Wraps the Stripe API client and ensures each charge is recorded in the
local ledger before returning a confirmation to the caller.
"""
@moduledoc """
处理通过Stripe的支付流程及本地分类账对账。
封装Stripe API客户端,确保每笔收费在向调用者返回确认前都已记录到本地分类账。
"""
BAD - first line is vague, forces reader to continue
不推荐写法 - 首行模糊,需读者继续阅读
@moduledoc """
This module contains various functions related to payments.
It uses Stripe and also updates the ledger.
"""
The same rule applies to `@doc`:
```elixir@moduledoc """
此模块包含与支付相关的各类函数。
它使用Stripe并更新分类账。
"""
该规则同样适用于`@doc`:
```elixirGOOD
推荐写法
@doc """
Charges a customer's default payment method for the given amount in cents.
Returns on success or when the payment
gateway rejects the request.
"""
{:ok, charge}{:error, reason}@doc """
向客户的默认支付方式收取指定金额(单位:最小货币单位)。
成功时返回,支付网关拒绝请求时返回。
"""
{:ok, charge}{:error, reason}BAD
不推荐写法
@doc """
This function is used to charge a customer.
"""
undefined@doc """
此函数用于向客户收费。
"""
undefined@moduledoc Structure
@moduledoc结构
A well-structured follows this pattern:
@moduledocelixir
defmodule MyApp.Inventory do
@moduledoc """
Tracks warehouse stock levels and triggers replenishment orders.
This module maintains an ETS-backed cache of current quantities and
exposes functions for atomic stock adjustments. It is designed to be
started under a supervisor and will restore state from the database
on init.
## Examples
iex> {:ok, pid} = MyApp.Inventory.start_link(warehouse: :east)
iex> MyApp.Inventory.current_stock(pid, "SKU-1042")
{:ok, 350}
## Configuration
Expects the following in `config/runtime.exs`:
config :my_app, MyApp.Inventory,
repo: MyApp.Repo,
low_stock_threshold: 50
"""
endKey points:
- First paragraph is the summary (one to two sentences).
- shows realistic usage. Use doctests when the example is runnable.
## Examples - documents required config keys. Omit this section if the module takes no config.
## Configuration - Use second-level headings () only. First-level (
##) is reserved for the module name in ExDoc output.#
结构良好的遵循以下模式:
@moduledocelixir
defmodule MyApp.Inventory do
@moduledoc """
跟踪仓库库存水平并触发补货订单。
此模块维护基于ETS的当前库存数量缓存,并提供原子化库存调整函数。它设计为由监督者启动,初始化时会从数据库恢复状态。
## 示例
iex> {:ok, pid} = MyApp.Inventory.start_link(warehouse: :east)
iex> MyApp.Inventory.current_stock(pid, "SKU-1042")
{:ok, 350}
## 配置
需在`config/runtime.exs`中配置以下内容:
config :my_app, MyApp.Inventory,
repo: MyApp.Repo,
low_stock_threshold: 50
"""
end关键点:
- 第一段为摘要(1-2句话)。
- 展示实际用法。示例可运行时请使用doctests。
## 示例 - 文档所需的配置键。若模块无需配置可省略此节。
## 配置 - 仅使用二级标题()。一级标题(
##)在ExDoc输出中预留给模块名称。#
Documenting Behaviour Modules
行为模块文档编写
When defining a behaviour, document the expected callbacks:
elixir
defmodule MyApp.PaymentGateway do
@moduledoc """
Behaviour for payment gateway integrations.
Implementations must handle charging, refunding, and status checks.
See `MyApp.PaymentGateway.Stripe` for a reference implementation.
## Callbacks
* `charge/2` - Initiate a charge for a given amount
* `refund/2` - Refund a previously completed charge
* `status/1` - Check the status of a transaction
"""
@callback charge(amount :: pos_integer(), currency :: atom()) ::
{:ok, transaction_id :: String.t()} | {:error, term()}
@callback refund(transaction_id :: String.t(), amount :: pos_integer()) ::
:ok | {:error, term()}
@callback status(transaction_id :: String.t()) ::
{:pending | :completed | :failed, map()}
end定义行为时,请记录预期的回调函数:
elixir
defmodule MyApp.PaymentGateway do
@moduledoc """
支付网关集成的行为规范。
实现必须处理收费、退款和状态检查。参考实现请见`MyApp.PaymentGateway.Stripe`。
## 回调函数
* `charge/2` - 发起指定金额的收费
* `refund/2` - 为已完成的收费发起退款
* `status/1` - 检查交易状态
"""
@callback charge(amount :: pos_integer(), currency :: atom()) ::
{:ok, transaction_id :: String.t()} | {:error, term()}
@callback refund(transaction_id :: String.t(), amount :: pos_integer()) ::
:ok | {:error, term()}
@callback status(transaction_id :: String.t()) ::
{:pending | :completed | :failed, map()}
end@doc Structure
@doc结构
elixir
@doc """
Reserves the given quantity of an item, decrementing available stock.
Returns `{:ok, reservation_id}` when stock is available, or
`{:error, :insufficient_stock}` when the requested quantity exceeds
what is on hand.elixir
@doc """
预留指定数量的商品,减少可用库存。
库存充足时返回`{:ok, reservation_id}`,请求数量超过现有库存时返回`{:error, :insufficient_stock}`。Examples
示例
iex> MyApp.Inventory.reserve("SKU-1042", 5)
{:ok, "res_abc123"}
iex> MyApp.Inventory.reserve("SKU-9999", 1)
{:error, :not_found}iex> MyApp.Inventory.reserve("SKU-1042", 5)
{:ok, "res_abc123"}
iex> MyApp.Inventory.reserve("SKU-9999", 1)
{:error, :not_found}Options
选项
- - Target warehouse atom. Defaults to
:warehouse.:primary- - Timeout in milliseconds. Defaults to
:timeout. """ @spec reserve(String.t(), pos_integer(), keyword()) :: {:ok, String.t()} | {:error, :insufficient_stock | :not_found} def reserve(sku, quantity, opts \ []) do5_000
...
end
**Guidelines:**
- State what the function does, then what it returns.
- Document each option in a bulleted `## Options` section when the function accepts a keyword list.
- Place `@spec` between `@doc` and `def`. This is the conventional ordering.
- Include doctests for pure functions. Skip them for side-effecting functions (see [references/doctests.md](references/doctests.md)).- - 目标仓库原子值,默认为
:warehouse。:primary- - 超时时间(毫秒),默认为
:timeout。 """ @spec reserve(String.t(), pos_integer(), keyword()) :: {:ok, String.t()} | {:error, :insufficient_stock | :not_found} def reserve(sku, quantity, opts \ []) do5_000
...
end
**指南:**
- 先说明函数的作用,再说明返回值。
- 当函数接受关键字列表时,在`## 选项`节中用项目符号记录每个选项。
- 将`@spec`放在`@doc`和`def`之间,这是常规顺序。
- 纯函数请包含doctests。有副作用的函数可跳过(详见[references/doctests.md](references/doctests.md))。@typedoc
@typedoc
Document custom types defined with or :
@type@opaqueelixir
@typedoc """
A positive integer representing an amount in the smallest currency unit (e.g., cents).
"""
@type amount :: pos_integer()
@typedoc """
Reservation status returned by `status/1`.
* `:held` - Stock is reserved but not yet shipped
* `:released` - Reservation was cancelled and stock restored
* `:fulfilled` - Items have shipped
"""
@type reservation_status :: :held | :released | :fulfilled
@typedoc """
Opaque handle returned by `connect/1`. Do not pattern-match on this value.
"""
@opaque connection :: %__MODULE__{socket: port(), buffer: binary()}For types, the is especially important because callers cannot inspect the structure.
@opaque@typedoc为或定义的自定义类型添加文档:
@type@opaqueelixir
@typedoc """
表示金额的正整数,单位为最小货币单位(如美分)。
"""
@type amount :: pos_integer()
@typedoc """
`status/1`返回的预留状态。
* `:held` - 库存已预留但尚未发货
* `:released` - 预留已取消,库存已恢复
* `:fulfilled` - 商品已发货
"""
@type reservation_status :: :held | :released | :fulfilled
@typedoc """
`connect/1`返回的不透明句柄。请勿对该值进行模式匹配。
"""
@opaque connection :: %__MODULE__{socket: port(), buffer: binary()}对于类型,尤为重要,因为调用者无法查看其结构。
@opaque@typedocMetadata
元数据
@doc since and @doc deprecated
@doc since与@doc deprecated
elixir
@doc since: "1.3.0"
@doc """
Transfers stock between two warehouses.
"""
def transfer(from, to, sku, quantity), do: # ...
@doc deprecated: "Use transfer/4 instead"
@doc """
Moves items between locations. Deprecated in favor of `transfer/4`
which supports cross-region transfers.
"""
def move_stock(from, to, sku, quantity), do: # ...You can combine metadata and the docstring in one attribute:
elixir
@doc since: "2.0.0", deprecated: "Use bulk_reserve/2 instead"
@doc """
Reserves multiple items in a single call.
"""
def batch_reserve(items), do: # ...@moduledoc since:elixir
@moduledoc since: "1.2.0"
@moduledoc """
Handles webhook signature verification for Stripe events.
"""elixir
@doc since: "1.3.0"
@doc """
在两个仓库之间转移库存。
"""
def transfer(from, to, sku, quantity), do: # ...
@doc deprecated: "Use transfer/4 instead"
@doc """
在不同地点之间转移商品。已被支持跨区域转移的`transfer/4`替代,不再推荐使用。
"""
def move_stock(from, to, sku, quantity), do: # ...你可以在一个属性中组合元数据和文档字符串:
elixir
@doc since: "2.0.0", deprecated: "Use bulk_reserve/2 instead"
@doc """
一次性预留多个商品。
"""
def batch_reserve(items), do: # ...@moduledoc since:elixir
@moduledoc since: "1.2.0"
@moduledoc """
处理Stripe事件的Webhook签名验证。
"""When to Use @doc false / @moduledoc false
何时使用@doc false / @moduledoc false
Suppress documentation when the module or function is not part of the public API:
elixir
undefined当模块或函数不属于公共API时,可隐藏其文档:
elixir
undefinedPrivate implementation module — internal to the application
私有实现模块 —— 仅应用内部使用
defmodule MyApp.Inventory.StockCache do
@moduledoc false
...
end
defmodule MyApp.Inventory.StockCache do
@moduledoc false
...
end
Protocol implementation — documented at the protocol level
协议实现 —— 在协议层面进行文档说明
defimpl String.Chars, for: MyApp.Money do
@moduledoc false
...
end
defimpl String.Chars, for: MyApp.Money do
@moduledoc false
...
end
Callback implementation — documented at the behaviour level
回调实现 —— 在行为规范层面进行文档说明
@doc false
def handle_info(:refresh, state) do
...
end
@doc false
def handle_info(:refresh, state) do
...
end
Helper used only inside the module
仅模块内部使用的辅助函数
@doc false
def do_format(value), do: # ...
**Do NOT use `@doc false` on genuinely public functions.** If a function is exported and callers depend on it, document it. If it should not be called externally, make it private with `defp`.@doc false
def do_format(value), do: # ...
**请勿在真正的公共函数上使用`@doc false`。** 如果函数已导出且调用者依赖它,请为其添加文档。如果它不应被外部调用,请用`defp`将其设为私有函数。Documentation vs Code Comments
文档与代码注释的区别
Documentation ( | Code Comments ( | |
|---|---|---|
| Audience | Users of your API | Developers reading source |
| Purpose | Contract: what it does, what it returns | Why a particular implementation choice was made |
| Rendered | Yes, by ExDoc in HTML/epub | No, visible only in source |
| Required | All public modules and functions | Only where code intent is non-obvious |
elixir
@doc """
Validates that the given coupon code is active and has remaining uses.
"""
@spec validate_coupon(String.t()) :: {:ok, Coupon.t()} | {:error, :expired | :exhausted}
def validate_coupon(code) do
# We query the read replica here to avoid adding load to the
# primary during high-traffic discount events.
Repo.replica().get_by(Coupon, code: code)
|> check_expiry()
|> check_remaining_uses()
endThe tells the caller what does and returns. The inline comment explains an implementation decision that would otherwise be surprising.
@docvalidate_coupon/1文档( | 代码注释( | |
|---|---|---|
| 受众 | API使用者 | 阅读源码的开发者 |
| 目的 | 契约:说明功能和返回值 | 解释特定实现选择的原因 |
| 是否会被渲染 | 是,ExDoc会将其渲染为HTML/epub | 否,仅在源码中可见 |
| 是否必需 | 所有公共模块和函数都需要 | 仅在代码意图不明显时需要 |
elixir
@doc """
验证给定优惠券代码是否有效且仍有剩余使用次数。
"""
@spec validate_coupon(String.t()) :: {:ok, Coupon.t()} | {:error, :expired | :exhausted}
def validate_coupon(code) do
# 此处使用只读副本查询,以避免在高流量折扣活动期间给主库增加负载。
Repo.replica().get_by(Coupon, code: code)
|> check_expiry()
|> check_remaining_uses()
end@docvalidate_coupon/1When to Load References
何时查阅参考文档
- Writing doctests or debugging doctest failures --> references/doctests.md
- Adding links between modules, functions, types --> references/cross-references.md
- Using admonition blocks, tabs, or formatting in docs --> references/admonitions-and-formatting.md
- 编写doctests或调试doctest失败问题 --> references/doctests.md
- 在模块、函数、类型之间添加链接 --> references/cross-references.md
- 在文档中使用警示块、标签页或格式化设置 --> references/admonitions-and-formatting.md