You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.9 KiB

11 months ago
  1. # ThrowTheSwitch.org Coding Standard
  2. Hi. Welcome to the coding standard for ThrowTheSwitch.org. For the most part,
  3. we try to follow these standards to unify our contributors' code into a cohesive
  4. unit (puns intended). You might find places where these standards aren't
  5. followed. We're not perfect. Please be polite where you notice these discrepancies
  6. and we'll try to be polite when we notice yours.
  7. ;)
  8. ## Why Have A Coding Standard?
  9. Being consistent makes code easier to understand. We've made an attempt to keep
  10. our standard simple because we also believe that we can only expect someone to
  11. follow something that is understandable. Please do your best.
  12. ## Our Philosophy
  13. Before we get into details on syntax, let's take a moment to talk about our
  14. vision for these tools. We're C developers and embedded software developers.
  15. These tools are great to test any C code, but catering to embedded software has
  16. made us more tolerant of compiler quirks. There are a LOT of quirky compilers
  17. out there. By quirky I mean "doesn't follow standards because they feel like
  18. they have a license to do as they wish."
  19. Our philosophy is "support every compiler we can". Most often, this means that
  20. we aim for writing C code that is standards compliant (often C89... that seems
  21. to be a sweet spot that is almost always compatible). But it also means these
  22. tools are tolerant of things that aren't common. Some that aren't even
  23. compliant. There are configuration options to override the size of standard
  24. types. There are configuration options to force Unity to not use certain
  25. standard library functions. A lot of Unity is configurable and we have worked
  26. hard to make it not TOO ugly in the process.
  27. Similarly, our tools that parse C do their best. They aren't full C parsers
  28. (yet) and, even if they were, they would still have to accept non-standard
  29. additions like gcc extensions or specifying `@0x1000` to force a variable to
  30. compile to a particular location. It's just what we do, because we like
  31. everything to Just Work™.
  32. Speaking of having things Just Work™, that's our second philosophy. By that, we
  33. mean that we do our best to have EVERY configuration option have a logical
  34. default. We believe that if you're working with a simple compiler and target,
  35. you shouldn't need to configure very much... we try to make the tools guess as
  36. much as they can, but give the user the power to override it when it's wrong.
  37. ## Naming Things
  38. Let's talk about naming things. Programming is all about naming things. We name
  39. files, functions, variables, and so much more. While we're not always going to
  40. find the best name for something, we actually put quite a bit of effort into
  41. finding *What Something WANTS to be Called*™.
  42. When naming things, we more or less follow this hierarchy, the first being the
  43. most important to us (but we do all four whenever possible):
  44. 1. Readable
  45. 2. Descriptive
  46. 3. Consistent
  47. 4. Memorable
  48. #### Readable
  49. We want to read our code. This means we like names and flow that are more
  50. naturally read. We try to avoid double negatives. We try to avoid cryptic
  51. abbreviations (sticking to ones we feel are common).
  52. #### Descriptive
  53. We like descriptive names for things, especially functions and variables.
  54. Finding the right name for something is an important endeavor. You might notice
  55. from poking around our code that this often results in names that are a little
  56. longer than the average. Guilty. We're okay with a tiny bit more typing if it
  57. means our code is easier to understand.
  58. There are two exceptions to this rule that we also stick to as religiously as
  59. possible:
  60. First, while we realize hungarian notation (and similar systems for encoding
  61. type information into variable names) is providing a more descriptive name, we
  62. feel that (for the average developer) it takes away from readability and
  63. therefore is to be avoided.
  64. Second, loop counters and other local throw-away variables often have a purpose
  65. which is obvious. There's no need, therefore, to get carried away with complex
  66. naming. We find i, j, and k are better loop counters than loopCounterVar or
  67. whatnot. We only break this rule when we see that more description could improve
  68. understanding of an algorithm.
  69. #### Consistent
  70. We like consistency, but we're not really obsessed with it. We try to name our
  71. configuration macros in a consistent fashion... you'll notice a repeated use of
  72. UNITY_EXCLUDE_BLAH or UNITY_USES_BLAH macros. This helps users avoid having to
  73. remember each macro's details.
  74. #### Memorable
  75. Where ever it doesn't violate the above principles, we try to apply memorable
  76. names. Sometimes this means using something that is simply descriptive, but
  77. often we strive for descriptive AND unique... we like quirky names that stand
  78. out in our memory and are easier to search for. Take a look through the file
  79. names in Ceedling and you'll get a good idea of what we are talking about here.
  80. Why use preprocess when you can use preprocessinator? Or what better describes a
  81. module in charge of invoking tasks during releases than release_invoker? Don't
  82. get carried away. The names are still descriptive and fulfill the above
  83. requirements, but they don't feel stale.
  84. ## C and C++ Details
  85. We don't really want to add to the style battles out there. Tabs or spaces?
  86. How many spaces? Where do the braces go? These are age-old questions that will
  87. never be answered... or at least not answered in a way that will make everyone
  88. happy.
  89. We've decided on our own style preferences. If you'd like to contribute to these
  90. projects (and we hope that you do), then we ask if you do your best to follow
  91. the same. It will only hurt a little. We promise.
  92. #### Whitespace
  93. Our C-style is to use spaces and to use 4 of them per indent level. It's a nice
  94. power-of-2 number that looks decent on a wide screen. We have no more reason
  95. than that. We break that rule when we have lines that wrap (macros or function
  96. arguments or whatnot). When that happens, we like to indent further to line
  97. things up in nice tidy columns.
  98. ```C
  99. if (stuff_happened)
  100. {
  101. do_something();
  102. }
  103. ```
  104. #### Case
  105. - Files - all lower case with underscores.
  106. - Variables - all lower case with underscores
  107. - Macros - all caps with underscores.
  108. - Typedefs - all caps with underscores. (also ends with _T).
  109. - Functions - camel cased. Usually named ModuleName_FuncName
  110. - Constants and Globals - camel cased.
  111. #### Braces
  112. The left brace is on the next line after the declaration. The right brace is
  113. directly below that. Everything in between in indented one level. If you're
  114. catching an error and you have a one-line, go ahead and to it on the same line.
  115. ```C
  116. while (blah)
  117. {
  118. //Like so. Even if only one line, we use braces.
  119. }
  120. ```
  121. #### Comments
  122. Do you know what we hate? Old-school C block comments. BUT, we're using them
  123. anyway. As we mentioned, our goal is to support every compiler we can,
  124. especially embedded compilers. There are STILL C compilers out there that only
  125. support old-school block comments. So that is what we're using. We apologize. We
  126. think they are ugly too.
  127. ## Ruby Details
  128. Is there really such thing as a Ruby coding standard? Ruby is such a free form
  129. language, it seems almost sacrilegious to suggest that people should comply to
  130. one method! We'll keep it really brief!
  131. #### Whitespace
  132. Our Ruby style is to use spaces and to use 2 of them per indent level. It's a
  133. nice power-of-2 number that really grooves with Ruby's compact style. We have no
  134. more reason than that. We break that rule when we have lines that wrap. When
  135. that happens, we like to indent further to line things up in nice tidy columns.
  136. #### Case
  137. - Files - all lower case with underscores.
  138. - Variables - all lower case with underscores
  139. - Classes, Modules, etc - Camel cased.
  140. - Functions - all lower case with underscores
  141. - Constants - all upper case with underscores
  142. ## Documentation
  143. Egad. Really? We use markdown and we like pdf files because they can be made to
  144. look nice while still being portable. Good enough?
  145. *Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)*