zoukankan      html  css  js  c++  java
  • 关于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

    关于命名空间:

    https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/naming/

    命名空间和目录层相对应,不需要额外的缩进。

    https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/formatting/

    布尔表达式的逻辑操作永远放在行尾。

  • 相关阅读:
    css实现自适应正方形
    遇到稍微复杂的场景发现css功力不足
    聊聊缓存
    git学习笔记
    font-size:0的作用
    移动端高清屏适配方案
    react生命周期
    javascript写定时器
    js判断字符串是否以某个字符串开头和js分解字符串
    json.parse()和json.stringify()
  • 原文地址:https://www.cnblogs.com/linyx/p/4153670.html
Copyright © 2011-2022 走看看