关于c++风格 code style

  1. Header files should be self-contained.
  2. All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be <PROJECT>_<PATH>_<FILE>_H_.
  3. You may forward declare ordinary classes in order to avoid unnecessary #includes. A "forward declaration" is a declaration of a class, function, or template without an associated definition. #include lines can often be replaced with forward declarations of whatever symbols are actually used by the client code.
  4. Use standard order for readability and to avoid hidden dependencies: Related header, C library, C++ library, other libraries‘ .h, your project‘s .h.
    • dir2/foo2.h.
    • C system files.
    • C++ system files.
    • Other libraries‘ .h files.
    • Your project‘s .h files.
  5. In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:
  6. Do not make nested classes public unless they are actually part of the interface, e.g., a class that holds a set of options for some method.
  7. Variables needed for if, while and for statements should normally be declared within those statements, so that such variables are confined to those scopes. E.g.:
  8. while (const char* p = strchr(str, ‘/‘)) str = p + 1;
  9. Constructors should never call virtual functions or attempt to raise non-fatal failures. If your object requires non-trivial initialization, consider using a factory function or Init() method.
  10. Use the C++ keyword explicit for constructors callable with one argument.
  11. Use delegating and inheriting constructors when they reduce code duplication.
  12. Inheriting constructors allow a derived class to have its base class‘s constructors available directly, just as with any of the base class‘s other member functions, instead of having to redeclare them. This is especially useful if the base has multiple constructors.
  13. Only very rarely is multiple implementation inheritance actually useful. We allow multiple inheritance only when at most one of the base classes has an implementation; all other base classes must be pure interface classes tagged with the Interface suffix. Multiple inheritance allows a sub-class to have more than one base class. We distinguish between base classes that are pure interfaces and those that have an implementation.
  14. All parameters passed by reference must be labeled const.
  15. Use streams only for logging.
  16. Use prefix form (++i) of the increment and decrement operators with iterators and other template objects.
  17. document that a variable is non-negative using assertions. Don‘t use an unsigned type.
  18. The names of variables and data members are all lowercase, with underscores between words. Data members of classes (but not structs) additionally have trailing underscores. For instance: a_local_variable, a_struct_data_member, a_class_data_member_. Data members of structs, both static and non-static, are named like ordinary nonmember variables. They do not have the trailing underscores that data members in classes have.
  19. There are no special requirements for global variables, which should be rare in any case, but if you use one, consider prefixing it with g_ or some other marker to easily distinguish it from local variables.
  20. Regular functions have mixed case; accessors and mutators match the name of the variable: MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(), set_my_exciting_member_variable().
  21. Use a k followed by mixed case, e.g., kDaysInAWeek, for constants defined globally or within a class.const int kDaysInAWeek = 7;
  22. Enumerators should be named either like constants or like macros: either kEnumName or ENUM_NAME.
  23. Empty loop bodies should use {} or continue, but not a single semicolon.
  24. both of the && logical AND operators are at the end of the line.

摘自:http://google-styleguide.googlecode.com/svn/trunk/cppguide.html

时间: 2024-10-09 21:11:55

关于c++风格 code style的相关文章

ios code style

注释 建议使用VVDocumenter插件 多行注释 格式: /** 注释内容 */ 单行注释 格式: ///在对文件.类.函数进行注释时推荐使用多行注释,在函数体内对代码块进行注释时,使用单行注释 函数的注释 函数注释的格式为 /** * @brief * @param * @return **/ 在brief中需要写明函数的主要功能.注意事项 在param中需要写明函数的变量类型.变量的作用 在return中需要写明函数的返回类型.返回值的作用 如有其他需要说明的地方,可以在@return后

idea google code style

1.https://github.com/google/styleguide 下载google code style风格配置xml 2.自己操作系统所属目录\.IntelliJIdea15\config\codestyles\  没有codestyles文件目录的自己创建一个把  intellij-java-google-style.xml 考进去 3.启动idea,全局setting中editor-code style-scheme选择googleStyle ps:自己用什么语言到时候用对应的

拯救 Java Code Style 强迫症

2018年10月31日 21:20:46 Java架构大数据 阅读数:1更多个人分类: java 架构 linux 程序猿编辑这篇文章缘起于上一个持续交付的咨询项目,当时正在指导客户团队的Java工程师做Code Review,发现一个很有意思的现象:有一位工程师对Code Style特别在意,所以在Code Review的大部分时间中都是该工程师在指出哪里哪里的格式不对,但是团队并没有找到改进方法,每次的结论都是"下次我注意一点."我挺欣赏这位工程师对Code Style的认真态度,

wyh 的 Code Style

参考GNAQ学长大人的Code Style Rust式代码风格: 预编译指令: 顺序: pragma include define undef 缩进: define undef 继承上层缩进,其余不缩进. 写法: include 中能使用 < > 的尽量不要使用 " " : include 中不使用空格. define undef 中允许被定义的宏为全部大写或全部小写,其余情况不允许使用大写. 限制: 不使用 #if #else #elif #endif #ifdef #i

szTom&#39;s Code Style

介绍szTom在C++中使用的代码风格. 头文件 必须使用using namespace std; 如果是C头文件,必须使用c前缀文件名. #include <cstdio> 而不是 #include <stdio.h> 所有的#include必须放置于程序开头 预处理 所有的预编译指令(包括 #ifdef 等)不能缩进. 代码不能出现魔鬼数字,必须使用#define 用#define声明的宏函数,减少空格的使用. 代码 缩进 对于每个代码块,使用与4个空格等长的 Tab 缩进.

VS2015--win32工程配置的一些想法之Google Code Style中头文件的顺序

工程大了,有很多的头文件,也要引用很多的库文件. 从我们学习C++写hello world的那一刻起,就知道要包含一些系统文件. 那么顺序如何呢? 在review的时候,感觉自己写的东西就是一坨屎. 看看Google code style中是如何描述include文件顺序的: Names and Order of Includes Use standard order for readability and to avoid hidden dependencies: C library, C++

[code style]javascript style

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 var globalVariable=null; var globalObject={     init:function(){         $.extend({method:function(e){e}});    //jquery static method extend         $.fn.extend(); //jquery instance method extend  

IDEA学习系列之剖析IDEA里的Code Style(适合各种语言)(不断更新)(图文详解)

不多说,直接上干货! File  -> Settings ->  Editor  ->   Code Style   (1)HOCON 分为: Tabs  and Indents . Spaces . Wrapping and Braces 和  Blank Lines (2)Scala 分为:Tabs and Indents.Spaces.Wrapping and Braces.Blank Lines.ScalaDoc.Imports.Multi-line strings.Type A

Code Style for OI

Code Style for OI #include #define 尽量少用 #include 能#include <foo>就不#include "foo" #if,#endif等预编译指令 尽量别用,用的话给出与之对应的指令 using 不使用using namespace foo,如有需要应使用using foo::bar 对于using foo = bar;不限制 缩进 4格缩进 大括号 大括号不换行才是最优雅的!大括号换行的都是异端! 大括号换行 空格 能用就用