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 tried 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 a bit of effort into 55finding *What Something WANTS to be Called*™. 56 57When naming things, we follow this hierarchy, the first being the 58most important to us (but we do all four when 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 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 is to be avoided. 86 87Second, loop counters and other local throw-away variables often have a purpose 88which is obvious. There's no need, therefore, to get carried away with complex 89naming. We find i, j, and k are better loop counters than loopCounterVar or 90whatnot. We only break this rule when we see that more description could improve 91understanding of an algorithm. 92 93 94#### Consistent 95 96We like consistency, but we're not really obsessed with it. We try to name our 97configuration macros in a consistent fashion... you'll notice a repeated use of 98UNITY_EXCLUDE_BLAH or UNITY_USES_BLAH macros. This helps users avoid having to 99remember each macro's details. 100 101 102#### Memorable 103 104Where ever it doesn't violate the above principles, we try to apply memorable 105names. Sometimes this means using something that is simply descriptive, but 106often we strive for descriptive AND unique... we like quirky names that stand 107out in our memory and are easier to search for. Take a look through the file 108names in Ceedling and you'll get a good idea of what we are talking about here. 109Why use preprocess when you can use preprocessinator? Or what better describes a 110module in charge of invoking tasks during releases than release_invoker? Don't 111get carried away. The names are still descriptive and fulfill the above 112requirements, but they don't feel stale. 113 114 115## C and C++ Details 116 117We don't really want to add to the style battles out there. Tabs or spaces? 118How many spaces? Where do the braces go? These are age-old questions that will 119never be answered... or at least not answered in a way that will make everyone 120happy. 121 122We've decided on our own style preferences. If you'd like to contribute to these 123projects (and we hope that you do), then we ask if you do your best to follow 124the same. It will only hurt a little. We promise. 125 126 127#### Whitespace 128 129Our C-style is to use spaces and to use 4 of them per indent level. It's a nice 130power-of-2 number that looks decent on a wide-screen. We have no more reason 131than that. We break that rule when we have lines that wrap (macros or function 132arguments or whatnot). When that happens, we like to indent further to line 133things up in nice tidy columns. 134 135```C 136 if (stuff_happened) 137 { 138 do_something(); 139 } 140``` 141 142 143#### Case 144 145- Files - all lower case with underscores. 146- Variables - all lower case with underscores 147- Macros - all caps with underscores. 148- Typedefs - all caps with underscores. (also ends with _T). 149- Functions - camel cased. Usually named ModuleName_FuncName 150- Constants and Globals - camel cased. 151 152 153#### Braces 154 155The left brace is on the next line after the declaration. The right brace is 156directly below that. Everything in between in indented one level. If you're 157catching an error and you have a one-line, go ahead and to it on the same line. 158 159```C 160 while (blah) 161 { 162 //Like so. Even if only one line, we use braces. 163 } 164``` 165 166 167#### Comments 168 169Do you know what we hate? Old-school C block comments. BUT, we're using them 170anyway. As we mentioned, our goal is to support every compiler we can, 171especially embedded compilers. There are STILL C compilers out there that only 172support old-school block comments. So that is what we're using. We apologize. We 173think they are ugly too. 174 175 176## Ruby Details 177 178Is there really such thing as a Ruby coding standard? Ruby is such a free form 179language, it seems almost sacrilegious to suggest that people should comply to 180one method! We'll keep it really brief! 181 182 183#### Whitespace 184 185Our Ruby style is to use spaces and to use 2 of them per indent level. It's a 186nice power-of-2 number that really grooves with Ruby's compact style. We have no 187more reason than that. We break that rule when we have lines that wrap. When 188that happens, we like to indent further to line things up in nice tidy columns. 189 190 191#### Case 192 193- Files - all lower case with underscores. 194- Variables - all lower case with underscores 195- Classes, Modules, etc - Camel cased. 196- Functions - all lower case with underscores 197- Constants - all upper case with underscores 198 199 200## Documentation 201 202Egad. Really? We use mark down and we like pdf files because they can be made to 203look nice while still being portable. Good enough? 204 205 206*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)* 207