19750e409Sopenharmony_ci# ThrowTheSwitch.org Coding Standard 29750e409Sopenharmony_ci 39750e409Sopenharmony_ciHi. Welcome to the coding standard for ThrowTheSwitch.org. For the most part, 49750e409Sopenharmony_ciwe try to follow these standards to unify our contributors' code into a cohesive 59750e409Sopenharmony_ciunit (puns intended). You might find places where these standards aren't 69750e409Sopenharmony_cifollowed. We're not perfect. Please be polite where you notice these discrepancies 79750e409Sopenharmony_ciand we'll try to be polite when we notice yours. 89750e409Sopenharmony_ci 99750e409Sopenharmony_ci;) 109750e409Sopenharmony_ci 119750e409Sopenharmony_ci 129750e409Sopenharmony_ci## Why Have A Coding Standard? 139750e409Sopenharmony_ci 149750e409Sopenharmony_ciBeing consistent makes code easier to understand. We've made an attempt to keep 159750e409Sopenharmony_ciour standard simple because we also believe that we can only expect someone to 169750e409Sopenharmony_cifollow something that is understandable. Please do your best. 179750e409Sopenharmony_ci 189750e409Sopenharmony_ci 199750e409Sopenharmony_ci## Our Philosophy 209750e409Sopenharmony_ci 219750e409Sopenharmony_ciBefore we get into details on syntax, let's take a moment to talk about our 229750e409Sopenharmony_civision for these tools. We're C developers and embedded software developers. 239750e409Sopenharmony_ciThese tools are great to test any C code, but catering to embedded software has 249750e409Sopenharmony_cimade us more tolerant of compiler quirks. There are a LOT of quirky compilers 259750e409Sopenharmony_ciout there. By quirky I mean "doesn't follow standards because they feel like 269750e409Sopenharmony_cithey have a license to do as they wish." 279750e409Sopenharmony_ci 289750e409Sopenharmony_ciOur philosophy is "support every compiler we can". Most often, this means that 299750e409Sopenharmony_ciwe aim for writing C code that is standards compliant (often C89... that seems 309750e409Sopenharmony_cito be a sweet spot that is almost always compatible). But it also means these 319750e409Sopenharmony_citools are tolerant of things that aren't common. Some that aren't even 329750e409Sopenharmony_cicompliant. There are configuration options to override the size of standard 339750e409Sopenharmony_citypes. There are configuration options to force Unity to not use certain 349750e409Sopenharmony_cistandard library functions. A lot of Unity is configurable and we have worked 359750e409Sopenharmony_cihard to make it not TOO ugly in the process. 369750e409Sopenharmony_ci 379750e409Sopenharmony_ciSimilarly, our tools that parse C do their best. They aren't full C parsers 389750e409Sopenharmony_ci(yet) and, even if they were, they would still have to accept non-standard 399750e409Sopenharmony_ciadditions like gcc extensions or specifying `@0x1000` to force a variable to 409750e409Sopenharmony_cicompile to a particular location. It's just what we do, because we like 419750e409Sopenharmony_cieverything to Just Work™. 429750e409Sopenharmony_ci 439750e409Sopenharmony_ciSpeaking of having things Just Work™, that's our second philosophy. By that, we 449750e409Sopenharmony_cimean that we do our best to have EVERY configuration option have a logical 459750e409Sopenharmony_cidefault. We believe that if you're working with a simple compiler and target, 469750e409Sopenharmony_ciyou shouldn't need to configure very much... we try to make the tools guess as 479750e409Sopenharmony_cimuch as they can, but give the user the power to override it when it's wrong. 489750e409Sopenharmony_ci 499750e409Sopenharmony_ci 509750e409Sopenharmony_ci## Naming Things 519750e409Sopenharmony_ci 529750e409Sopenharmony_ciLet's talk about naming things. Programming is all about naming things. We name 539750e409Sopenharmony_cifiles, functions, variables, and so much more. While we're not always going to 549750e409Sopenharmony_cifind the best name for something, we actually put quite a bit of effort into 559750e409Sopenharmony_cifinding *What Something WANTS to be Called*™. 569750e409Sopenharmony_ci 579750e409Sopenharmony_ciWhen naming things, we more or less follow this hierarchy, the first being the 589750e409Sopenharmony_cimost important to us (but we do all four whenever possible): 599750e409Sopenharmony_ci1. Readable 609750e409Sopenharmony_ci2. Descriptive 619750e409Sopenharmony_ci3. Consistent 629750e409Sopenharmony_ci4. Memorable 639750e409Sopenharmony_ci 649750e409Sopenharmony_ci 659750e409Sopenharmony_ci#### Readable 669750e409Sopenharmony_ci 679750e409Sopenharmony_ciWe want to read our code. This means we like names and flow that are more 689750e409Sopenharmony_cinaturally read. We try to avoid double negatives. We try to avoid cryptic 699750e409Sopenharmony_ciabbreviations (sticking to ones we feel are common). 709750e409Sopenharmony_ci 719750e409Sopenharmony_ci 729750e409Sopenharmony_ci#### Descriptive 739750e409Sopenharmony_ci 749750e409Sopenharmony_ciWe like descriptive names for things, especially functions and variables. 759750e409Sopenharmony_ciFinding the right name for something is an important endeavor. You might notice 769750e409Sopenharmony_cifrom poking around our code that this often results in names that are a little 779750e409Sopenharmony_cilonger than the average. Guilty. We're okay with a tiny bit more typing if it 789750e409Sopenharmony_cimeans our code is easier to understand. 799750e409Sopenharmony_ci 809750e409Sopenharmony_ciThere are two exceptions to this rule that we also stick to as religiously as 819750e409Sopenharmony_cipossible: 829750e409Sopenharmony_ci 839750e409Sopenharmony_ciFirst, while we realize hungarian notation (and similar systems for encoding 849750e409Sopenharmony_citype information into variable names) is providing a more descriptive name, we 859750e409Sopenharmony_cifeel that (for the average developer) it takes away from readability and 869750e409Sopenharmony_citherefore is to be avoided. 879750e409Sopenharmony_ci 889750e409Sopenharmony_ciSecond, loop counters and other local throw-away variables often have a purpose 899750e409Sopenharmony_ciwhich is obvious. There's no need, therefore, to get carried away with complex 909750e409Sopenharmony_cinaming. We find i, j, and k are better loop counters than loopCounterVar or 919750e409Sopenharmony_ciwhatnot. We only break this rule when we see that more description could improve 929750e409Sopenharmony_ciunderstanding of an algorithm. 939750e409Sopenharmony_ci 949750e409Sopenharmony_ci 959750e409Sopenharmony_ci#### Consistent 969750e409Sopenharmony_ci 979750e409Sopenharmony_ciWe like consistency, but we're not really obsessed with it. We try to name our 989750e409Sopenharmony_ciconfiguration macros in a consistent fashion... you'll notice a repeated use of 999750e409Sopenharmony_ciUNITY_EXCLUDE_BLAH or UNITY_USES_BLAH macros. This helps users avoid having to 1009750e409Sopenharmony_ciremember each macro's details. 1019750e409Sopenharmony_ci 1029750e409Sopenharmony_ci 1039750e409Sopenharmony_ci#### Memorable 1049750e409Sopenharmony_ci 1059750e409Sopenharmony_ciWhere ever it doesn't violate the above principles, we try to apply memorable 1069750e409Sopenharmony_cinames. Sometimes this means using something that is simply descriptive, but 1079750e409Sopenharmony_cioften we strive for descriptive AND unique... we like quirky names that stand 1089750e409Sopenharmony_ciout in our memory and are easier to search for. Take a look through the file 1099750e409Sopenharmony_cinames in Ceedling and you'll get a good idea of what we are talking about here. 1109750e409Sopenharmony_ciWhy use preprocess when you can use preprocessinator? Or what better describes a 1119750e409Sopenharmony_cimodule in charge of invoking tasks during releases than release_invoker? Don't 1129750e409Sopenharmony_ciget carried away. The names are still descriptive and fulfill the above 1139750e409Sopenharmony_cirequirements, but they don't feel stale. 1149750e409Sopenharmony_ci 1159750e409Sopenharmony_ci 1169750e409Sopenharmony_ci## C and C++ Details 1179750e409Sopenharmony_ci 1189750e409Sopenharmony_ciWe don't really want to add to the style battles out there. Tabs or spaces? 1199750e409Sopenharmony_ciHow many spaces? Where do the braces go? These are age-old questions that will 1209750e409Sopenharmony_cinever be answered... or at least not answered in a way that will make everyone 1219750e409Sopenharmony_cihappy. 1229750e409Sopenharmony_ci 1239750e409Sopenharmony_ciWe've decided on our own style preferences. If you'd like to contribute to these 1249750e409Sopenharmony_ciprojects (and we hope that you do), then we ask if you do your best to follow 1259750e409Sopenharmony_cithe same. It will only hurt a little. We promise. 1269750e409Sopenharmony_ci 1279750e409Sopenharmony_ci 1289750e409Sopenharmony_ci#### Whitespace 1299750e409Sopenharmony_ci 1309750e409Sopenharmony_ciOur C-style is to use spaces and to use 4 of them per indent level. It's a nice 1319750e409Sopenharmony_cipower-of-2 number that looks decent on a wide screen. We have no more reason 1329750e409Sopenharmony_cithan that. We break that rule when we have lines that wrap (macros or function 1339750e409Sopenharmony_ciarguments or whatnot). When that happens, we like to indent further to line 1349750e409Sopenharmony_cithings up in nice tidy columns. 1359750e409Sopenharmony_ci 1369750e409Sopenharmony_ci```C 1379750e409Sopenharmony_ci if (stuff_happened) 1389750e409Sopenharmony_ci { 1399750e409Sopenharmony_ci do_something(); 1409750e409Sopenharmony_ci } 1419750e409Sopenharmony_ci``` 1429750e409Sopenharmony_ci 1439750e409Sopenharmony_ci 1449750e409Sopenharmony_ci#### Case 1459750e409Sopenharmony_ci 1469750e409Sopenharmony_ci- Files - all lower case with underscores. 1479750e409Sopenharmony_ci- Variables - all lower case with underscores 1489750e409Sopenharmony_ci- Macros - all caps with underscores. 1499750e409Sopenharmony_ci- Typedefs - all caps with underscores. (also ends with _T). 1509750e409Sopenharmony_ci- Functions - camel cased. Usually named ModuleName_FuncName 1519750e409Sopenharmony_ci- Constants and Globals - camel cased. 1529750e409Sopenharmony_ci 1539750e409Sopenharmony_ci 1549750e409Sopenharmony_ci#### Braces 1559750e409Sopenharmony_ci 1569750e409Sopenharmony_ciThe left brace is on the next line after the declaration. The right brace is 1579750e409Sopenharmony_cidirectly below that. Everything in between in indented one level. If you're 1589750e409Sopenharmony_cicatching an error and you have a one-line, go ahead and to it on the same line. 1599750e409Sopenharmony_ci 1609750e409Sopenharmony_ci```C 1619750e409Sopenharmony_ci while (blah) 1629750e409Sopenharmony_ci { 1639750e409Sopenharmony_ci //Like so. Even if only one line, we use braces. 1649750e409Sopenharmony_ci } 1659750e409Sopenharmony_ci``` 1669750e409Sopenharmony_ci 1679750e409Sopenharmony_ci 1689750e409Sopenharmony_ci#### Comments 1699750e409Sopenharmony_ci 1709750e409Sopenharmony_ciDo you know what we hate? Old-school C block comments. BUT, we're using them 1719750e409Sopenharmony_cianyway. As we mentioned, our goal is to support every compiler we can, 1729750e409Sopenharmony_ciespecially embedded compilers. There are STILL C compilers out there that only 1739750e409Sopenharmony_cisupport old-school block comments. So that is what we're using. We apologize. We 1749750e409Sopenharmony_cithink they are ugly too. 1759750e409Sopenharmony_ci 1769750e409Sopenharmony_ci 1779750e409Sopenharmony_ci## Ruby Details 1789750e409Sopenharmony_ci 1799750e409Sopenharmony_ciIs there really such thing as a Ruby coding standard? Ruby is such a free form 1809750e409Sopenharmony_cilanguage, it seems almost sacrilegious to suggest that people should comply to 1819750e409Sopenharmony_cione method! We'll keep it really brief! 1829750e409Sopenharmony_ci 1839750e409Sopenharmony_ci 1849750e409Sopenharmony_ci#### Whitespace 1859750e409Sopenharmony_ci 1869750e409Sopenharmony_ciOur Ruby style is to use spaces and to use 2 of them per indent level. It's a 1879750e409Sopenharmony_cinice power-of-2 number that really grooves with Ruby's compact style. We have no 1889750e409Sopenharmony_cimore reason than that. We break that rule when we have lines that wrap. When 1899750e409Sopenharmony_cithat happens, we like to indent further to line things up in nice tidy columns. 1909750e409Sopenharmony_ci 1919750e409Sopenharmony_ci 1929750e409Sopenharmony_ci#### Case 1939750e409Sopenharmony_ci 1949750e409Sopenharmony_ci- Files - all lower case with underscores. 1959750e409Sopenharmony_ci- Variables - all lower case with underscores 1969750e409Sopenharmony_ci- Classes, Modules, etc - Camel cased. 1979750e409Sopenharmony_ci- Functions - all lower case with underscores 1989750e409Sopenharmony_ci- Constants - all upper case with underscores 1999750e409Sopenharmony_ci 2009750e409Sopenharmony_ci 2019750e409Sopenharmony_ci## Documentation 2029750e409Sopenharmony_ci 2039750e409Sopenharmony_ciEgad. Really? We use markdown and we like pdf files because they can be made to 2049750e409Sopenharmony_cilook nice while still being portable. Good enough? 2059750e409Sopenharmony_ci 2069750e409Sopenharmony_ci 2079750e409Sopenharmony_ci*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)* 208