1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc title="{[project]}" subtitle="Coding Standards" toc="y">
<description>{[project]} Coding Standard.</description>
<!-- ======================================================================================================================= -->
<section id="uncrustify">
<title>Formatting with <proper>uncrustify</proper></title>
<p><backrest/> uses <proper>uncrustify</proper> to check/update the code formatting. If the <id>code-format</id> test fails in CI then reformat the code:</p>
<code-block>
pgbackrest/test/test.pl --code-format
</code-block>
<p>Also review the standards in the following sections below. Some standards require further explanation and others are not enforced by <proper>uncrustify</proper>.</p>
</section>
<!-- ======================================================================================================================= -->
<section id="standards">
<title>Standards</title>
<!-- =================================================================================================================== -->
<section id="indentation">
<title>Indentation</title>
<p>Indentation is four spaces -- no tabs. Only file types that absolutely require tabs (e.g. `Makefile`) may use them.</p>
</section>
<!-- =================================================================================================================== -->
<section id="line-length">
<title>Line Length</title>
<p>With the exception of documentation code, no line of any code or test file shall exceed 132 characters. If a line break is required, then it shall be after the first function parenthesis:</p>
<code-block>
// CORRECT - location of line break after first function parenthesis if line length is greater than 132
StringList *removeList = infoBackupDataLabelList(
infoBackup, strNewFmt("^%s.*", strZ(strLstGet(currentBackupList, fullIdx))));
// INCORRECT
StringList *removeList = infoBackupDataLabelList(infoBackup, strNewFmt("^%s.*", strZ(strLstGet(currentBackupList,
fullIdx))));
</code-block>
<p>If a conditional, then after a completed conditional, for example:</p>
<code-block>
// CORRECT - location of line break after a completed conditional if line length is greater than 132
if (archiveInfoPgHistory.id != backupInfoPgHistory.id ||
archiveInfoPgHistory.systemId != backupInfoPgHistory.systemId ||
archiveInfoPgHistory.version != backupInfoPgHistory.version)
// INCORRECT
if (archiveInfoPgHistory.id != backupInfoPgHistory.id || archiveInfoPgHistory.systemId !=
backupInfoPgHistory.systemId || archiveInfoPgHistory.version != backupInfoPgHistory.version)
</code-block>
</section>
<!-- =================================================================================================================== -->
<section id="function-comment">
<title>Function Comments</title>
<p>Comments for <code>extern</code> functions should be included in the <file>.h</file> file. Comments for <code>static</code> functions and implementation-specific notes for <code>extern</code> functions (i.e., not of interest to the general user) should be included in the <file>.c</file> file.</p>
</section>
<!-- =================================================================================================================== -->
<section id="inline-comment">
<title>Inline Comment</title>
<p>Inline comments shall start at character 69 and must not exceed the line length of 132. For example:</p>
<code-block>
typedef struct InlineCommentExample
{
const String *comment; // Inline comment example
const String *longComment; // Inline comment example that exceeds 132 characters should
// then go to next line but this should be avoided
} InlineCommentExample;
</code-block>
</section>
<!-- =================================================================================================================== -->
<section id="naming">
<title>Naming</title>
<!-- =============================================================================================================== -->
<section id="variables">
<title>Variables</title>
<p>Variable names use camel case with the first letter lower-case.</p>
<list>
<list-item><id>stanzaName</id> - the name of the stanza</list-item>
<list-item><id>nameIdx</id> - loop variable for iterating through a list of names</list-item>
</list>
<p>Variable names should be descriptive. Avoid <id>i</id>, <id>j</id>, etc.</p>
</section>
<!-- =============================================================================================================== -->
<section id="types">
<title>Types</title>
<p>Type names use camel case with the first letter upper case:</p>
<p><code>typedef struct MemContext <...></code></p>
<p><code>typedef enum {<...>} ErrorState;</code></p>
</section>
<!-- =============================================================================================================== -->
<section id="constants">
<title>Constants</title>
<p><b>#define Constants</b></p>
<p><code>#define</code> constants should be all caps with <id>_</id> separators.</p>
<code-block type="c">
#define MY_CONSTANT "STRING"
</code-block>
<p>The value should be aligned at column 69 whenever possible.</p>
<p>This type of constant should mostly be used for strings. Use enums whenever possible for integer constants.</p>
<p><b>String Constants</b></p>
<p>String constants can be declared using the <code>STRING_STATIC()</code> macro for local strings and <code>STRING_EXTERN()</code> for strings that will be externed for use in other modules.</p>
<p>Externed strings should be declared in the header file as:</p>
<code-block type="c">
#define SAMPLE_VALUE "STRING"
STRING_DECLARE(SAMPLE_VALUE_STR);
</code-block>
<p>And in the C file as:</p>
<code-block type="c">
STRING_EXTERN(SAMPLE_VALUE_STR, SAMPLE_VALUE);
</code-block>
<p>Static strings declared in the C file are not required to have a <code>#define</code> if the <code>#define</code> version is not used. Externed strings must always have the <code>#define</code> in the header file.</p>
<p><b>Enum Constants</b></p>
<p>Enum elements follow the same case rules as variables. They are strongly typed so this shouldn't present any confusion.</p>
<code-block type="c">
typedef enum
{
cipherModeEncrypt,
cipherModeDecrypt,
} CipherMode;
</code-block>
<p>Note the comma after the last element. This reduces diff churn when new elements are added.</p>
</section>
<!-- =============================================================================================================== -->
<section id="macros">
<title>Macros</title>
<p>Macro names should be upper-case with underscores between words. Macros (except simple constants) should be avoided whenever possible as they make code less clear and test coverage harder to measure.</p>
<p>Macros should follow the format:</p>
<code-block type="c">
#define MACRO(paramName1, paramName2) \
<code>
</code-block>
<p>If the macro defines a block it should look like:</p>
<code-block type="c">
#define MACRO_2(paramName1, paramName2) \
{ \
<code> \
}
</code-block>
<p>Continuation characters should be aligned at column 132 (unlike the examples above that have been shortened for display purposes).</p>
<p>To avoid conflicts, variables in a macro will be named <id>[macro name]_[var name]</id>, e.g. <id>TEST_RESULT_resultExpected</id>. Variables that need to be accessed in wrapped code should be provided accessor macros.</p>
<p><link section="/language-elements/variadic-functions">Variadic functions</link> are an exception to the capitalization rule.</p>
</section>
<!-- =============================================================================================================== -->
<section id="begin-end">
<title>Begin / End</title>
<p>Use <id>Begin</id> / <id>End</id> for names rather than <id>Start</id> / <id>Finish</id>, etc.</p>
</section>
<!-- =============================================================================================================== -->
<section id="new-free">
<title>New / Free</title>
<p>Use <id>New</id> / <id>Free</id> for constructors and destructors rather than <id>Create</id> / <id>Destroy</id>, etc.</p>
</section>
</section>
<!-- =================================================================================================================== -->
<section id="formatting">
<title>Formatting</title>
<!-- =============================================================================================================== -->
<section id="braces">
<title>Braces</title>
<p>C allows braces to be excluded for a single statement. However, braces should be used when the control statement (if, while, etc.) spans more than one line or the statement to be executed spans more than one line.</p>
<p>No braces needed:</p>
<code-block type="c">
if (condition)
return value;
</code-block>
<p>Braces needed:</p>
<code-block type="c">
if (conditionThatUsesEntireLine1 &&
conditionThatUsesEntireLine2)
{
return value;
}
</code-block>
<code-block type="c">
if (condition)
{
return
valueThatUsesEntireLine1 &&
valueThatUsesEntireLine2;
}
</code-block>
<p>Braces should be added to <code>switch</code> statement cases that have a significant amount of code. As a general rule of thumb, if the code block in the <code>case</code> is large enough to have blank lines and/or multiple comments then it should be enclosed in braces.</p>
<code-block type="c">
switch (int)
{
case 1:
a = 2;
break;
case 2:
{
# Comment this more complex code
a = 1;
b = 2;
c = func(a, b);
break;
}
}
</code-block>
</section>
<!-- =============================================================================================================== -->
<section id="hints-warnings-errors">
<title>Hints, Warnings, and Errors</title>
<p>Hints are to be formatted with capitalized <id>HINT:</id> followed by a space and a sentence. The sentence shall only begin with a capital letter if the first word is an acronym (e.g. TLS) or a proper name (e.g. PostgreSQL). The sentence must end with a period, question mark or exclamation point as appropriate.</p>
<p>Warning and errors shall be lowercase with the exceptions for proper names and acronyms and end without punctuation.</p>
</section>
</section>
</section>
<!-- ======================================================================================================================= -->
<section id="language-elements">
<title>Language Elements</title>
<!-- =================================================================================================================== -->
<section id="data-types">
<title>Data Types</title>
<p>Don't get exotic - use the simplest type that will work.</p>
<p>Use <id>int</id> or <id>unsigned int</id> for general cases. <id>int</id> will be at least 32 bits. When not using <id>int</id> use one of the types defined in <file>common/type.h</file>.</p>
</section>
<!-- =================================================================================================================== -->
<section id="macros">
<title>Macros</title>
<p>Don't use a macro when a function could be used instead. Macros make it hard to measure code coverage.</p>
</section>
<!-- =================================================================================================================== -->
<section id="objects">
<title>Objects</title>
<p>Object-oriented programming is used extensively. The object pointer is always referred to as <id>this</id>.</p>
<p>An object can expose internal struct members by defining a public struct that contains the members to be exposed and using inline functions to get/set the members.</p>
<p>The header file:</p>
<code-block type="c">
/***********************************************************************************************************************************
Getters/setters
***********************************************************************************************************************************/
typedef struct ListPub
{
unsigned int listSize; // List size
} ListPub;
// List size
FN_INLINE_ALWAYS unsigned int
lstSize(const List *const this)
{
return THIS_PUB(List)->listSize;
}
</code-block>
<p><code>THIS_PUB()</code> ensures that <code>this != NULL</code> so there is no need to check that in the calling function.</p>
<p>And the C file:</p>
<code-block type="c">
struct List
{
ListPub pub; // Publicly accessible variables
...
};
</code-block>
<p>The public struct must be the first member of the private struct. The naming convention for the public struct is to add <id>Pub</id> to the end of the private struct name.</p>
</section>
<!-- =================================================================================================================== -->
<section id="variadic-functions">
<title>Variadic Functions</title>
<p>Variadic functions can take a variable number of parameters. While the <code>printf()</code> pattern is variadic, it is not very flexible in terms of optional parameters given in any order.</p>
<p>This project implements variadic functions using macros (which are exempt from the normal macro rule of being all caps). A typical variadic function definition:</p>
<code-block type="c">
typedef struct StoragePathCreateParam
{
bool errorOnExists;
bool noParentCreate;
mode_t mode;
} StoragePathCreateParam;
#define storagePathCreateP(this, pathExp, ...) \
storagePathCreate(this, pathExp, (StoragePathCreateParam){__VA_ARGS__})
#define storagePathCreateP(this, pathExp) \
storagePathCreate(this, pathExp, (StoragePathCreateParam){0})
void storagePathCreate(const Storage *this, const String *pathExp, StoragePathCreateParam param);
</code-block>
<p>Continuation characters should be aligned at column 132 (unlike the example above that has been shortened for display purposes).</p>
<p>This function can be called without variable parameters:</p>
<code-block type="c">
storagePathCreateP(storageLocal(), "/tmp/pgbackrest");
</code-block>
<p>Or with variable parameters:</p>
<code-block type="c">
storagePathCreateP(storageLocal(), "/tmp/pgbackrest", .errorOnExists = true, .mode = 0777);
</code-block>
<p>If the majority of functions in a module or object are variadic it is best to provide macros for all functions even if they do not have variable parameters. Do not use the base function when variadic macros exist.</p>
</section>
</section>
<!-- ======================================================================================================================= -->
<section id="testing">
<title>Testing</title>
<!-- =================================================================================================================== -->
<section id="uncoverable-uncovered">
<title>Uncoverable/Uncovered Code</title>
<!-- =============================================================================================================== -->
<section id="uncoverable">
<title>Uncoverable Code</title>
<p>The <id>uncoverable</id> keyword marks code that can never be covered. For instance, a function that never returns because it always throws an error. Uncoverable code should be rare to non-existent outside the common libraries and test code.</p>
<code-block type="c">
} // {uncoverable - function throws error so never returns}
</code-block>
<p>Subsequent code that is uncoverable for the same reason is marked with <code>// {+uncoverable}</code>.</p>
</section>
<!-- =============================================================================================================== -->
<section id="uncovered">
<title>Uncovered Code</title>
<p>Marks code that is not tested for one reason or another. This should be kept to a minimum and an excuse given for each instance.</p>
<code-block type="c">
exit(EXIT_FAILURE); // {uncovered - test harness does not support non-zero exit}
</code-block>
<p>Subsequent code that is uncovered for the same reason is marked with `// {+uncovered}`.</p>
</section>
</section>
</section>
</doc>
|