The official µGFX library repository.

codingstyle.txt 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. uGFX coding style
  2. This is a short document describing the preferred coding style for uGFX.
  3. Chapter 1: Indentation
  4. Tabs are 4 characters, and thus indentations are also 4 characters.
  5. Rationale: We like 4 character tabs much better than 8 character tabs.
  6. It is more readable.
  7. Chapter 2: Placing Braces
  8. The preferred way, as shown to us by the prophets Kernighan and Ritchie,
  9. is to put the opening brace last on the line, and put the closing brace first,
  10. thusly:
  11. if (x is true) {
  12. we do y
  13. }
  14. However, there is one special case, namely functions: they have the
  15. opening brace at the beginning of the next line, thus:
  16. int function(int x)
  17. {
  18. body of function
  19. }
  20. We will however accept braces in the general block style for functions
  21. but not the other way around. General blocks MUST have their opening brace
  22. on the same line as the conditional statement.
  23. Note that the closing brace is empty on a line of its own, _except_ in
  24. the cases where it is followed by a continuation of the same statement,
  25. ie a "while" in a do-statement or an "else" in an if-statement, like
  26. this:
  27. do {
  28. body of do-loop
  29. } while (condition);
  30. and
  31. if (x == y) {
  32. ..
  33. } else if (x > y) {
  34. ...
  35. } else {
  36. ....
  37. }
  38. Note that closing brace is indented to the level of the start of the block.
  39. Structure definitions are an optional exception. Both of the below style are
  40. acceptable:
  41. typedef struct {
  42. int a;
  43. ...
  44. } mytype;
  45. struct mystruct {
  46. int a;
  47. ...
  48. }
  49. Chapter 3: Naming
  50. C is a Spartan language, and so should your naming be. Unlike Modula-2
  51. and Pascal programmers, C programmers do not use cute names like
  52. ThisVariableIsATemporaryCounter. A C programmer would call that
  53. variable "tmp", which is much easier to write, and a lot less
  54. difficult to understand.
  55. HOWEVER, while long mixed-case names are frowned upon, descriptive names for
  56. global variables are a must. To call a global function "foo" is a
  57. shooting offense.
  58. GLOBAL variables (to be used only if you _really_ need them) need to
  59. have descriptive names, as do global functions. If you have a function
  60. that counts the number of active users, you should call that
  61. "countActiveUsers()" or similar, you should _not_ call it "cntusr()".
  62. WHERE long names are required as described above, we prefer the use of
  63. capitalisation on subsequent words (but not the first) rather than underscores
  64. to seperate the words. For example "countActiveUsers()" is preferred to
  65. "count_active_users()" as it is at least as readable and is shorter.
  66. Encoding the type of a function into the name (so-called Hungarian
  67. notation) is brain damaged - the compiler knows the types anyway and can
  68. check those, and it only confuses the programmer.
  69. LOCAL variable names should be short, and to the point. If you have
  70. some random integer loop counter, it should probably be called "i".
  71. Calling it "loopCounter" is non-productive, if there is no chance of it
  72. being mis-understood. Similarly, "tmp" can be just about any type of
  73. variable that is used to hold a temporary value.
  74. Chapter 4: Functions
  75. Functions should be short and sweet, and do just one thing.
  76. The maximum length of a function is inversely proportional to the
  77. complexity and indentation level of that function. So, if you have a
  78. conceptually simple function that is just one long (but simple)
  79. case-statement, where you have to do lots of small things for a lot of
  80. different cases, it's OK to have a longer function.
  81. However, if you have a complex function, and you suspect that a
  82. less-than-gifted first-year high-school student might not even
  83. understand what the function is all about, you should adhere to the
  84. maximum limits all the more closely. Use helper functions with
  85. descriptive names (you can ask the compiler to in-line them if you think
  86. it's performance-critical, and it will probably do a better job of it
  87. that you would have done).
  88. Another measure of the function is the number of local variables. They
  89. shouldn't exceed 5-10, or you're possibly doing something wrong. Re-think the
  90. function, and split it into smaller pieces. A human brain can
  91. generally easily keep track of about 7 different things, anything more
  92. and it gets confused. You need to understand what you did 2 weeks from now.
  93. Because uGFX is intended for embedded platforms there are other considerations
  94. that may cause exceptions or emphasise the above. For example, stack space is
  95. a premium. This means that the number of local variables should be minimised as
  96. should the number of parameters. Passing through multiple levels of functions
  97. with lots of parameters is very bad indeed and this can override the desire to
  98. keep functions short and sweet. Clarity however is still essential.
  99. Chapter 5: Commenting
  100. Comments are good, but there is also a danger of over-commenting. NEVER
  101. try to explain HOW your code works in a comment: it's much better to
  102. write the code so that the _working_ is obvious, and it's a waste of
  103. time to explain badly written code. Generally, you want your comments to tell
  104. WHAT your code does, not HOW.
  105. We use doxygen to document the system. That means that most public functions
  106. are documented in the header defintion file. We do not put doxygen comments in
  107. the source file itself.
  108. Within the source file, comments should be used to seperate blocks of functions
  109. or definitions within the file. This is to provide clarity to the structure of
  110. the source file itself. An example could be:
  111. /***************************
  112. * Drawing Functions
  113. ***************************/
  114. Single line comments using "//" to start the comment should be used for just that
  115. purpose, to assist in the understanding of that single line. Mutliple single line
  116. comments should never be used to create a block comment. For example,
  117. // This is a very long
  118. // comment spanning several
  119. // lines
  120. is a very bad use of comments.
  121. Comments within function bodies should be small comments to note or warn
  122. about something particularly clever (or ugly), but try to avoid excess.
  123. Instead, put the comments at the head of a block of code to explain the block
  124. rather than a comment on each line.