Elements of Programming Style -- The C++ Style Guide

本文围绕C++代码编写展开,介绍了头文件和实现文件的后缀及内容,如头文件含类、结构体等声明,实现文件含函数方法实现。还阐述了命名约定,包括不同命名风格及应用场景,同时对代码的空格、换行、注释等规范做了说明,旨在提升代码可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Elements of Programming Style -- The C++ Style Guide
January 23, 1996
Version 1.0

Neill Kipp

Files

Header files have a ".h" suffix. Header file contains class, struct, and union declarations, enum declarations, #defines, typedefs.

Implementation files have a ".cc" (UNIX) or ".cpp" (Windows, DOS) suffix. Implementation files contain function and method implementations.

Make a header block in header files and source code files. The header should contain title, author, date, and project information as well as a simple description of how the file fits into the project.

Names of Things

Names of Common C++ Characters

{open brace, open curly
}close brace, close curly
(open parenthesis, open paren
)close parenthesis, close paren
[open bracket
]close bracket
.period, dot
!exclamation point, bang, not
|bar, vertical-bar, or, or-bar (actually a "vertical virgule")
&ampersand, and, reference, ref
*asterisk, multiply, star, pointer
/slash, divide
//slash-slash, comment
#pound
\backslash, (sometimes "escape")
~tilde

The primitive type name "char" is usually pronounced like the first syllable of "charcoal." Sometimes it is pronounced like "care" and sometimes "car."

Names and Indentation

Names of Naming Conventions

  • interspersed_underscores lowercaseMixedCapital CapitalMixedCapital ALL_UPPERCASE

Applications of Naming Conventions

  • enumeration_item_name
  • variableName
  • TypeName, ClassName, MethodName()
  • UnixFileName.cc
  • dosfn.cpp
  • POUND_DEFINES

Self-documenting Code

  • Use long names for every name in your program.

No Magic Numbers

  • Numeric constants besides 0 (and sometimes 1) are not allowed. Use constants or #defines.

Whitespace

  • Space (what you get when you press spacebar)
  • Newline (what you get when you press enter)
  • Tab (substitutes for 8 spaces)

Space and Indentation

  • After an open brace, indent every subsequent line four spaces until the matching close brace.
  • If an if, while, or for has no brace following, indent the next line two spaces.
  • Indent lines which end in a colon backward two spaces (public, case).
  • A space precedes and follows reserved words (if, else, class, struct) unless preceded or followed by indentation or newlines or special punctuation.
  • A space precedes and follows operators and comparators (except unary operator bang is not followed by a space).
  • Pointer variants (ampersand, star) are preceded and followed by space in declarations.
  • Pointer variants (ampersand, star) are preceded (but not followed) by a space in expressions.
  • A space follows an open parenthesis.

Newline

  • Newline precedes an open brace in the following: class, struct, union, enum, method, function (but not: if, else, do, for, while, switch --- these braces are preceded by a single space.)
  • Newline follows a close brace for method, function, if, else, do, for, while, switch.
  • Semi-colon, then newline, then blank line after close brace for class, struct, union.
  • Newline follows an open brace.

Comments

  • Comments always begin at current indentation level with "//" and a space.
  • No other construct may appear on the same line as a comment.
  • Comments always preceed the construct they address.
  • Use complete sentences in a comment.
  • When describing a statement, comments may be in the imperative.

Above all, be guided by what pleases the eye. Be guided by what makes your code MORE READABLE.

Header File Example


Source code file example


Other examples


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值