Documentation Writer
Create documentation that people actually read, understand, and follow.
Documentation Types
1. README Files
Structure:
# Project Name
Brief description (1-2 sentences)
## Features
- Key feature 1
- Key feature 2
- Key feature 3
## Quick Start
bash
Install
npm install package
Use
package do-thing
CODEBLOCK1
What Makes Good README:
- - Clear project name
- One-sentence description
- Installation in 3 commands or less
- Working examples
- Common use cases
- Link to full docs
2. API Documentation
Endpoint Documentation:
## Get User
`GET /api/users/{id}`
Retrieves user details by ID.
### Parameters
| Name | Type | In | Required | Description |
|------|------|------|----------|-------------|
| id | string | path | Yes | User ID |
| fields | string | query | No | Fields to return |
### Response
json
{
"id": "123",
"name": "John Doe",
"email": "john@example.com",
"created_at": "2026-01-15T10:30:00Z"
}
### Errors
| Code | Description |
|------|-------------|
| 404 | User not found |
| 401 | Unauthorized |
| 500 | Server error |
### Example
bash
curl -X GET "https://api.example.com/users/123" \
-H "Authorization: Bearer {token}"
CODEBLOCK4
API Doc Structure:
- - HTTP method and endpoint
- Brief description
- Parameters (path, query, body)
- Response format
- Error codes
- Example request
- Rate limits (if applicable)
3. User Guides
Structure:
CODEBLOCK5
Writing Tips:
- - Start with simplest path
- One concept per section
- Use numbered steps
- Include screenshots
- Anticipate problems
4. Code Comments
When to Comment:
- - Why, not what
- Complex logic
- Non-obvious decisions
- Workarounds
- TODOs with context
Good Comments:
CODEBLOCK6
Bad Comments:
CODEBLOCK7
Documentation Principles
1. Audience First
Identify Reader:
- - Beginner? Explain concepts
- Expert? Focus on specifics
- Internal team? Use shorthand
- External users? Full context
Match Tone:
CODEBLOCK8
2. Show, Don't Tell
Bad:
CODEBLOCK9
Good:
CODEBLOCK10
Even Better:
CODEBLOCK11
3. Complete Examples
Bad:
CODEBLOCK12
Good:
CODEBLOCK13
4. Keep Updated
Version Your Docs:
CODEBLOCK14
Mark Outdated:
CODEBLOCK15
Changelog:
CODEBLOCK16
Documentation Patterns
Quick Start
Pattern:
## Quick Start (5 minutes)
### 1. Install
bash
npm install my-package
### 2. Configure
javascript
const myPackage = require('my-package');
myPackage.configure({ apiKey: 'your-key' });
### 3. Use
javascript
const result = myPackage.doSomething();
console.log(result); // "Success!"
CODEBLOCK20
FAQ
Pattern:
CODEBLOCK21
Troubleshooting
Pattern:
## Troubleshooting
### Error: "Connection refused"
**Cause:** The server isn't running.
**Solution:** Start the server with `npm start`.
### Error: "Invalid API key"
**Cause:** Your API key is incorrect or expired.
**Solution:**
1. Check your API key in settings
2. Regenerate if needed
3. Update your configuration
### Performance is slow
**Cause:** Large datasets without pagination.
**Solution:** Use pagination for datasets > 1000 items:
javascript
client.getRecords({ limit: 100, offset: 0 });
CODEBLOCK23
Tools and Formats
Markdown Best Practices
Headers:
CODEBLOCK24
Code Blocks:
Inline `code` for short references.
python
Block code for examples
def example():
return "example"
CODEBLOCK26
Tables:
CODEBLOCK27
Lists:
CODEBLOCK28
Documentation Generators
JSDoc (JavaScript):
CODEBLOCK29
Python Docstrings:
CODEBLOCK30
Documentation Checklist
README:
- - [ ] Project name and description
- [ ] Installation instructions
- [ ] Basic usage example
- [ ] License
- [ ] Contact/support
API Docs:
- - [ ] All endpoints documented
- [ ] Request/response examples
- [ ] Error codes
- [ ] Authentication
- [ ] Rate limits
User Guide:
- - [ ] Prerequisites clear
- [ ] Step-by-step instructions
- [ ] Screenshots/diagrams
- [ ] Troubleshooting section
- [ ] Next steps
Code Comments:
- - [ ] Why, not what
- [ ] Complex logic explained
- [ ] TODOs have context
- [ ] No obvious comments
Common Mistakes
1. Assumption Dumping
Bad:
CODEBLOCK31
Good:
Set these environment variables:
bash
export API_KEY="your-api-key"
export API_URL="https://api.example.com"
CODEBLOCK33
2. Missing Prerequisites
Bad:
CODEBLOCK34
Good:
## Prerequisites
- Node.js 18+ installed
- npm or yarn package manager
- API key from [dashboard](https://dashboard.example.com)
## Start
bash
npm install
npm start
CODEBLOCK36
3. Outdated Examples
Bad:
CODEBLOCK37
Good:
CODEBLOCK38
4. No Error Handling
Bad:
CODEBLOCK39
Good:
CODEBLOCK40
Writing Style
Be Concise
CODEBLOCK41
Be Precise
CODEBLOCK42
Be Active
CODEBLOCK43
Use Examples
CODEBLOCK44 json
{
"timeout": 5000,
"retries": 3
}
CODEBLOCK45
文档编写者
创建人们真正会阅读、理解并遵循的文档。
文档类型
1. README 文件
结构:
markdown
项目名称
简短描述(1-2句话)
功能特性
快速开始
bash
安装
npm install package
使用
package do-thing
使用方法
详细的使用示例
配置
选项和设置
API
API 参考
贡献指南
如何贡献
许可证
MIT
优秀 README 的特点:
- - 清晰的项目名称
- 一句话描述
- 3条命令以内完成安装
- 可运行的示例
- 常见用例
- 完整文档的链接
2. API 文档
接口文档:
markdown
获取用户
GET /api/users/{id}
根据ID获取用户详情。
参数
| 名称 | 类型 | 位置 | 必填 | 描述 |
|---|
| id | string | path | 是 | 用户ID |
| fields |
string | query | 否 | 返回的字段 |
响应
json
{
id: 123,
name: 张三,
email: zhangsan@example.com,
created_at: 2026-01-15T10:30:00Z
}
错误码
未授权 |
| 500 | 服务器错误 |
示例
bash
curl -X GET https://api.example.com/users/123 \
-H Authorization: Bearer {token}
API 文档结构:
- - HTTP 方法和接口地址
- 简短描述
- 参数(路径、查询、请求体)
- 响应格式
- 错误码
- 请求示例
- 速率限制(如适用)
3. 用户指南
结构:
markdown
X 入门指南
前提条件
步骤 1:第一个操作
带截图的详细说明
步骤 2:第二个操作
继续提供清晰的说明
故障排除
常见问题及解决方案
下一步
写作技巧:
- - 从最简单的路径开始
- 每个部分只讲一个概念
- 使用编号步骤
- 包含截图
- 预判可能出现的问题
4. 代码注释
何时添加注释:
- - 解释原因,而非内容
- 复杂逻辑
- 非显而易见的决策
- 变通方案
- 带上下文的待办事项
好的注释:
python
使用二分查找,因为列表已排序,且我们需要 O(log n) 的性能
以实现实时自动补全。对于超过 10,000 项的列表,线性搜索太慢。
def find
item(sortedlist, target):
...
Safari 浏览器问题的变通方案:Safari 不处理 localStorage 中的 null 值
参见:https://bugs.webkit.org/show_bug?id=123456
def safe_store(key, value):
...
TODO(john):当我们添加对多个支付提供商的支持时,应将其重构为独立服务
目前仅处理 Stripe
def process_payment(amount):
...
不好的注释:
python
增加计数器
counter += 1 # 显而易见
遍历项目
for item in items: # 显而易见
process(item) # 显而易见
文档编写原则
1. 受众优先
识别读者:
- - 初学者?解释概念
- 专家?关注细节
- 内部团队?使用简写
- 外部用户?提供完整上下文
匹配语气:
初学者:首先,从 nodejs.org 安装 Node.js
专家:需要 Node 18+
2. 展示,而非告知
不好:
该函数高效处理数据。
好:
该函数在标准笔记本电脑上能在 2 秒内处理 100 万条记录。
更好:
python
在 M1 MacBook 上处理 100 万条记录 < 2 秒
基准测试:testprocessbenchmark.py
def process_batch(data):
...
3. 完整的示例
不好:
python
使用 API
client.do_something()
好:
python
import MyClient
使用 API 密钥初始化
client = MyClient(api_key=your-api-key)
创建新资源
response = client.create_resource(
name=我的资源,
type=standard
)
处理响应
if response.success:
print(f已创建:{response.id})
4. 保持更新
为文档标注版本:
markdown
最后更新:2026-03-16
版本:2.1.0
标记已过时内容:
markdown
⚠️ 已弃用:此接口将在 v3.0 中移除。请改用 /api/v2/users。
变更日志:
markdown
v2.1.0 (2026-03-16)
新增
变更
- - /api/users 现在返回 created_at 时间戳
弃用
- - /api/legacy-endpoint 将在 v3.0 中移除
文档模式
快速开始
模式:
markdown
快速开始(5分钟)
1. 安装
bash
npm install my-package
2. 配置
javascript
const myPackage = require(my-package);
myPackage.configure({ apiKey: your-key });
3. 使用
javascript
const result = myPackage.doSomething();
console.log(result); // 成功!
就是这样!更多内容请参见完整文档。
常见问题
模式:
markdown
常见问题
如何做 X?
简短回答并附上代码示例。
为什么 Y 会发生?
解释原因,如适用则提供解决方案。
我能做 Z 吗?
是/否,并解释如何做或为什么不能。
故障排除
模式:
markdown
故障排除
错误:连接被拒绝
原因: 服务器未运行。
解决方案: 使用 npm start 启动服务器。
错误:无效的 API 密钥
原因: 您的 API 密钥不正确或已过期。
解决方案:
- 1. 在设置中检查您的 API 密钥
- 如有需要,重新生成
- 更新您的配置
性能缓慢
原因: 大数据集未使用分页。
解决方案: 对于超过 1000 项的数据集使用分页:
javascript
client.getRecords({ limit: 100, offset: 0 });
工具和格式
Markdown 最佳实践
标题:
markdown
H1 - 标题
H2 - 主要章节
H3 - 子章节
H4 - 详情
代码块:
markdown
行内 代码 用于简短引用。
python
代码块用于示例
def example():
return 示例
表格:
markdown
值 5 | 值 6 |
列表:
markdown
- 1. 有序项
- 另一项
文档生成器
JSDoc(JavaScript):
javascript
/
* 计算两个数字的和。
* @param {number} a - 第一个数字
* @param {number} b - 第二个数字
* @returns {number} a 和 b 的和
* @example
* sum(2, 3) // 返回 5
*/
function sum(a, b) {
return a + b;
}
Python 文档字符串:
python
def calculate_average(numbers: list[float]) -> float:
计算数字列表的平均值。
Args:
numbers: 数值列表
Returns:
数字的算术平均值