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