style 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. .PP
  65. Ultimately, the goal is to write code that fits in with the other code
  66. around it and the system as a whole. If the file you are editing
  67. already deviates from these guidelines, do what it does. After you
  68. edit a file, a reader should not be able to tell just from coding
  69. style which parts you worked on.
  70. .SS COMMENTS
  71. If your code is readable, you shouldn't need many comments. A line or
  72. two comment above a function explaining what it does is always welcome.
  73. .PP
  74. Comment any code you find yourself wondering about for more than 2
  75. seconds, even if it's to say that you don't understand what's going
  76. on. Explain why.
  77. .PP
  78. Don't use commenting as an excuse for writing confusing code. Rewrite
  79. the code to make it clear.
  80. .SS EFFICIENCY
  81. Do the simple thing. Don't optimize unless you've measured the code
  82. and it is too slow. Fix the data structures and the algorithms
  83. instead of going for little 5% tunings.
  84. .SH SEE ALSO
  85. ``Notes on Programming in C'', Rob Pike,
  86. .br
  87. .B http://www.literateprogramming.com/pikestyle.pdf
  88. .SH BUGS
  89. Some programs use very different styles, for example,
  90. .IR rc .
  91. .PP
  92. Some programs and programmers diverge from the above rules due to
  93. habits formed long before these rules.
  94. Notably, some programs have a single space after a keyword and
  95. before an opening brace,
  96. and some initialize automatic variables at declaration.