ThrowTheSwitchCodingStandard.md 7.8 KB
Newer Older
T
toby 已提交
1
# ThrowTheSwitch.org Coding Standard
2 3 4 5

Hi. Welcome to the coding standard for ThrowTheSwitch.org. For the most part,
we try to follow these standards to unify our contributors' code into a cohesive
unit (puns intended). You might find places where these standards aren't
T
toby 已提交
6
followed. We're not perfect. Please be polite where
7
you notice these discrepancies and we'll try to be polite when we notice yours.
T
toby 已提交
8 9
;)

10

T
toby 已提交
11
## Why Have A Coding Standard?
12 13 14

Being consistent makes code easier to understand. We've made an attempt to keep
our standard simple because we also believe that we can only expect someone to
T
toby 已提交
15 16
follow something that is understandable. Please do your best.

17

T
toby 已提交
18
## Our Philosophy
19 20 21 22 23 24

Before we get into details on syntax, let's take a moment to talk about our
vision for these tools. We're C developers and embedded software developers.
These tools are great to test any C code, but catering to embedded software has
made us more tolerant of compiler quirks. There are a LOT of quirky compilers
out there. By quirky I mean "doesn't follow standards because they feel like
T
toby 已提交
25 26
they have a license to do as they wish."

27 28 29 30 31 32 33
Our philosophy is "support every compiler we can". 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). But it also means these
tools are tolerant of things that aren't common. Some that aren't even
compliant. There are configuration options to override the size of standard
types. There are configuration options to force Unity to not use certain
standard library functions. A lot of Unity is configurable and we have worked
T
toby 已提交
34 35
hard to make it not TOO ugly in the process.

36 37 38 39
Similarly, our tools that parse C do their best. 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. It's just what we do, because we like
T
toby 已提交
40 41
everything to Just Work™.

42 43 44 45
Speaking of having things Just Work™, that's our second philosophy. By that, we
mean that we do our best to have EVERY configuration option have a logical
default. 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
T
toby 已提交
46 47
much as they can, but give the user the power to override it when it's wrong.

48

T
toby 已提交
49
## Naming Things
50 51 52 53

Let's talk about naming things. Programming is all about naming things. We name
files, functions, variables, and so much more. While we're not always going to
find the best name for something, we actually put quite a bit of effort into
T
toby 已提交
54 55
finding *What Something WANTS to be Called*™.

56
When naming things, we more or less follow this hierarchy, the first being the
T
toby 已提交
57 58 59 60 61 62
most important to us (but we do all four whenever possible):
1. Readable
2. Descriptive
3. Consistent
4. Memorable

63

T
toby 已提交
64
#### Readable
65 66 67

We want to read our code. This means we like names and flow that are more
naturally read. We try to avoid double negatives. We try to avoid cryptic
T
toby 已提交
68 69
abbreviations (sticking to ones we feel are common).

70

T
toby 已提交
71
#### Descriptive
72 73 74 75 76

We like descriptive names for things, especially functions and variables.
Finding the right name for something is an important endeavor. You might notice
from poking around our code that this often results in names that are a little
longer than the average. Guilty. We're okay with a tiny bit more typing if it
T
toby 已提交
77 78
means our code is easier to understand.

79
There are two exceptions to this rule that we also stick to as religiously as
T
toby 已提交
80 81
possible:

82 83 84
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
T
toby 已提交
85 86
therefore is to be avoided.

87 88 89 90
Second, loop counters and other local throw-away variables often have a purpose
which is obvious. There's no need, therefore, to get carried away with complex
naming. We find i, j, and k are better loop counters than loopCounterVar or
whatnot. We only break this rule when we see that more description could improve
T
toby 已提交
91 92
understanding of an algorithm.

93

T
toby 已提交
94
#### Consistent
95 96 97 98 99 100

We like consistency, but we're not really obsessed with it. 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. This helps users avoid having to
remember each macro's details.

T
toby 已提交
101 102

#### Memorable
103 104 105 106 107 108 109 110 111

Where ever it doesn't violate the above principles, we try to apply memorable
names. 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. Take a look through the file
names in Ceedling and you'll get a good idea of what we are talking about here.
Why use preprocess when you can use preprocessinator? Or what better describes a
module in charge of invoking tasks during releases than release_invoker? Don't
get carried away. The names are still descriptive and fulfill the above
T
toby 已提交
112 113
requirements, but they don't feel stale.

114

T
toby 已提交
115
## C and C++ Details
116 117 118 119

We don't really want to add to the style battles out there. Tabs or spaces?
How many spaces? Where do the braces go? These are age-old questions that will
never be answered... or at least not answered in a way that will make everyone
T
toby 已提交
120 121
happy.

122 123
We've decided on our own style preferences. 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
T
toby 已提交
124 125
the same. It will only hurt a little. We promise.

126

T
toby 已提交
127
#### Whitespace
128 129 130 131 132

Our C-style is to use spaces and to use 4 of them per indent level. It's a nice
power-of-2 number that looks decent on a wide screen. We have no more reason
than that. We break that rule when we have lines that wrap (macros or function
arguments or whatnot). When that happens, we like to indent further to line
T
toby 已提交
133 134 135 136 137 138 139 140 141
things up in nice tidy columns.

```C
    if (stuff_happened)
    {
        do_something();
    }
```

142

T
toby 已提交
143
#### Case
144

T
toby 已提交
145 146 147 148 149 150 151
- Files - all lower case with underscores.
- Variables - all lower case with underscores
- Macros - all caps with underscores.
- Typedefs - all caps with underscores. (also ends with _T).
- Functions - camel cased. Usually named ModuleName_FuncName
- Constants and Globals - camel cased.

152

T
toby 已提交
153
#### Braces
154 155 156

The left brace is on the next line after the declaration. The right brace is
directly below that. Everything in between in indented one level. If you're
T
toby 已提交
157 158 159 160 161 162 163 164
catching an error and you have a one-line, go ahead and to it on the same line.

```C
    while (blah)
    {
        //Like so. Even if only one line, we use braces.
    }
```
165 166


T
toby 已提交
167
#### Comments
168 169 170 171 172

Do you know what we hate? Old-school C block comments. BUT, we're using them
anyway. As we mentioned, our goal is to support every compiler we can,
especially embedded compilers. There are STILL C compilers out there that only
support old-school block comments. So that is what we're using. We apologize. We
T
toby 已提交
173 174
think they are ugly too.

175

T
toby 已提交
176
## Ruby Details
177 178 179

Is there really such thing as a Ruby coding standard? Ruby is such a free form
language, it seems almost sacrilegious to suggest that people should comply to
T
toby 已提交
180 181
one method! We'll keep it really brief!

182

T
toby 已提交
183
#### Whitespace
184 185 186 187

Our Ruby style is to use spaces and to use 2 of them per indent level. It's a
nice power-of-2 number that really grooves with Ruby's compact style. We have no
more reason than that. We break that rule when we have lines that wrap. When
T
toby 已提交
188 189
that happens, we like to indent further to line things up in nice tidy columns.

190

T
toby 已提交
191
#### Case
192

T
toby 已提交
193 194 195 196 197 198
- Files - all lower case with underscores.
- Variables - all lower case with underscores
- Classes, Modules, etc - Camel cased.
- Functions - all lower case with underscores
- Constants - all upper case with underscores

199

T
toby 已提交
200
## Documentation
201 202

Egad. Really? We use markdown and we like pdf files because they can be made to
T
toby 已提交
203
look nice while still being portable. Good enough?