# 缩进风格

缩进风格是一种约定，规范源代码块的缩进方式，使程序结构对读者可见：缩进字符的放置、其宽度，以及在以花括号分隔块的语言中，开括号和闭括号相对于它们所包围的语句位于何处<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup>。在大多数语言中，缩进纯粹是表现形式，但在包括 Python、Haskell 和 YAML 在内的一组语言中，缩进具有语法意义，决定了语句如何分组<sup>[2](https://docs.python.org/3/reference/lexical_analysis.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 规范代码块缩进以传达程序结构的规则<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup> |
| 主要 C 族风格 | K&R（亦称内核风格或唯一真括号风格）、奥尔曼（BSD）风格和 GNU 风格，主要区别在于括号位置<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup> |
| 常见缩进宽度 | K&R C 中为 8 个空格（或一个制表符），谷歌指南为每级 2 个空格，Python（PEP 8）、Java 和 C# 为 4 个空格<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup><sup> • </sup><sup>[3](https://developers.google.com/style/code-samples)</sup><sup> • </sup><sup>[4](https://pep8.org/)</sup><sup> • </sup><sup>[5](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions)</sup> |
| 制表符与空格 | Linux 内核强制要求 8 字符宽的制表符；LLVM、谷歌和微软则偏好空格<sup>[6](https://www.kernel.org/doc/html/v4.10/process/coding-style.html)</sup><sup> • </sup><sup>[7](https://www.llvm.org/docs/CodingStandards.html)</sup><sup> • </sup><sup>[3](https://developers.google.com/style/code-samples)</sup> |
| 显著缩进 | Python 的词法分析器计算每一行的缩进级别，并使用栈发出 INDENT 和 DEDENT 标记；制表符与空格混用不一致会引发 `TabError`<sup>[2](https://docs.python.org/3/reference/lexical_analysis.html)</sup> |
| 强制执行 | Prettier、Black 和 gofmt 等工具自动应用项目的缩进规则，EditorConfig 文件为编辑器声明 `indent_style`、`indent_size` 和 `tab_width`<sup>[8](https://www.libretxts.com/blog/post-4.html)</sup><sup> • </sup><sup>[9](https://spec.editorconfig.org/)</sup> |

## 主要 C 族风格

《行话文件》的记述确认了四种主要的 C 缩进风格，它们都旨在让读者更容易在视觉上追踪控制结构的范围，并且都通过其类 C 语法被 C++ 和 Java 继承。区分它们的关键变量是 `{` 和 `}` 相对于它们所包围的语句以及相对于控制语句（`if`、`else`、`for`、`while` 或 `do`）的位置<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup>。

**K&R 风格。** 以 Kernighan 和 Ritchie 命名，因为《C 程序设计语言》中的示例均采用此格式；亦称内核风格，因为 [Unix](https://www.edgepedia.org/zh/unix) 内核就是用这种风格编写的。在 C 代码中，每级正文通常缩进八个空格（或一个制表符）；在 C 中偶尔可见四个空格，但在 C++ 和 Java 中，四个空格往往成为规则。其拥护者称之为“唯一真括号风格”（1TBS），并主张其节省垂直空间，使屏幕上能同时容纳更多代码<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup>。

**奥尔曼风格。** 以 Eric Allman 命名，他是一位伯克利黑客，用这种风格编写了许多 BSD 工具；这种风格有时也称 BSD 风格。开括号独占一行<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup>。微软的 C# 规范正是这样规定的：括号使用“奥尔曼”风格，开括号和闭括号各占一个新行，与当前缩进级别对齐<sup>[5](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions)</sup>。

**GNU 风格。** 通用于 GNU Emacs 和[自由软件基金会](https://www.edgepedia.org/zh/free-software-foundation)的代码中，而据《行话文件》所述，几乎不见于其他地方<sup>[1](https://www.catb.org/jargon/html/I/indent-style.html)</sup>。

由于这些约定仅在形式上不同，它们可以通过机械方式转换。GNU 的 `indent` 程序通过插入或删除空白来改变 C 程序的外观，并可将一种 C 书写风格转换为另一种；诸如 `-i8` 之类的选项设置每级八个字符的缩进<sup>[10](https://www.gnu.org/software/indent/manual/indent.html)</sup>。

## 缩进宽度与制表符对空格之争

第二个选择维度是使用什么字符产生缩进以及每级多宽。谷歌的文档风格指南指出，对于大多数编程语言，代码示例应使用空格而非制表符，每级缩进两个空格，同时指出某些场景每级使用四个空格，某些则使用制表符<sup>[3](https://developers.google.com/style/code-samples)</sup>。PEP 8——Python 标准库的风格指南——规定每级缩进四个空格，并声明空格是首选的缩进方法，制表符仅用于与已用制表符缩进的代码保持一致<sup>[4](https://pep8.org/)</sup>。微软的 C# 规范同样要求使用四个空格而非制表符<sup>[5](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions)</sup>。

[Linux 内核](https://www.edgepedia.org/zh/linux-kernel)在字符选择上持相反立场：除注释、文档和 Kconfig 外，缩进绝不使用空格，制表符应为 8 字符宽<sup>[6](https://www.kernel.org/doc/html/v4.10/process/coding-style.html)</sup>。LLVM 编码标准解释了支持空格的常见论据：不同的编辑器和查看器将制表符展开为不同的制表位，可能使代码看起来难以阅读，因此 LLVM 在所有情况下都偏好空格<sup>[7](https://www.llvm.org/docs/CodingStandards.html)</sup>。

由于各项目意见不一，配置文件应运而生，以按目录记录选择。EditorConfig 规范定义了 `indent_style`（制表符或空格）、`indent_size` 和 `tab_width` 声明，一系列编辑器插件读取这些文件，使编辑器遵循所声明的风格<sup>[9](https://spec.editorconfig.org/)</sup>。

## 显著缩进

在 Python 中，逻辑行开头的空白用于计算该行的缩进级别，从而决定语句的分组<sup>[2](https://docs.python.org/3/reference/lexical_analysis.html)</sup>。词法分析器从左到右将制表符转换为 1 到 8 个空格，使得截至并包括替换内容在内的总字符数为 8 的倍数，这与 Unix 使用的规则相同。连续行的缩进级别通过栈生成 `INDENT` 和 `DEDENT` 标记：更深的级别压栈并发出一个 `INDENT`；更浅的级别弹栈，每移除一个级别就发出一个 `DEDENT`。如果文件以某种方式混用制表符和空格，使得含义取决于制表符相当于多少个空格，则会引发 `TabError`<sup>[2](https://docs.python.org/3/reference/lexical_analysis.html)</sup>。

这一设计是有意的权衡。支持者认为，在具有显著空白的语言中，代码不可能在视觉上暗示一种结构而在词法上意味着另一种结构，因为计算机读取的正是读者所看到的缩进；批评者则指出复制粘贴的脆弱性、制表符与空格不匹配以及代码生成更困难等问题<sup>[11](https://wiki.python.org/python/Why%2820%29separate%2820%29sections%2820%29by%2820%29indentation%2820%29instead%2820%29of%2820%29by%2820%29brackets%2820%29or%282027%29end%2827%29.html)</sup>。布局敏感语言也带来了自身的解析问题：关于缩进敏感解析的研究描述了这样的形式体系——后续表达式必须至少缩进到引入它的构造的第一个标记那么远；并且已有工具被构建出来，以声明式方式指定缩进规则，而非将其硬编码到解析器中<sup>[12](https://michaeldadams.org/papers/layout_parsing/LayoutParsing.pdf)</sup><sup> • </sup><sup>[13](https://eelcovisser.org/publications/2018/AmorimSEV18.pdf)</sup>。

在代码之外，缩进在标记语言中也可以承载含义。reStructuredText 仅使用缩进来表示块引用、定义列表中的定义以及嵌套列表或字面块内容<sup>[14](https://www.docutils.org/docs/ref/rst/restructuredtext.html)</sup>。

## 强制执行与自动化

**格式化工具终结争论。** 用于 JavaScript 的 Prettier、用于 Python 的 Black 和用于 Go 的 gofmt 等工具自动应用一致的缩进，从而消除了在格式方面进行手动风格审查评论的需要<sup>[8](https://www.libretxts.com/blog/post-4.html)</sup>。项目风格指南强化了这一点：LLVM 的规则是修改代码时遵循现有代码的风格，并避免添加行尾空白，否则会在差异中浮现无关的更改<sup>[7](https://www.llvm.org/docs/CodingStandards.html)</sup>。

文档有其自身的约定。谷歌的指南要求代码行在 80 个字符处换行以便在窄窗口中阅读，代码块标记为预格式化文本，在 Markdown 中使用四空格缩进<sup>[3](https://developers.google.com/style/code-samples)</sup>。

## 可读性与无障碍性

一项针对以人为中心的研究的系统性文献综述识别了 13 种格式元素（包括缩进）以及这些元素的 33 个级别（如两空格缩进），在 27 项比较中进行了考察。研究发现，适当使用块缩进在统计上更有利于代码可读性，而其他元素如整体格式布局则未显示显著效果，或得出了相互矛盾的结果<sup>[15](https://prg.is.titech.ac.jp/papers/pdf/jss2023.pdf)</sup>。一项针对新手开发者的眼动追踪研究比较了包括 PEP 8 在内的 Python 风格指南，发现受试者在某些构造上偏好 PEP 8 的建议，即使他们的眼动指标更差<sup>[16](https://arxiv.org/abs/2408.14566)</sup>。

缩进在网络上也很重要。W3C 的 WCAG 技术 G224 处理缩进传达含义的情况，如嵌套无序列表或代码：当内容重排到窄视口（如 320 CSS 像素）时，传达层次结构的缩进应予以保留，并在断点处调整样式，使结构保持清晰可读，而不强制进行二维滚动<sup>[17](https://www.w3.org/WAI/WCAG21/Techniques/general/G224)</sup>。

## References

1. [indent style, The Jargon File](https://www.catb.org/jargon/html/I/indent-style.html)
2. [2. Lexical analysis, Python 3 documentation](https://docs.python.org/3/reference/lexical_analysis.html)
3. [Code samples, Google developer documentation style guide](https://developers.google.com/style/code-samples)
4. [PEP 8 – Style Guide for Python Code](https://pep8.org/)
5. [.NET Coding Conventions – C#, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions)
6. [Linux kernel coding style](https://www.kernel.org/doc/html/v4.10/process/coding-style.html)
7. [LLVM Coding Standards](https://www.llvm.org/docs/CodingStandards.html)
8. [How to Format Code for Readability, libretxts](https://www.libretxts.com/blog/post-4.html)
9. [EditorConfig Specification 0.17.2](https://spec.editorconfig.org/)
10. [`indent`: Indent and Format C Program Source, GNU manual](https://www.gnu.org/software/indent/manual/indent.html)
11. [Why separate sections by indentation instead of by brackets or 'end', Python wiki](https://wiki.python.org/python/Why%2820%29separate%2820%29sections%2820%29by%2820%29indentation%2820%29instead%2820%29of%2820%29by%2820%29brackets%2820%29or%282027%29end%2827%29.html)
12. [Principled Parsing for Indentation-Sensitive Languages, Michael D. Adams](https://michaeldadams.org/papers/layout_parsing/LayoutParsing.pdf)
13. [Declarative Specification of Indentation Rules, Amorim et al. (2018)](https://eelcovisser.org/publications/2018/AmorimSEV18.pdf)
14. [reStructuredText Markup Specification, Docutils](https://www.docutils.org/docs/ref/rst/restructuredtext.html)
15. [A systematic literature review on the impact of formatting elements on code legibility (JSS 2023)](https://prg.is.titech.ac.jp/papers/pdf/jss2023.pdf)
16. [Assessing Python Style Guides: An Eye-Tracking Study with Novice Developers (2024)](https://arxiv.org/abs/2408.14566)
17. [WCAG Technique G224: Accounting for meaningful text indentation and Reflow, W3C](https://www.w3.org/WAI/WCAG21/Techniques/general/G224)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 25, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 25, 2026*

Topic: 社会与历史 › 语言与语言学 › 语言学 › Formal and computational linguistics › Syntax highlighting and source presentation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/indentation-style

English original: https://www.edgepedia.org/indentation-style
