c99_style.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. WIP
  2. This is my C99 formatting style guide.
  3. GENERAL
  4. -------
  5. - Use empty spaces to nicely separate chunks of code that belongs together,
  6. e.g.:
  7. int a = x;
  8. char b = y;
  9. if (a > b)
  10. doSomething();
  11. if (something())
  12. doSomethingElse();
  13. - Undef macros when no longer needed. This prevents clashes.
  14. INDENTATION
  15. -----------
  16. Use two (2) SPACES for indentation, NEVER use tabs.
  17. Reasons:
  18. - 1 space is too few, 3 is retarded, 4 is too many (makes code hard to keep
  19. in 80 columns). 2 is just perfect for people with normal sight.
  20. - Tabs look like spaces but aren't, they're very sneaky and messy and fuck
  21. up the formatting depending on how each editor handles them (how long?
  22. wrapping? etc.). They are non-standard characters that behave differently
  23. than any other character that just takes exactly 1 space. Tabs also get
  24. mixed with spaces when you want to align to non-tab width multiple. When
  25. tabs are on, editor does shit like automatically replace spaces with tabs,
  26. you don't want your editor to be smart and fuck up your code behind your
  27. back. Tabs force bloat in text editors. Tabs just bother people who don't
  28. use them and don't have them configured, this isn't he case the other way
  29. around (there isn't anyone who doesn't have spaces configured).
  30. IDENTIFIERS
  31. -----------
  32. Generally, for identifiers with big scope (globals etc.) use descriptive,
  33. self-documenting identifiers, e.g. "getTimeSecs" instead of "time". For
  34. smaller scope identifiers (e.g. local variable in a short function) short
  35. identifiers like "x", like in mathematical formulas, can be used as the
  36. programmer can see what's in the variable. The same goes for macro names.
  37. Variables and functions: camelSpace with lowercase first letter, e.g.:
  38. myVartiable
  39. myNiceFunction
  40. Data types: CamelSpace with uppercase first letter, e.g.:
  41. MyInt
  42. SceneArray
  43. In code that is meant to be reused elsewhere use a namespace prefix for glabal
  44. identifiers, typically formed by three uppercase letters (usually an
  45. abbreviation of project's name) followed by an underscore, e.g. for My Awesome
  46. Project a global identifier "var" would be:
  47. MAP_var
  48. Prefix private/internal global variables with underscore to avoid clashes with
  49. normal identifiers, e.g.:
  50. _MAP_privateVar
  51. CURLY BRACKETS
  52. --------------
  53. Write curly brackets like this:
  54. if (condition)
  55. {
  56. doA();
  57. doB();
  58. }
  59. Reasons:
  60. - Visually very well readable, same level brackets are aligned. The lines
  61. with brackets also create nice empty spaces separating chunks of code.
  62. - Results in more LOC, forcing the programmer a little more to write shorter
  63. code.
  64. If not required (a single command in block), don't use brackets, except with
  65. tricky structures (e.g. if-else inside an if).
  66. LINE LENGTH
  67. -----------
  68. Maximum line length: 80
  69. Reasons:
  70. - When using two columns in vim on smaller displays, 80 columns fit just
  71. nicely without overflow/wrapping.
  72. - When making a paper backup, 80 lines fit just nicely into two columns on
  73. A4 paper and use the page size very efficiently.
  74. - LOC will correlate better with code complexity, as with very long line can
  75. be worth many a shorter lines.
  76. - Forces the programmer a little bit to write shorter code as shorter lines
  77. result in more LOC and create pressure on keeping it shorter and write
  78. more compact expressions that fit to a single line.
  79. - This can make processing easier for simple compilers or text editors that
  80. have limited line length.
  81. - It just looks cool.
  82. COMMENTS
  83. --------
  84. Generally use comments to make each file self-documented, do NOT rely on
  85. other files to provide crucial info (like a license, author, ...).
  86. At the beginning of each file always put a comment with:
  87. - Short file description. In bigger files include also basic documentation,
  88. like API explanation, usage explanation etc.
  89. - Author, year of creation (for copyright).
  90. - Waiver of all copyright and intellectual property (at worst a permissive
  91. license).
  92. Use Doxygen style comments so that auto doc can easily be created.