C/C++ - 编码规范(USNA版)

[IC210] Resources/C++ Programming Guide and Tips

所有提交的评分作业(作业、项目、实验、考试)都必须使用本风格指南。本指南的目的不是限制你的编程,而是为你的程序建立统一的风格格式。

* 这将有助于你调试和维护程序。

* 有助于他人(包括指导教师)理解您的程序。

This style guide is mandatory for all submitted work for grading (homework, projects, labs, exams). The purpose of the guide is not to restrict your programming but rather to establish a consistent style format for your programs.

* This will help you debug and maintain your programs.

* It will help others (including your instructor) to understand them.

Whitespace and Visual Layout

"空白 "指换行符、空格字符和制表符。在源代码中,我们一般不使用制表符。事实上,emacs 会用适当数量的空格字符替换制表符。

"Whitespace" refers to newlines, space characters and tabs. In source code we do not generally use tabs. In fact, emacs replaces tab characters with an appropriate number of space characters.

在很多情况下,为了让编译器正确编译源代码,我们并不需要空格,尽管编译出来的结果我们无法阅读。我们主要通过三种方式使用空格来格式化代码,以达到最清晰的效果:

In many circumstances whitespace is not required in order for compilers to correctly compile source code, even though the result is unreadable to us. There are three major ways we use whitespace to format code for maximum clarity:

* 缩进: 每一级块嵌套必须缩进一致的空格数(通常为两空格)。

* 空行: 在源代码中,使用空行来分隔完成不同任务的代码块。虽然在某种程度上这是一种品味和艺术,但你应该期望用空行来分隔不同的函数,而一个长的函数体通常也应该用空行来分割成若干块。

* 用空格分隔代码元素: 我们建议在运算符和操作数之间使用空格,除非通过省略空格来突出优先级。

* Indentation: Each level of block nesting must be indented by a consistent number of spaces (usually two spaces).

* Blank Lines: In source code, use blank lines to separate chunks of code that accomplish distinct tasks. While to some extent this is taste and art, you should expect distinct functions to be separated by blank lines, and a long function body should usually be broken into chunks by blank lines as well.

* Whitespace separating elements of code: We recommend using whitespace between operators and operands, except where precedence is highlighted by leaving out spaces.

Comments

注释的缩进应与其描述的代码块一致。注释可分为战略和战术两大类。

  • 应在代码开头给出文件名和作者。

  • 策略性注释用于向读者概述正在发生的事情。这些注释出现在文件开头、重要函数之前和重要代码块之上。策略性注释通常以句子的形式编写,重点是解释代码块设计的全貌。

  • 战术性注释旨在解释代码中棘手的地方

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值