Debian Codegraph @

vendor/unity/docs/ThrowTheSwitchCodingStandard.md

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