robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e8ace82093
master
1*dev_style.txt* Nvim 2 3 4 NVIM REFERENCE MANUAL 5 6 7Nvim style guide *dev-style* 8 9Style guidelines for developers working on Nvim's source code. 10 11License: CC-By 3.0 https://creativecommons.org/licenses/by/3.0/ 12 13 Type |gO| to see the table of contents. 14 15============================================================================== 16Background 17 18One way in which we keep the code base manageable is by enforcing consistency. 19It is very important that any programmer be able to look at another's code and 20quickly understand it. 21 22Maintaining a uniform style and following conventions means that we can more 23easily use "pattern-matching" to infer what various symbols are and what 24invariants are true about them. Creating common, required idioms and patterns 25makes code much easier to understand. 26 27In some cases there might be good arguments for changing certain style rules, 28but we nonetheless keep things as they are in order to preserve consistency. 29 30 31============================================================================== 32Header Files *dev-style-header* 33 34 35Header guard ~ 36 37All header files should start with `#pragma once` to prevent multiple inclusion. 38 39 In foo/bar.h: 40>c 41 #pragma once 42< 43 44Headers system ~ 45 46Nvim uses two types of headers. There are "normal" headers and "defs" headers. 47Typically, each normal header will have a corresponding defs header, e.g. 48`fileio.h` and `fileio_defs.h`. This distinction is done to minimize 49recompilation on change. The reason for this is because adding a function or 50modifying a function's signature happens more frequently than changing a type 51The goal is to achieve the following: 52 53- All headers (defs and normal) must include only defs headers, system 54 headers, and generated declarations. In other words, headers must not 55 include normal headers. 56 57- Source (.c) files may include all headers, but should only include normal 58 headers if they need symbols and not types. 59 60Use the following guideline to determine what to put where: 61 62Symbols: 63 - regular function declarations 64 - `extern` variables (including the `EXTERN` macro) 65 66Non-symbols: 67 - macros, i.e. `#define` 68 - static inline functions with the `FUNC_ATTR_ALWAYS_INLINE` attribute 69 - typedefs 70 - structs 71 - enums 72 73- All symbols must be moved to normal headers. 74 75- Non-symbols used by multiple headers should be moved to defs headers. This 76 is to ensure headers only include defs headers. Conversely, non-symbols used 77 by only a single header should be moved to that header. 78 79- EXCEPTION: if the macro calls a function, then it must be moved to a normal 80 header. 81 82============================================================================== 83Scoping *dev-style-scope* 84 85Local Variables ~ 86 87Place a function's variables in the narrowest scope possible, and initialize 88variables in the declaration. 89 90C99 allows you to declare variables anywhere in a function. Declare them in as 91local a scope as possible, and as close to the first use as possible. This 92makes it easier for the reader to find the declaration and see what type the 93variable is and what it was initialized to. In particular, initialization 94should be used instead of declaration and assignment, e.g. >c 95 96 int i; 97 i = f(); // ❌: initialization separate from declaration. 98 99 int j = g(); // ✅: declaration has initialization. 100 101 102Initialization ~ 103 104Multiple declarations can be defined in one line if they aren't initialized, 105but each initialization should be done on a separate line. 106 107>c 108 int i; 109 int j; // ✅ 110 int i, j; // ✅: multiple declarations, no initialization. 111 int i = 0; 112 int j = 0; // ✅: one initialization per line. 113 114 int i = 0, j; // ❌: multiple declarations with initialization. 115 int i = 0, j = 0; // ❌: multiple declarations with initialization. 116 117============================================================================== 118Nvim-Specific Magic 119 120clint ~ 121 122Use `clint.lua` to detect style errors. 123 124`src/clint.lua` is a Lua script that reads a source file and identifies 125style errors. It is not perfect, and has both false positives and false 126negatives, but it is still a valuable tool. False positives can be ignored by 127putting `// NOLINT` at the end of the line. 128 129uncrustify ~ 130 131src/uncrustify.cfg is the authority for expected code formatting, for cases 132not covered by clint.lua. We remove checks in clint.lua if they are covered by 133uncrustify rules. 134 135============================================================================== 136Other C Features *dev-style-features* 137 138 139Variable-Length Arrays and alloca() ~ 140 141We do not allow variable-length arrays or `alloca()`. 142 143Variable-length arrays can cause hard to detect stack overflows. 144 145 146Postincrement and Postdecrement ~ 147 148Use postfix form (`i++`) in statements. >c 149 150 for (int i = 0; i < 3; i++) { } 151 int j = ++i; // ✅: ++i is used as an expression. 152 153 for (int i = 0; i < 3; ++i) { } 154 ++i; // ❌: ++i is used as a statement. 155 156 157Use of const ~ 158 159Use `const` pointers whenever possible. Avoid `const` on non-pointer parameter definitions. 160 161 Where to put the const ~ 162 163 Some people favor the form `int const *foo` to `const int *foo` . They 164 argue that this is more readable because it's more consistent: it keeps 165 the rule that `const` always follows the object it's describing. However, 166 this consistency argument doesn't apply in codebases with few 167 deeply-nested pointer expressions since most `const` expressions have only 168 one `const`, and it applies to the underlying value. In such cases, there's 169 no consistency to maintain. Putting the `const` first is arguably more 170 readable, since it follows English in putting the "adjective" (`const`) 171 before the "noun" (`int`). 172 173 That said, while we encourage putting `const` first, we do not require it. 174 But be consistent with the code around you! >c 175 176 void foo(const char *p, int i); 177 } 178 179 int foo(const int a, const bool b) { 180 } 181 182 int foo(int *const p) { 183 } 184 185 186Integer Types ~ 187 188Of the built-in integer types only use `char`, `int`, `uint8_t`, `int8_t`, 189`uint16_t`, `int16_t`, `uint32_t`, `int32_t`, `uint64_t`, `int64_t`, 190`uintmax_t`, `intmax_t`, `size_t`, `ssize_t`, `uintptr_t`, `intptr_t`, and 191`ptrdiff_t`. 192 193Use `int` for error codes and local, trivial variables only. 194 195Use care when converting integer types. Integer conversions and promotions can 196cause non-intuitive behavior. Note that the signedness of `char` is 197implementation defined. 198 199Public facing types must have fixed width (`uint8_t`, etc.) 200 201There are no convenient `printf` format placeholders for fixed width types. 202Cast to `uintmax_t` or `intmax_t` if you have to format fixed width integers. 203 204Type unsigned signed 205`char` `%hhu` `%hhd` 206`int` n/a `%d` 207`(u)intmax_t` `%ju` `%jd` 208`(s)size_t` `%zu` `%zd` 209`ptrdiff_t` `%tu` `%td` 210 211 212Booleans ~ 213 214Use `bool` to represent boolean values. >c 215 216 int loaded = 1; // ❌: loaded should have type bool. 217 218 219Conditions ~ 220 221Don't use "yoda-conditions". Use at most one assignment per condition. >c 222 223 if (1 == x) { 224 225 if (x == 1) { //use this order 226 227 if ((x = f()) && (y = g())) { 228 229 230Function declarations ~ 231 232Every function must not have a separate declaration. 233 234Function declarations are created by the gen_declarations.lua script. >c 235 236 static void f(void); 237 238 static void f(void) 239 { 240 ... 241 } 242 243 244General translation unit layout ~ 245 246The definitions of public functions precede the definitions of static 247functions. >c 248 249 <HEADER> 250 251 <PUBLIC FUNCTION DEFINITIONS> 252 253 <STATIC FUNCTION DEFINITIONS> 254 255 256Integration with declarations generator ~ 257 258Every C file must contain #include of the generated header file. 259 260Include must go after other #includes and typedefs in .c files and after 261everything else in header files. It is allowed to omit #include in a .c file 262if .c file does not contain any static functions. 263 264Included file name consists of the .c file name without extension, preceded by 265the directory name relative to src/nvim. Name of the file containing static 266functions declarations ends with `.c.generated.h`, `*.h.generated.h` files 267contain only non-static function declarations. >c 268 269 // src/nvim/foo.c file 270 #include <stddef.h> 271 272 typedef int FooType; 273 274 #include "foo.c.generated.h" 275 276 … 277 278 279 // src/nvim/foo.h file 280 #pragma once 281 282 … 283 284 #include "foo.h.generated.h" 285 286 28764-bit Portability ~ 288 289Code should be 64-bit and 32-bit friendly. Bear in mind problems of printing, 290comparisons, and structure alignment. 291 292- Remember that `sizeof(void *)` != `sizeof(int)`. Use `intptr_t` if you want 293 a pointer-sized integer. 294 295- You may need to be careful with structure alignments, particularly for 296 structures being stored on disk. Any class/structure with a 297 `int64_t`/`uint64_t` member will by default end up being 8-byte aligned on a 298 64-bit system. If you have such structures being shared on disk between 299 32-bit and 64-bit code, you will need to ensure that they are packed the 300 same on both architectures. Most compilers offer a way to alter structure 301 alignment. For gcc, you can use `__attribute__((packed))`. MSVC offers 302 `#pragma pack()` and `__declspec(align())`. 303 304- Use the `LL` or `ULL` suffixes as needed to create 64-bit constants. For 305 example: >c 306 307 int64_t my_value = 0x123456789LL; 308 uint64_t my_mask = 3ULL << 48; 309 310 311sizeof ~ 312 313Prefer `sizeof(varname)` to `sizeof(type)`. 314 315Use `sizeof(varname)` when you take the size of a particular variable. 316`sizeof(varname)` will update appropriately if someone changes the variable 317type either now or later. You may use `sizeof(type)` for code unrelated to any 318particular variable, such as code that manages an external or internal data 319format where a variable of an appropriate C type is not convenient. >c 320 321 Struct data; 322 memset(&data, 0, sizeof(data)); 323 324 memset(&data, 0, sizeof(Struct)); 325 326 if (raw_size < sizeof(int)) { 327 fprintf(stderr, "compressed record not big enough for count: %ju", raw_size); 328 return false; 329 } 330 331 332============================================================================== 333Naming *dev-style-naming* 334 335The most important consistency rules are those that govern naming. The style 336of a name immediately informs us what sort of thing the named entity is: a 337type, a variable, a function, a constant, a macro, etc., without requiring us 338to search for the declaration of that entity. The pattern-matching engine in 339our brains relies a great deal on these naming rules. 340 341Naming rules are pretty arbitrary, but we feel that consistency is more 342important than individual preferences in this area, so regardless of whether 343you find them sensible or not, the rules are the rules. 344 345 346General Naming Rules ~ 347 348Function names, variable names, and filenames should be descriptive; eschew 349abbreviation. 350 351Give as descriptive a name as possible, within reason. Do not worry about 352saving horizontal space as it is far more important to make your code 353immediately understandable by a new reader. Do not use abbreviations that are 354ambiguous or unfamiliar to readers outside your project, and do not abbreviate 355by deleting letters within a word. >c 356 357 int price_count_reader; // No abbreviation. 358 int num_errors; // "num" is a widespread convention. 359 int num_dns_connections; // Most people know what "DNS" stands for. 360 361 int n; // Meaningless. 362 int nerr; // Ambiguous abbreviation. 363 int n_comp_conns; // Ambiguous abbreviation. 364 int wgc_connections; // Only your group knows what this stands for. 365 int pc_reader; // Lots of things can be abbreviated "pc". 366 int cstmr_id; // Deletes internal letters. 367 368 369File Names ~ 370 371Filenames should be all lowercase and can include underscores (`_`). 372 373Use underscores to separate words. Examples of acceptable file names: > 374 375 my_useful_file.c 376 getline_fix.c // ✅: getline refers to the glibc function. 377 378C files should end in `.c` and header files should end in `.h`. 379 380Do not use filenames that already exist in `/usr/include`, such as `db.h`. 381 382In general, make your filenames very specific. For example, use 383`http_server_logs.h` rather than `logs.h`. 384 385 386Type Names ~ 387 388Typedef-ed structs and enums start with a capital letter and have a capital 389letter for each new word, with no underscores: `MyExcitingStruct`. 390 391Non-Typedef-ed structs and enums are all lowercase with underscores between 392words: `struct my_exciting_struct` . >c 393 394 struct my_struct { 395 ... 396 }; 397 typedef struct my_struct MyAwesomeStruct; 398 399 400Variable Names ~ 401 402Variable names are all lowercase, with underscores between words. For 403instance: `my_exciting_local_variable`. 404 405 Common Variable names ~ 406 407 For example: >c 408 409 string table_name; // ✅: uses underscore. 410 string tablename; // ✅: all lowercase. 411 412 string tableName; // ❌: mixed case. 413< 414 415 Struct Variables ~ 416 417 Data members in structs should be named like regular variables. >c 418 419 struct url_table_properties { 420 string name; 421 int num_entries; 422 } 423< 424 425 Global Variables ~ 426 427 Don't use global variables unless absolutely necessary. Prefix global 428 variables with `g_`. 429 430 431Constant Names ~ 432 433Use a `k` followed by mixed case: `kDaysInAWeek`. 434 435All compile-time constants, whether they are declared locally or globally, 436follow a slightly different naming convention from other variables. Use a `k` 437followed by words with uppercase first letters: >c 438 439 const int kDaysInAWeek = 7; 440 441Function Names ~ 442 443Function names are all lowercase, with underscores between words. For 444instance: `my_exceptional_function()`. All functions in the same header file 445should have a common prefix. 446 447In `os_unix.h`: >c 448 449 void unix_open(const char *path); 450 void unix_user_id(void); 451 452If your function crashes upon an error, you should append `or_die` to the 453function name. This only applies to functions which could be used by 454production code and to errors that are reasonably likely to occur during 455normal operation. 456 457 458Enumerator Names ~ 459 460Enumerators should be named like constants: `kEnumName`. >c 461 462 enum url_table_errors { 463 kOK = 0, 464 kErrorOutOfMemory, 465 kErrorMalformedInput, 466 }; 467 468 469Macro Names ~ 470 471They're like this: `MY_MACRO_THAT_SCARES_CPP_DEVELOPERS`. >c 472 473 #define ROUND(x) ... 474 #define PI_ROUNDED 5.0 475 476 477============================================================================== 478Comments *dev-style-comments* 479 480Comments are vital to keeping our code readable. The following rules describe 481what you should comment and where. But remember: while comments are very 482important, the best code is self-documenting. 483 484When writing your comments, write for your audience: the next contributor who 485will need to understand your code. Be generous — the next one may be you! 486 487Nvim uses Doxygen comments. 488 489 490Comment Style ~ 491 492Use the `//`-style syntax only. >c 493 494 // This is a comment spanning 495 // multiple lines 496 f(); 497 498 499File Comments ~ 500 501Start each file with a description of its contents. 502 503 Legal Notice ~ 504 505 We have no such thing. These things are in LICENSE and only there. 506 507 File Contents ~ 508 509 Every file should have a comment at the top describing its contents. 510 511 Generally a `.h` file will describe the variables and functions that are 512 declared in the file with an overview of what they are for and how they 513 are used. A `.c` file should contain more information about implementation 514 details or discussions of tricky algorithms. If you feel the 515 implementation details or a discussion of the algorithms would be useful 516 for someone reading the `.h`, feel free to put it there instead, but 517 mention in the `.c` that the documentation is in the `.h` file. 518 519 Do not duplicate comments in both the `.h` and the `.c`. Duplicated 520 comments diverge. >c 521 522 /// A brief description of this file. 523 /// 524 /// A longer description of this file. 525 /// Be very generous here. 526 527 528Struct Comments ~ 529 530Every struct definition should have accompanying comments that describes what 531it is for and how it should be used. >c 532 533 /// Window info stored with a buffer. 534 /// 535 /// Two types of info are kept for a buffer which are associated with a 536 /// specific window: 537 /// 1. Each window can have a different line number associated with a 538 /// buffer. 539 /// 2. The window-local options for a buffer work in a similar way. 540 /// The window-info is kept in a list at g_wininfo. It is kept in 541 /// most-recently-used order. 542 struct win_info { 543 /// Next entry or NULL for last entry. 544 WinInfo *wi_next; 545 /// Previous entry or NULL for first entry. 546 WinInfo *wi_prev; 547 /// Pointer to window that did the wi_fpos. 548 Win *wi_win; 549 ... 550 }; 551 552If the field comments are short, you can also put them next to the field. But 553be consistent within one struct, and follow the necessary doxygen style. >c 554 555 struct wininfo_S { 556 WinInfo *wi_next; ///< Next entry or NULL for last entry. 557 WinInfo *wi_prev; ///< Previous entry or NULL for first entry. 558 Win *wi_win; ///< Pointer to window that did the wi_fpos. 559 ... 560 }; 561 562If you have already described a struct in detail in the comments at the top of 563your file feel free to simply state "See comment at top of file for a complete 564description", but be sure to have some sort of comment. 565 566Document the synchronization assumptions the struct makes, if any. If an 567instance of the struct can be accessed by multiple threads, take extra care to 568document the rules and invariants surrounding multithreaded use. 569 570 571Function Comments ~ 572 573Declaration comments describe use of the function; comments at the definition 574of a function describe operation. 575 576 Function Declarations ~ 577 578 Every function declaration should have comments immediately preceding it 579 that describe what the function does and how to use it. These comments 580 should be descriptive ("Opens the file") rather than imperative ("Open the 581 file"); the comment describes the function, it does not tell the function 582 what to do. In general, these comments do not describe how the function 583 performs its task. Instead, that should be left to comments in the 584 function definition. 585 586 Types of things to mention in comments at the function declaration: 587 588 - If the function allocates memory that the caller must free. 589 - Whether any of the arguments can be a null pointer. 590 - If there are any performance implications of how a function is used. 591 - If the function is re-entrant. What are its synchronization assumptions? >c 592 /// Brief description of the function. 593 /// 594 /// Detailed description. 595 /// May span multiple paragraphs. 596 /// 597 /// @param arg1 Description of arg1 598 /// @param arg2 Description of arg2. May span 599 /// multiple lines. 600 /// 601 /// @return Description of the return value. 602 Iterator *get_iterator(void *arg1, void *arg2); 603< 604 605 Function Definitions ~ 606 607 If there is anything tricky about how a function does its job, the 608 function definition should have an explanatory comment. For example, in 609 the definition comment you might describe any coding tricks you use, give 610 an overview of the steps you go through, or explain why you chose to 611 implement the function in the way you did rather than using a viable 612 alternative. For instance, you might mention why it must acquire a lock 613 for the first half of the function but why it is not needed for the second 614 half. 615 616 Note you should not just repeat the comments given with the function 617 declaration, in the `.h` file or wherever. It's okay to recapitulate 618 briefly what the function does, but the focus of the comments should be on 619 how it does it. >c 620 621 // Note that we don't use Doxygen comments here. 622 Iterator *get_iterator(void *arg1, void *arg2) 623 { 624 ... 625 } 626 627 628Variable Comments ~ 629 630In general the actual name of the variable should be descriptive enough to 631give a good idea of what the variable is used for. In certain cases, more 632comments are required. 633 634 Global Variables ~ 635 636 All global variables should have a comment describing what they are and 637 what they are used for. For example: >c 638 639 /// The total number of tests cases that we run 640 /// through in this regression test. 641 const int kNumTestCases = 6; 642 643 644Implementation Comments ~ 645 646In your implementation you should have comments in tricky, non-obvious, 647interesting, or important parts of your code. 648 649 Line Comments ~ 650 651 Also, lines that are non-obvious should get a comment at the end of the 652 line. These end-of-line comments should be separated from the code by 2 653 spaces. Example: >c 654 655 // If we have enough memory, mmap the data portion too. 656 mmap_budget = max<int64>(0, mmap_budget - index_->length()); 657 if (mmap_budget >= data_size_ && !MmapData(mmap_chunk_bytes, mlock)) { 658 return; // Error already logged. 659 } 660< 661 Note that there are both comments that describe what the code is doing, 662 and comments that mention that an error has already been logged when the 663 function returns. 664 665 If you have several comments on subsequent lines, it can often be more 666 readable to line them up: >c 667 668 do_something(); // Comment here so the comments line up. 669 do_something_else_that_is_longer(); // Comment here so there are two spaces between 670 // the code and the comment. 671 { // One space before comment when opening a new scope is allowed, 672 // thus the comment lines up with the following comments and code. 673 do_something_else(); // Two spaces before line comments normally. 674 } 675< 676 677 NULL, true/false, 1, 2, 3... ~ 678 679 When you pass in a null pointer, boolean, or literal integer values to 680 functions, you should consider adding a comment about what they are, or 681 make your code self-documenting by using constants. For example, compare: 682 >c 683 684 bool success = calculate_something(interesting_value, 685 10, 686 false, 687 NULL); // What are these arguments?? 688< 689 690 versus: >c 691 692 bool success = calculate_something(interesting_value, 693 10, // Default base value. 694 false, // Not the first time we're calling this. 695 NULL); // No callback. 696< 697 698 Or alternatively, constants or self-describing variables: >c 699 700 const int kDefaultBaseValue = 10; 701 const bool kFirstTimeCalling = false; 702 Callback *null_callback = NULL; 703 bool success = calculate_something(interesting_value, 704 kDefaultBaseValue, 705 kFirstTimeCalling, 706 null_callback); 707< 708 709 Don'ts ~ 710 711 Note that you should never describe the code itself. Assume that the 712 person reading the code knows C better than you do, even though he or she 713 does not know what you are trying to do: >c 714 715 // Now go through the b array and make sure that if i occurs, 716 // the next element is i+1. 717 ... // Geez. What a useless comment. 718 719 720Punctuation, Spelling and Grammar ~ 721 722Pay attention to punctuation, spelling, and grammar; it is easier to read 723well-written comments than badly written ones. 724 725Comments should be as readable as narrative text, with proper capitalization 726and punctuation. In many cases, complete sentences are more readable than 727sentence fragments. Shorter comments, such as comments at the end of a line of 728code, can sometimes be less formal, but you should be consistent with your 729style. 730 731Although it can be frustrating to have a code reviewer point out that you are 732using a comma when you should be using a semicolon, it is very important that 733source code maintain a high level of clarity and readability. Proper 734punctuation, spelling, and grammar help with that goal. 735 736 737TODO Comments ~ 738 739Use `TODO` comments for code that is temporary, a short-term solution, or 740good-enough but not perfect. 741 742`TODO`s should include the string `TODO` in all caps, followed by the name, 743email address, or other identifier of the person who can best provide context 744about the problem referenced by the `TODO`. The main purpose is to have a 745consistent `TODO` format that can be searched to find the person who can 746provide more details upon request. A `TODO` is not a commitment that the 747person referenced will fix the problem. Thus when you create a `TODO`, it is 748almost always your name that is given. >c 749 750 // TODO(kl@gmail.com): Use a "*" here for concatenation operator. 751 // TODO(Zeke): change this to use relations. 752 753If your `TODO` is of the form "At a future date do something" make sure that 754you either include a very specific date ("Fix by November 2005") or a very 755specific event ("Remove this code when all clients can handle XML 756responses."). 757 758 759Deprecation Comments ~ 760 761Mark deprecated interface points with `@deprecated` docstring token. 762 763You can mark an interface as deprecated by writing a comment containing the 764word `@deprecated` in all caps. The comment goes either before the declaration 765of the interface or on the same line as the declaration. 766 767After `@deprecated`, write your name, email, or other identifier in 768parentheses. 769 770A deprecation comment must include simple, clear directions for people to fix 771their callsites. In C, you can implement a deprecated function as an inline 772function that calls the new interface point. 773 774Marking an interface point `DEPRECATED` will not magically cause any callsites 775to change. If you want people to actually stop using the deprecated facility, 776you will have to fix the callsites yourself or recruit a crew to help you. 777 778New code should not contain calls to deprecated interface points. Use the new 779interface point instead. If you cannot understand the directions, find the 780person who created the deprecation and ask them for help using the new 781interface point. 782 783 784============================================================================== 785Formatting *dev-style-format* 786 787Coding style and formatting are pretty arbitrary, but a project is much easier 788to follow if everyone uses the same style. Individuals may not agree with 789every aspect of the formatting rules, and some of the rules may take some 790getting used to, but it is important that all project contributors follow the 791style rules so that they can all read and understand everyone's code easily. 792 793 794Non-ASCII Characters ~ 795 796Non-ASCII characters should be rare, and must use UTF-8 formatting. 797 798You shouldn't hard-code user-facing text in source (OR SHOULD YOU?), even 799English, so use of non-ASCII characters should be rare. However, in certain 800cases it is appropriate to include such words in your code. For example, if 801your code parses data files from foreign sources, it may be appropriate to 802hard-code the non-ASCII string(s) used in those data files as delimiters. More 803commonly, unittest code (which does not need to be localized) might contain 804non-ASCII strings. In such cases, you should use UTF-8, since that is an 805encoding understood by most tools able to handle more than just ASCII. 806 807Hex encoding is also OK, and encouraged where it enhances readability — for 808example, `"\uFEFF"`, is the Unicode zero-width no-break space character, which 809would be invisible if included in the source as straight UTF-8. 810 811 812Braced Initializer Lists ~ 813 814Format a braced list exactly like you would format a function call in its 815place but with one space after the `{` and one space before the `}` 816 817If the braced list follows a name (e.g. a type or variable name), format as if 818the `{}` were the parentheses of a function call with that name. If there is 819no name, assume a zero-length name. >c 820 821 struct my_struct m = { // Here, you could also break before {. 822 superlongvariablename1, 823 superlongvariablename2, 824 { short, interior, list }, 825 { interiorwrappinglist, 826 interiorwrappinglist2 } }; 827 828 829Loops and Switch Statements ~ 830 831Annotate non-trivial fall-through between cases. 832 833If not conditional on an enumerated value, switch statements should always 834have a `default` case (in the case of an enumerated value, the compiler will 835warn you if any values are not handled). If the default case should never 836execute, simply use `abort()`: >c 837 838 switch (var) { 839 case 0: 840 ... 841 break; 842 case 1: 843 ... 844 break; 845 default: 846 abort(); 847 } 848 849Switch statements that are conditional on an enumerated value should not have 850a `default` case if it is exhaustive. Explicit case labels are preferred over 851`default`, even if it leads to multiple case labels for the same code. For 852example, instead of: >c 853 854 case A: 855 ... 856 case B: 857 ... 858 case C: 859 ... 860 default: 861 ... 862 863You should use: >c 864 865 case A: 866 ... 867 case B: 868 ... 869 case C: 870 ... 871 case D: 872 case E: 873 case F: 874 ... 875 876Certain compilers do not recognize an exhaustive enum switch statement as 877exhaustive, which causes compiler warnings when there is a return statement in 878every case of a switch statement, but no catch-all return statement. To fix 879these spurious errors, you are advised to use `UNREACHABLE` after the switch 880statement to explicitly tell the compiler that the switch statement always 881returns and any code after it is unreachable. For example: >c 882 883 enum { A, B, C } var; 884 ... 885 switch (var) { 886 case A: 887 return 1; 888 case B: 889 return 2; 890 case C: 891 return 3; 892 } 893 UNREACHABLE; 894 895Return Values ~ 896 897Do not needlessly surround the `return` expression with parentheses. 898 899Use parentheses in `return expr;` only where you would also use them in 900`x = expr;`. >c 901 902 return result; 903 return (some_long_condition && another_condition); 904 905 return (value); // You wouldn't write var = (value); 906 return(result); // return is not a function! 907 908 909Horizontal Whitespace ~ 910 911Use of horizontal whitespace depends on location. 912 913 Variables ~ 914>c 915 int long_variable = 0; // Don't align assignments. 916 int i = 1; 917 918 struct my_struct { // Exception: struct arrays. 919 const char *boy; 920 const char *girl; 921 int pos; 922 } my_variable[] = { 923 { "Mia", "Michael", 8 }, 924 { "Elizabeth", "Aiden", 10 }, 925 { "Emma", "Mason", 2 }, 926 }; 927< 928 929============================================================================== 930Parting Words 931 932The style guide is intended to make the code more readable. If you think you 933must violate its rules for the sake of clarity, do it! But please add a note 934to your pull request explaining your reasoning. 935 936 937 vim:tw=78:ts=8:et:ft=help:norl: