conventions.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. indent with tabs up to the indent level, then spaces for visual alignment of code broken lines.
  3. int foo() {
  4. <tab>if(rand() < 10) {
  5. <tab><tab>if((some_really_long_var != some_really_long_expression) ||
  6. <tab><tab> (more_really_long_stuff == last_really_long_stuff)) {
  7. ^^^ spaces
  8. <tab><tab><tab>return 1;
  9. <tab><tab>}
  10. <tab>}
  11. <tab> <--- empty lines also indented
  12. <tab>return 0;
  13. }
  14. Note how the above if statement is broken onto a separate line. Avoid spreading the code too thin, like below:
  15. if(
  16. (some_really_long_var != some_really_long_expression) ||
  17. (more_really_long_stuff == last_really_long_stuff)
  18. ) {
  19. This is just ugly and hard to read. Two entire lines are wasted for the if( and the ) {, to no real benefit
  20. and in conflict with the general nature of the rest of the formatting. Judgement is key; the ugly version
  21. may be best for an if statement that spans 8 lines, but not for two or three.
  22. int* foo; <-- C++ style asterisk placement (with the type)
  23. int* foo, *bar; <--- multiple pointers on same line
  24. int***** foo;
  25. Never mix pointer and non-pointer variables in the same declaration:
  26. int* foo, bar; <--- bad
  27. int foo, *bar; <--- bad
  28. int* foo; \___ good
  29. int bar; /
  30. typedef struct node { <--- typedefs and opening braces go on the same line
  31. ^------ space before opening brace
  32. float x, y, z;
  33. union {
  34. unsigned long id;
  35. char* name;
  36. };
  37. <--------------- blank line for general clarity, separating the list pointers from the data
  38. struct node* next, *prev;
  39. } node_t; <--- closing brace at original indent, typedef'd name on same line
  40. ^^------ type name has _t suffix
  41. for(int i = 0, j = 1; i < (j + 1); i++) {
  42. ^^ ^^ ^ ^^ ^^^ ^ ^ space before opening braces
  43. | | | | | +--- x++ preferred to ++x where semantically equivalent
  44. | | | | +------- in for, no space before ; but yes space after unless the next token is also ; (see below)
  45. | | | +------------- no spaces after opening parens or before closing parens
  46. | | +---------------- spaces around operators
  47. | +-------------------------- space after , but not before
  48. +------------------------------------- no space between keywords and their opening paren
  49. for formatting:
  50. for(;;) { <-- never use; write while(1) instead
  51. for(int i = 0;;) {
  52. for(;x < 3;) { <-- never use; write while(x < 3) instead
  53. for(; i < 3; i++) {
  54. if(x < 10) { <-------- opening braces are always on the same line as the closing paren of their condition
  55. printf("single");
  56. } <------------------- closing braces are at the same indent level as the original keyword
  57. else if(x < 100) { <-- chained clauses are on the next line and the same indent level as the first
  58. printf("tens");
  59. }
  60. else {
  61. printf("big");
  62. }
  63. static const char* strstr(const char* haystack, const char* needle) { <--- opening brace on same line
  64. ^^^^^^^^^^^^^^^^^^ ^^--------------------^^-----------------^^^------ note space conventions
  65. +------------------------------------------------------------------ return values and all modifiers on the same line
  66. // function contents
  67. } <-- closing brace at original indent
  68. char* s = strstr(x, "foo");
  69. float excessively_long_arg = ( <----- don't nest excessively long computations inside a function call. move to a var beforehand
  70. <multiple lines of calculations>
  71. );
  72. int ret = complicated_function( <------ opening paren on the same line, also the return value assignment
  73. arg1, <-------------------------- arguments each on their own line, even if they are short
  74. really_really_long_arg2[with.some[other]->stuff],
  75. arg3_x, arg3_y, \__________ logically grouped arguments can be on the same line if they are short enough
  76. arg5_x, arg5_y, arg5_z, /
  77. excessively_long_arg <------------ see above
  78. ); <---------------------------------- closing paren on its own line at original indent level
  79. Declarations of functions with many arguments follows a similar pattern:
  80. int many_args_fn(
  81. <args>
  82. ) {
  83. <code>
  84. return 0;
  85. }
  86. Functions with many arguments should be avoided when reasonable. Many arguments is often a sign of bad design, structuring, or factoring.
  87. Everything should compile with no warnings on -std=std11 -Werror -Wall -Wextra -Wpedandic
  88. */