internals-coding-standard.txt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. Manticoresearch coding standard
  2. ===============================
  3. WARNING. This document is just an internal note. It might or might not be
  4. in sync with the actual code. Use it as an overview; refer to the source
  5. for precise details.
  6. General
  7. --------
  8. This document describes C++ coding standard that MUST be used for
  9. Manticoresearch source code.
  10. It is not yet complete. Currently, we're not aiming for an exhaustive set
  11. of rules (that noone will read anyway). Rather, we're covering only those
  12. gray areas that are not immediately obvious from the source. However,
  13. you should also *first* refer to the source for general look and feel.
  14. Certain rules might change over time. However, the following rules are not
  15. going to be changed:
  16. - All source parts must look alike.
  17. - When in doubt, ask. If not answered, mimic. (And ask again.)
  18. 1. General formatting rules
  19. ----------------------------
  20. - No duplicate spaces, use tabs.
  21. - Indent is 1 tab.
  22. - Tab size is 4.
  23. 2. Formatting C++ clauses
  24. --------------------------
  25. 2.1. Enumeration
  26. -----------------
  27. enum Example_e
  28. {
  29. FIRST = mandatory_value,
  30. SECOND,
  31. THIRD,
  32. FOURTH
  33. };
  34. Same for typed enums (aka enum class)
  35. enum class TypedExample_e
  36. {
  37. FIRST,
  38. SECOND,
  39. THIRD,
  40. FOURTH
  41. };
  42. 2.2. Switch statement
  43. ----------------------
  44. switch ( iMyCondition )
  45. {
  46. case FIRST: iShortStatement = 1; break;
  47. case SECOND: iAnotherShortStatement = 2; break;
  48. case THIRD:
  49. iLongerStatement = 3;
  50. DoSomething();
  51. break;
  52. case FOURTH:
  53. {
  54. int iEvenLongerStatementWithLocals = 4;
  55. DoSomethingElse();
  56. break;
  57. }
  58. }
  59. 2.3. Parentheses
  60. -----------------
  61. - 1 space around opening '('.
  62. - 1 space before closing ')'.
  63. - Empty argument lists in function calls can omit spaces.
  64. - Short 1-argument lists can omit spaces too.
  65. TypicalCall ( iFirst, sSecond );
  66. NullArglist();
  67. ShortArglist(i);
  68. StillShortArglist(iIdx);
  69. LongEnoughArglist ( iFirst );
  70. iVar = !( iCode & BOOLEAN_EXPRESSION );
  71. if ( i==1 || ( j==2 && k==3 ) ) { ... }
  72. if ( i ) {... }
  73. 2.4. Class declaration layout
  74. ------------------------------
  75. - Usually, variables go first, then functions.
  76. - Usually, public members go first, then protected, then private.
  77. - All the rules above are not mandatory, but generally suggested.
  78. class SampleClass_c
  79. {
  80. public:
  81. SampleClass_c ();
  82. ~SampleClass_c ();
  83. void DoThings ();
  84. protected:
  85. int m_iLocalState;
  86. bool m_bSomeFlag;
  87. int m_iAnotherLogicalBlock;
  88. int m_iOfMemberVariables;
  89. void Helper1();
  90. void Helper2();
  91. };
  92. 3. Naming conventions
  93. ----------------------
  94. - Camel case, and reasonable subset of Hungarian notation.
  95. - Identifiers are "MultiWordName", not "multi_word_name" (this is CamelCase).
  96. - "m_" prefix on data members is mandatory (this is Hungarian).
  97. - Single-char typeid prefix on variables is mandatory (this is Hungarian).
  98. - "i" means int
  99. - "u" means unsigned
  100. - "b" means boolean
  101. - "c" means char
  102. - "f" means float
  103. - "h" means hash, i.e. associative array (like std::map)
  104. - "p" means pointer
  105. - "pp" means pointer to pointer
  106. - "s" means string (CSphString)
  107. - "sz" means char *
  108. - "e" means enum
  109. - "d" means array (no idea why "d", maybe "data"?)
  110. - "t" means any other (complex) type
  111. - Single-char variable names (same as typeid) are allowed for iterator variables
  112. - auto variable names should be prefixed by the typeid of their implied type:
  113. auto sNewString = sOldString;
  114. - No special rules for public/protected/private member names.
  115. - Enum names must end with "_e": SomeEnum_e
  116. - Interface names must end with "_i": SomeInterface_i
  117. - Class names must end with "_c": SomeClass_c
  118. - Struct names must end with "_t": SomeStruct_t
  119. class SampleClass_c
  120. {
  121. int m_iSomething; // right, got both "m_" prefix and "i" typeid
  122. char iAnotherthing; // WRONG, bad typeid char, bad capitalization
  123. long m_AnotherField; // WRONG, missing typeid char
  124. char * m_lpszWtf; // WRONG, typeid must be single char (or "pp", "sz")
  125. ...
  126. /// right
  127. void SampleCall ( RuleType_e eRule, char cKey, bool bFlag, char * szArg );
  128. };
  129. - constants, either typed or defined, must be all caps:
  130. const bool FAIL_ON_NULL_SOURCE = false;
  131. #define READ_NO_SIZE_HINT 0
  132. --eof--