dev_style.txt (31411B)
1 *dev_style.txt* Nvim 2 3 4 NVIM REFERENCE MANUAL 5 6 7 Nvim style guide *dev-style* 8 9 Style guidelines for developers working on Nvim's source code. 10 11 License: CC-By 3.0 https://creativecommons.org/licenses/by/3.0/ 12 13 Type |gO| to see the table of contents. 14 15 ============================================================================== 16 Background 17 18 One way in which we keep the code base manageable is by enforcing consistency. 19 It is very important that any programmer be able to look at another's code and 20 quickly understand it. 21 22 Maintaining a uniform style and following conventions means that we can more 23 easily use "pattern-matching" to infer what various symbols are and what 24 invariants are true about them. Creating common, required idioms and patterns 25 makes code much easier to understand. 26 27 In some cases there might be good arguments for changing certain style rules, 28 but we nonetheless keep things as they are in order to preserve consistency. 29 30 31 ============================================================================== 32 Header Files *dev-style-header* 33 34 35 Header guard ~ 36 37 All header files should start with `#pragma once` to prevent multiple inclusion. 38 39 In foo/bar.h: 40 >c 41 #pragma once 42 < 43 44 Headers system ~ 45 46 Nvim uses two types of headers. There are "normal" headers and "defs" headers. 47 Typically, each normal header will have a corresponding defs header, e.g. 48 `fileio.h` and `fileio_defs.h`. This distinction is done to minimize 49 recompilation on change. The reason for this is because adding a function or 50 modifying a function's signature happens more frequently than changing a type 51 The 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 60 Use the following guideline to determine what to put where: 61 62 Symbols: 63 - regular function declarations 64 - `extern` variables (including the `EXTERN` macro) 65 66 Non-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 ============================================================================== 83 Scoping *dev-style-scope* 84 85 Local Variables ~ 86 87 Place a function's variables in the narrowest scope possible, and initialize 88 variables in the declaration. 89 90 C99 allows you to declare variables anywhere in a function. Declare them in as 91 local a scope as possible, and as close to the first use as possible. This 92 makes it easier for the reader to find the declaration and see what type the 93 variable is and what it was initialized to. In particular, initialization 94 should 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 102 Initialization ~ 103 104 Multiple declarations can be defined in one line if they aren't initialized, 105 but 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 ============================================================================== 118 Nvim-Specific Magic 119 120 clint ~ 121 122 Use `clint.lua` to detect style errors. 123 124 `src/clint.lua` is a Lua script that reads a source file and identifies 125 style errors. It is not perfect, and has both false positives and false 126 negatives, but it is still a valuable tool. False positives can be ignored by 127 putting `// NOLINT` at the end of the line. 128 129 uncrustify ~ 130 131 src/uncrustify.cfg is the authority for expected code formatting, for cases 132 not covered by clint.lua. We remove checks in clint.lua if they are covered by 133 uncrustify rules. 134 135 ============================================================================== 136 Other C Features *dev-style-features* 137 138 139 Variable-Length Arrays and alloca() ~ 140 141 We do not allow variable-length arrays or `alloca()`. 142 143 Variable-length arrays can cause hard to detect stack overflows. 144 145 146 Postincrement and Postdecrement ~ 147 148 Use 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 157 Use of const ~ 158 159 Use `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 186 Integer Types ~ 187 188 Of 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 193 Use `int` for error codes and local, trivial variables only. 194 195 Use care when converting integer types. Integer conversions and promotions can 196 cause non-intuitive behavior. Note that the signedness of `char` is 197 implementation defined. 198 199 Public facing types must have fixed width (`uint8_t`, etc.) 200 201 There are no convenient `printf` format placeholders for fixed width types. 202 Cast to `uintmax_t` or `intmax_t` if you have to format fixed width integers. 203 204 Type 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 212 Booleans ~ 213 214 Use `bool` to represent boolean values. >c 215 216 int loaded = 1; // ❌: loaded should have type bool. 217 218 219 Conditions ~ 220 221 Don'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 230 Function declarations ~ 231 232 Every function must not have a separate declaration. 233 234 Function 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 244 General translation unit layout ~ 245 246 The definitions of public functions precede the definitions of static 247 functions. >c 248 249 <HEADER> 250 251 <PUBLIC FUNCTION DEFINITIONS> 252 253 <STATIC FUNCTION DEFINITIONS> 254 255 256 Integration with declarations generator ~ 257 258 Every C file must contain #include of the generated header file. 259 260 Include must go after other #includes and typedefs in .c files and after 261 everything else in header files. It is allowed to omit #include in a .c file 262 if .c file does not contain any static functions. 263 264 Included file name consists of the .c file name without extension, preceded by 265 the directory name relative to src/nvim. Name of the file containing static 266 functions declarations ends with `.c.generated.h`, `*.h.generated.h` files 267 contain 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 287 64-bit Portability ~ 288 289 Code should be 64-bit and 32-bit friendly. Bear in mind problems of printing, 290 comparisons, 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 311 sizeof ~ 312 313 Prefer `sizeof(varname)` to `sizeof(type)`. 314 315 Use `sizeof(varname)` when you take the size of a particular variable. 316 `sizeof(varname)` will update appropriately if someone changes the variable 317 type either now or later. You may use `sizeof(type)` for code unrelated to any 318 particular variable, such as code that manages an external or internal data 319 format 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 ============================================================================== 333 Naming *dev-style-naming* 334 335 The most important consistency rules are those that govern naming. The style 336 of a name immediately informs us what sort of thing the named entity is: a 337 type, a variable, a function, a constant, a macro, etc., without requiring us 338 to search for the declaration of that entity. The pattern-matching engine in 339 our brains relies a great deal on these naming rules. 340 341 Naming rules are pretty arbitrary, but we feel that consistency is more 342 important than individual preferences in this area, so regardless of whether 343 you find them sensible or not, the rules are the rules. 344 345 346 General Naming Rules ~ 347 348 Function names, variable names, and filenames should be descriptive; eschew 349 abbreviation. 350 351 Give as descriptive a name as possible, within reason. Do not worry about 352 saving horizontal space as it is far more important to make your code 353 immediately understandable by a new reader. Do not use abbreviations that are 354 ambiguous or unfamiliar to readers outside your project, and do not abbreviate 355 by 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 369 File Names ~ 370 371 Filenames should be all lowercase and can include underscores (`_`). 372 373 Use 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 378 C files should end in `.c` and header files should end in `.h`. 379 380 Do not use filenames that already exist in `/usr/include`, such as `db.h`. 381 382 In general, make your filenames very specific. For example, use 383 `http_server_logs.h` rather than `logs.h`. 384 385 386 Type Names ~ 387 388 Typedef-ed structs and enums start with a capital letter and have a capital 389 letter for each new word, with no underscores: `MyExcitingStruct`. 390 391 Non-Typedef-ed structs and enums are all lowercase with underscores between 392 words: `struct my_exciting_struct` . >c 393 394 struct my_struct { 395 ... 396 }; 397 typedef struct my_struct MyAwesomeStruct; 398 399 400 Variable Names ~ 401 402 Variable names are all lowercase, with underscores between words. For 403 instance: `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 431 Constant Names ~ 432 433 Use a `k` followed by mixed case: `kDaysInAWeek`. 434 435 All compile-time constants, whether they are declared locally or globally, 436 follow a slightly different naming convention from other variables. Use a `k` 437 followed by words with uppercase first letters: >c 438 439 const int kDaysInAWeek = 7; 440 441 Function Names ~ 442 443 Function names are all lowercase, with underscores between words. For 444 instance: `my_exceptional_function()`. All functions in the same header file 445 should have a common prefix. 446 447 In `os_unix.h`: >c 448 449 void unix_open(const char *path); 450 void unix_user_id(void); 451 452 If your function crashes upon an error, you should append `or_die` to the 453 function name. This only applies to functions which could be used by 454 production code and to errors that are reasonably likely to occur during 455 normal operation. 456 457 458 Enumerator Names ~ 459 460 Enumerators should be named like constants: `kEnumName`. >c 461 462 enum url_table_errors { 463 kOK = 0, 464 kErrorOutOfMemory, 465 kErrorMalformedInput, 466 }; 467 468 469 Macro Names ~ 470 471 They're like this: `MY_MACRO_THAT_SCARES_CPP_DEVELOPERS`. >c 472 473 #define ROUND(x) ... 474 #define PI_ROUNDED 5.0 475 476 477 ============================================================================== 478 Comments *dev-style-comments* 479 480 Comments are vital to keeping our code readable. The following rules describe 481 what you should comment and where. But remember: while comments are very 482 important, the best code is self-documenting. 483 484 When writing your comments, write for your audience: the next contributor who 485 will need to understand your code. Be generous — the next one may be you! 486 487 Nvim uses Doxygen comments. 488 489 490 Comment Style ~ 491 492 Use the `//`-style syntax only. >c 493 494 // This is a comment spanning 495 // multiple lines 496 f(); 497 498 499 File Comments ~ 500 501 Start 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 528 Struct Comments ~ 529 530 Every struct definition should have accompanying comments that describes what 531 it 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 552 If the field comments are short, you can also put them next to the field. But 553 be 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 562 If you have already described a struct in detail in the comments at the top of 563 your file feel free to simply state "See comment at top of file for a complete 564 description", but be sure to have some sort of comment. 565 566 Document the synchronization assumptions the struct makes, if any. If an 567 instance of the struct can be accessed by multiple threads, take extra care to 568 document the rules and invariants surrounding multithreaded use. 569 570 571 Function Comments ~ 572 573 Declaration comments describe use of the function; comments at the definition 574 of 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 628 Variable Comments ~ 629 630 In general the actual name of the variable should be descriptive enough to 631 give a good idea of what the variable is used for. In certain cases, more 632 comments 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 644 Implementation Comments ~ 645 646 In your implementation you should have comments in tricky, non-obvious, 647 interesting, 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 720 Punctuation, Spelling and Grammar ~ 721 722 Pay attention to punctuation, spelling, and grammar; it is easier to read 723 well-written comments than badly written ones. 724 725 Comments should be as readable as narrative text, with proper capitalization 726 and punctuation. In many cases, complete sentences are more readable than 727 sentence fragments. Shorter comments, such as comments at the end of a line of 728 code, can sometimes be less formal, but you should be consistent with your 729 style. 730 731 Although it can be frustrating to have a code reviewer point out that you are 732 using a comma when you should be using a semicolon, it is very important that 733 source code maintain a high level of clarity and readability. Proper 734 punctuation, spelling, and grammar help with that goal. 735 736 737 TODO Comments ~ 738 739 Use `TODO` comments for code that is temporary, a short-term solution, or 740 good-enough but not perfect. 741 742 `TODO`s should include the string `TODO` in all caps, followed by the name, 743 email address, or other identifier of the person who can best provide context 744 about the problem referenced by the `TODO`. The main purpose is to have a 745 consistent `TODO` format that can be searched to find the person who can 746 provide more details upon request. A `TODO` is not a commitment that the 747 person referenced will fix the problem. Thus when you create a `TODO`, it is 748 almost 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 753 If your `TODO` is of the form "At a future date do something" make sure that 754 you either include a very specific date ("Fix by November 2005") or a very 755 specific event ("Remove this code when all clients can handle XML 756 responses."). 757 758 759 Deprecation Comments ~ 760 761 Mark deprecated interface points with `@deprecated` docstring token. 762 763 You can mark an interface as deprecated by writing a comment containing the 764 word `@deprecated` in all caps. The comment goes either before the declaration 765 of the interface or on the same line as the declaration. 766 767 After `@deprecated`, write your name, email, or other identifier in 768 parentheses. 769 770 A deprecation comment must include simple, clear directions for people to fix 771 their callsites. In C, you can implement a deprecated function as an inline 772 function that calls the new interface point. 773 774 Marking an interface point `DEPRECATED` will not magically cause any callsites 775 to change. If you want people to actually stop using the deprecated facility, 776 you will have to fix the callsites yourself or recruit a crew to help you. 777 778 New code should not contain calls to deprecated interface points. Use the new 779 interface point instead. If you cannot understand the directions, find the 780 person who created the deprecation and ask them for help using the new 781 interface point. 782 783 784 ============================================================================== 785 Formatting *dev-style-format* 786 787 Coding style and formatting are pretty arbitrary, but a project is much easier 788 to follow if everyone uses the same style. Individuals may not agree with 789 every aspect of the formatting rules, and some of the rules may take some 790 getting used to, but it is important that all project contributors follow the 791 style rules so that they can all read and understand everyone's code easily. 792 793 794 Non-ASCII Characters ~ 795 796 Non-ASCII characters should be rare, and must use UTF-8 formatting. 797 798 You shouldn't hard-code user-facing text in source (OR SHOULD YOU?), even 799 English, so use of non-ASCII characters should be rare. However, in certain 800 cases it is appropriate to include such words in your code. For example, if 801 your code parses data files from foreign sources, it may be appropriate to 802 hard-code the non-ASCII string(s) used in those data files as delimiters. More 803 commonly, unittest code (which does not need to be localized) might contain 804 non-ASCII strings. In such cases, you should use UTF-8, since that is an 805 encoding understood by most tools able to handle more than just ASCII. 806 807 Hex encoding is also OK, and encouraged where it enhances readability — for 808 example, `"\uFEFF"`, is the Unicode zero-width no-break space character, which 809 would be invisible if included in the source as straight UTF-8. 810 811 812 Braced Initializer Lists ~ 813 814 Format a braced list exactly like you would format a function call in its 815 place but with one space after the `{` and one space before the `}` 816 817 If the braced list follows a name (e.g. a type or variable name), format as if 818 the `{}` were the parentheses of a function call with that name. If there is 819 no 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 829 Loops and Switch Statements ~ 830 831 Annotate non-trivial fall-through between cases. 832 833 If not conditional on an enumerated value, switch statements should always 834 have a `default` case (in the case of an enumerated value, the compiler will 835 warn you if any values are not handled). If the default case should never 836 execute, 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 849 Switch statements that are conditional on an enumerated value should not have 850 a `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 852 example, instead of: >c 853 854 case A: 855 ... 856 case B: 857 ... 858 case C: 859 ... 860 default: 861 ... 862 863 You 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 876 Certain compilers do not recognize an exhaustive enum switch statement as 877 exhaustive, which causes compiler warnings when there is a return statement in 878 every case of a switch statement, but no catch-all return statement. To fix 879 these spurious errors, you are advised to use `UNREACHABLE` after the switch 880 statement to explicitly tell the compiler that the switch statement always 881 returns 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 895 Return Values ~ 896 897 Do not needlessly surround the `return` expression with parentheses. 898 899 Use 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 909 Horizontal Whitespace ~ 910 911 Use 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 ============================================================================== 930 Parting Words 931 932 The style guide is intended to make the code more readable. If you think you 933 must violate its rules for the sake of clarity, do it! But please add a note 934 to your pull request explaining your reasoning. 935 936 937 vim:tw=78:ts=8:et:ft=help:norl: