style 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. .TH STYLE 6
  2. .SH NAME
  3. style \- Plan 9 coding conventions for C
  4. .SH DESCRIPTION
  5. Plan 9 C code has its own conventions.
  6. You would do well to follow them.
  7. Here are a few:
  8. .IP • 3
  9. don't use
  10. .L //
  11. comments; some old Plan 9 code does, but we're converting it as we touch it.
  12. We do sometimes use
  13. .L //
  14. to comment-out a few lines of code.
  15. .IP •
  16. avoid
  17. .BR goto s.
  18. .IP •
  19. no tabs expanded to spaces.
  20. .IP •
  21. surround a binary operator (particular a low precedence one) with spaces;
  22. don't try to write the most compact code possible
  23. but rather the most readable.
  24. .IP •
  25. no white space before opening braces.
  26. .IP •
  27. no white space after the keywords
  28. .LR if ,
  29. .LR for ,
  30. .LR while ,
  31. etc.
  32. .IP •
  33. no braces around single-line blocks (e.g.,
  34. .LR if ,
  35. .LR for ,
  36. and
  37. .L while
  38. bodies).
  39. .IP •
  40. integer-valued functions return -1 on error, 0 or positive on success.
  41. .IP •
  42. functions that return errors should set
  43. .IR errstr (2).
  44. .IP •
  45. variable and function names are all lowercase, with no underscores.
  46. .IP •
  47. .B enum
  48. or
  49. .BR #define d
  50. constants should be Uppercase (or UPPERCASE).
  51. .IP •
  52. .B struct
  53. tags are Uppercase, with matching
  54. .BR typedef s.
  55. .IP •
  56. automatic variables (local variables inside a function) are
  57. never initialized at declaration.
  58. .IP •
  59. follow the standard idioms: use
  60. .L "x < 0"
  61. not
  62. .LR "0 > x" ,
  63. etc.
  64. .IP •
  65. don't write
  66. .L !strcmp
  67. (nor
  68. .LR !memcmp ,
  69. etc.);
  70. always explicitly compare the result of string comparison with zero using
  71. a relational operator.
  72. .PP
  73. Ultimately, the goal is to write code that fits in with the other code
  74. around it and the system as a whole. If the file you are editing
  75. already deviates from these guidelines, do what it does. After you
  76. edit a file, a reader should not be able to tell just from coding
  77. style which parts you worked on.
  78. .SS COMMENTS
  79. If your code is readable, you shouldn't need many comments. A line or
  80. two comment above a function explaining what it does is always welcome.
  81. .PP
  82. Comment any code you find yourself wondering about for more than 2
  83. seconds, even if it's to say that you don't understand what's going
  84. on. Explain why.
  85. .PP
  86. Don't use commenting as an excuse for writing confusing code. Rewrite
  87. the code to make it clear.
  88. .SS EFFICIENCY
  89. Do the simple thing. Don't optimize unless you've measured the code
  90. and it is too slow. Fix the data structures and the algorithms
  91. instead of going for little 5% tunings.
  92. .SH SEE ALSO
  93. ``Notes on Programming in C'', Rob Pike,
  94. .br
  95. .B http://www.literateprogramming.com/pikestyle.pdf
  96. .SH BUGS
  97. Some programs use very different styles, for example,
  98. .IR rc .
  99. .PP
  100. Some programs and programmers diverge from the above rules due to
  101. habits formed long before these rules.
  102. Notably, some programs have a single space after a keyword and
  103. before an opening brace,
  104. and some initialize automatic variables at declaration.