---
id: 487
title: "鄙人之代码规范"
slug: my-code-style
lang: zh
date: 2019-01-07T22:23:09
date_gmt: 2019-01-07T14:23:09
modified: 2019-01-07T22:23:09
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, 代码规范]
original_url: https://kalorona.com/oi/鄙人之代码规范/
archived: 2026-11-27
editor: classic
media: []
archive_notes:
  - "Body headings were <h1> in the original; demoted to level 2 so the page title is the only h1."
  - "The table's header row was <th> cells inside <tbody>; moved into <thead> so it renders as a header."
comments:
  - id: 45
    parent: 0
    type: comment
    author: crazydave
    url: "http://www.crazydave.top"
    owner: false
    date: 2019-01-07T22:24:00
    content: |-
      菜死了
  - id: 48
    parent: 0
    type: comment
    author: lornd
    url: ""
    owner: false
    date: 2019-01-21T12:53:13
    content: |-
      大括号永不换行！
---

## 大体的几条规则

1. 大括号换行（至少在 C 系语言里是这样的，JavaScript 随意，但是我没有见过谁竞赛用 JavaScript）
2. 尽量用题目里的变量名。
3. 临时变量（比如说遍历变量）可以用一个字母或者是两位字母缩写来命名。
4. 全局变量、生成周期长的字段尽量小驼峰命名。
5. 函数、函数变量命名使用小驼峰。
6. 赋值的构造函数、初始化的函数一行搞定（比如说初始一条边或者是并查集初始化）
7. 少用 #define，多用 const。
8. 循环里或者是条件语句中用逗号压行（不要过长）。
9. 使用现代编辑器进行自动格式化（墙裂安利 VSCode，告别 F\*\*K 调试法）。

示例代码：

```cpp
// snippet.cpp
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX_N = 100;
int n, seqa[MAX_N], seqb[MAX_N], ans;
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &seqa[i]), scanf("%d", &seqb[i]);
    for (int i = 1; i <= n; i++)
        ans += seqa[i] - seqb[i];
    printf("%d", ans);
    return 0;
}
```

## 变量名表

| 变量意义 | 变量名 |
|---|---|
| 答案 | ans |
| 栈 | st |
| 队列 | q |
| 最大值 | MAX_??? |
| 最小值 | MIN_??? |
| 数组 | arr[] |
| 多个数组或数列 | seq?[] |
| 源数据/起点 | src |
| 目标数据/终点 | dst |
| 距离 | dist |
