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