135375f98Sopenharmony_ci# ThrowTheSwitch.org Coding Standard 235375f98Sopenharmony_ci 335375f98Sopenharmony_ciHi. 435375f98Sopenharmony_ciWelcome to the coding standard for ThrowTheSwitch.org. 535375f98Sopenharmony_ciFor the most part, we try to follow these standards to unify our contributors' code into a cohesive unit (puns intended). 635375f98Sopenharmony_ciYou might find places where these standards aren't followed. 735375f98Sopenharmony_ciWe're not perfect. Please be polite where you notice these discrepancies and we'll try to be polite when we notice yours. 835375f98Sopenharmony_ci 935375f98Sopenharmony_ci;) 1035375f98Sopenharmony_ci 1135375f98Sopenharmony_ci## Why Have A Coding Standard? 1235375f98Sopenharmony_ci 1335375f98Sopenharmony_ciBeing consistent makes code easier to understand. 1435375f98Sopenharmony_ciWe've tried to keep our standard simple because we also believe that we can only expect someone to follow something that is understandable. 1535375f98Sopenharmony_ciPlease do your best. 1635375f98Sopenharmony_ci 1735375f98Sopenharmony_ci## Our Philosophy 1835375f98Sopenharmony_ci 1935375f98Sopenharmony_ciBefore we get into details on syntax, let's take a moment to talk about our vision for these tools. 2035375f98Sopenharmony_ciWe're C developers and embedded software developers. 2135375f98Sopenharmony_ciThese tools are great to test any C code, but catering to embedded software made us more tolerant of compiler quirks. 2235375f98Sopenharmony_ciThere are a LOT of quirky compilers out there. 2335375f98Sopenharmony_ciBy quirky I mean "doesn't follow standards because they feel like they have a license to do as they wish." 2435375f98Sopenharmony_ci 2535375f98Sopenharmony_ciOur philosophy is "support every compiler we can". 2635375f98Sopenharmony_ciMost 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). 2735375f98Sopenharmony_ciBut it also means these tools are tolerant of things that aren't common. 2835375f98Sopenharmony_ciSome that aren't even compliant. 2935375f98Sopenharmony_ciThere are configuration options to override the size of standard types. 3035375f98Sopenharmony_ciThere are configuration options to force Unity to not use certain standard library functions. 3135375f98Sopenharmony_ciA lot of Unity is configurable and we have worked hard to make it not TOO ugly in the process. 3235375f98Sopenharmony_ci 3335375f98Sopenharmony_ciSimilarly, our tools that parse C do their best. 3435375f98Sopenharmony_ciThey 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. 3535375f98Sopenharmony_ciIt's just what we do, because we like everything to Just Work™. 3635375f98Sopenharmony_ci 3735375f98Sopenharmony_ciSpeaking of having things Just Work™, that's our second philosophy. 3835375f98Sopenharmony_ciBy that, we mean that we do our best to have EVERY configuration option have a logical default. 3935375f98Sopenharmony_ciWe 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. 4035375f98Sopenharmony_ci 4135375f98Sopenharmony_ci## Naming Things 4235375f98Sopenharmony_ci 4335375f98Sopenharmony_ciLet's talk about naming things. 4435375f98Sopenharmony_ciProgramming is all about naming things. 4535375f98Sopenharmony_ciWe name files, functions, variables, and so much more. 4635375f98Sopenharmony_ciWhile 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*™. 4735375f98Sopenharmony_ci 4835375f98Sopenharmony_ciWhen naming things, we follow this hierarchy, the first being the most important to us (but we do all four when possible): 4935375f98Sopenharmony_ci 5035375f98Sopenharmony_ci1. Readable 5135375f98Sopenharmony_ci2. Descriptive 5235375f98Sopenharmony_ci3. Consistent 5335375f98Sopenharmony_ci4. Memorable 5435375f98Sopenharmony_ci 5535375f98Sopenharmony_ci### Readable 5635375f98Sopenharmony_ci 5735375f98Sopenharmony_ciWe want to read our code. 5835375f98Sopenharmony_ciThis means we like names and flow that are more naturally read. 5935375f98Sopenharmony_ciWe try to avoid double negatives. 6035375f98Sopenharmony_ciWe try to avoid cryptic abbreviations (sticking to ones we feel are common). 6135375f98Sopenharmony_ci 6235375f98Sopenharmony_ci### Descriptive 6335375f98Sopenharmony_ci 6435375f98Sopenharmony_ciWe like descriptive names for things, especially functions and variables. 6535375f98Sopenharmony_ciFinding the right name for something is an important endeavour. 6635375f98Sopenharmony_ciYou might notice from poking around our code that this often results in names that are a little longer than the average. 6735375f98Sopenharmony_ciGuilty. 6835375f98Sopenharmony_ciWe're okay with a bit more typing if it means our code is easier to understand. 6935375f98Sopenharmony_ci 7035375f98Sopenharmony_ciThere are two exceptions to this rule that we also stick to as religiously as possible: 7135375f98Sopenharmony_ci 7235375f98Sopenharmony_ciFirst, 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. 7335375f98Sopenharmony_ci 7435375f98Sopenharmony_ciSecond, loop counters and other local throw-away variables often have a purpose which is obvious. 7535375f98Sopenharmony_ciThere's no need, therefore, to get carried away with complex naming. 7635375f98Sopenharmony_ciWe find i, j, and k are better loop counters than loopCounterVar or whatnot. 7735375f98Sopenharmony_ciWe only break this rule when we see that more description could improve understanding of an algorithm. 7835375f98Sopenharmony_ci 7935375f98Sopenharmony_ci### Consistent 8035375f98Sopenharmony_ci 8135375f98Sopenharmony_ciWe like consistency, but we're not really obsessed with it. 8235375f98Sopenharmony_ciWe 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. 8335375f98Sopenharmony_ciThis helps users avoid having to remember each macro's details. 8435375f98Sopenharmony_ci 8535375f98Sopenharmony_ci### Memorable 8635375f98Sopenharmony_ci 8735375f98Sopenharmony_ciWhere ever it doesn't violate the above principles, we try to apply memorable names. 8835375f98Sopenharmony_ciSometimes 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. 8935375f98Sopenharmony_ciTake a look through the file names in Ceedling and you'll get a good idea of what we are talking about here. 9035375f98Sopenharmony_ciWhy use preprocess when you can use preprocessinator? 9135375f98Sopenharmony_ciOr what better describes a module in charge of invoking tasks during releases than release_invoker? 9235375f98Sopenharmony_ciDon't get carried away. 9335375f98Sopenharmony_ciThe names are still descriptive and fulfil the above requirements, but they don't feel stale. 9435375f98Sopenharmony_ci 9535375f98Sopenharmony_ci## C and C++ Details 9635375f98Sopenharmony_ci 9735375f98Sopenharmony_ciWe don't really want to add to the style battles out there. 9835375f98Sopenharmony_ciTabs or spaces? 9935375f98Sopenharmony_ciHow many spaces? 10035375f98Sopenharmony_ciWhere do the braces go? 10135375f98Sopenharmony_ciThese are age-old questions that will never be answered... or at least not answered in a way that will make everyone happy. 10235375f98Sopenharmony_ci 10335375f98Sopenharmony_ciWe've decided on our own style preferences. 10435375f98Sopenharmony_ciIf 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. 10535375f98Sopenharmony_ciIt will only hurt a little. We promise. 10635375f98Sopenharmony_ci 10735375f98Sopenharmony_ci### Whitespace in C/C++ 10835375f98Sopenharmony_ci 10935375f98Sopenharmony_ciOur C-style is to use spaces and to use 4 of them per indent level. 11035375f98Sopenharmony_ciIt's a nice power-of-2 number that looks decent on a wide-screen. 11135375f98Sopenharmony_ciWe have no more reason than that. 11235375f98Sopenharmony_ciWe break that rule when we have lines that wrap (macros or function arguments or whatnot). 11335375f98Sopenharmony_ciWhen that happens, we like to indent further to line things up in nice tidy columns. 11435375f98Sopenharmony_ci 11535375f98Sopenharmony_ci```C 11635375f98Sopenharmony_ci if (stuff_happened) 11735375f98Sopenharmony_ci { 11835375f98Sopenharmony_ci do_something(); 11935375f98Sopenharmony_ci } 12035375f98Sopenharmony_ci``` 12135375f98Sopenharmony_ci 12235375f98Sopenharmony_ci### Case in C/C++ 12335375f98Sopenharmony_ci 12435375f98Sopenharmony_ci- Files - all lower case with underscores. 12535375f98Sopenharmony_ci- Variables - all lower case with underscores 12635375f98Sopenharmony_ci- Macros - all caps with underscores. 12735375f98Sopenharmony_ci- Typedefs - all caps with underscores. (also ends with _T). 12835375f98Sopenharmony_ci- Functions - camel cased. Usually named ModuleName_FuncName 12935375f98Sopenharmony_ci- Constants and Globals - camel cased. 13035375f98Sopenharmony_ci 13135375f98Sopenharmony_ci### Braces in C/C++ 13235375f98Sopenharmony_ci 13335375f98Sopenharmony_ciThe left brace is on the next line after the declaration. 13435375f98Sopenharmony_ciThe right brace is directly below that. 13535375f98Sopenharmony_ciEverything in between in indented one level. 13635375f98Sopenharmony_ciIf you're catching an error and you have a one-line, go ahead and to it on the same line. 13735375f98Sopenharmony_ci 13835375f98Sopenharmony_ci```C 13935375f98Sopenharmony_ci while (blah) 14035375f98Sopenharmony_ci { 14135375f98Sopenharmony_ci //Like so. Even if only one line, we use braces. 14235375f98Sopenharmony_ci } 14335375f98Sopenharmony_ci``` 14435375f98Sopenharmony_ci 14535375f98Sopenharmony_ci### Comments in C/C++ 14635375f98Sopenharmony_ci 14735375f98Sopenharmony_ciDo you know what we hate? 14835375f98Sopenharmony_ciOld-school C block comments. 14935375f98Sopenharmony_ciBUT, we're using them anyway. 15035375f98Sopenharmony_ciAs we mentioned, our goal is to support every compiler we can, especially embedded compilers. 15135375f98Sopenharmony_ciThere are STILL C compilers out there that only support old-school block comments. 15235375f98Sopenharmony_ciSo that is what we're using. 15335375f98Sopenharmony_ciWe apologize. 15435375f98Sopenharmony_ciWe think they are ugly too. 15535375f98Sopenharmony_ci 15635375f98Sopenharmony_ci## Ruby Details 15735375f98Sopenharmony_ci 15835375f98Sopenharmony_ciIs there really such thing as a Ruby coding standard? 15935375f98Sopenharmony_ciRuby is such a free form language, it seems almost sacrilegious to suggest that people should comply to one method! 16035375f98Sopenharmony_ciWe'll keep it really brief! 16135375f98Sopenharmony_ci 16235375f98Sopenharmony_ci### Whitespace in Ruby 16335375f98Sopenharmony_ci 16435375f98Sopenharmony_ciOur Ruby style is to use spaces and to use 2 of them per indent level. 16535375f98Sopenharmony_ciIt's a nice power-of-2 number that really grooves with Ruby's compact style. 16635375f98Sopenharmony_ciWe have no more reason than that. 16735375f98Sopenharmony_ciWe break that rule when we have lines that wrap. 16835375f98Sopenharmony_ciWhen that happens, we like to indent further to line things up in nice tidy columns. 16935375f98Sopenharmony_ci 17035375f98Sopenharmony_ci### Case in Ruby 17135375f98Sopenharmony_ci 17235375f98Sopenharmony_ci- Files - all lower case with underscores. 17335375f98Sopenharmony_ci- Variables - all lower case with underscores 17435375f98Sopenharmony_ci- Classes, Modules, etc - Camel cased. 17535375f98Sopenharmony_ci- Functions - all lower case with underscores 17635375f98Sopenharmony_ci- Constants - all upper case with underscores 17735375f98Sopenharmony_ci 17835375f98Sopenharmony_ci## Documentation 17935375f98Sopenharmony_ci 18035375f98Sopenharmony_ciEgad. 18135375f98Sopenharmony_ciReally? 18235375f98Sopenharmony_ciWe use markdown and we like PDF files because they can be made to look nice while still being portable. 18335375f98Sopenharmony_ciGood enough? 18435375f98Sopenharmony_ci 18535375f98Sopenharmony_ci*Find The Latest of This And More at [ThrowTheSwitch.org][]* 18635375f98Sopenharmony_ci 18735375f98Sopenharmony_ci[ThrowTheSwitch.org]: https://throwtheswitch.org