|
| 1 | +## Coding Style Guide |
| 2 | + |
| 3 | +<!-- Keywords: edit, c, styleguide --> |
| 4 | + |
| 5 | +This document outlines the coding conventions. All contributions must adhere to |
| 6 | +these rules to maintain code consistency and readability. |
| 7 | + |
| 8 | +### 1. Formatting |
| 9 | + |
| 10 | +#### 1.1. Indentation |
| 11 | +- Use three (3) spaces for each level of indentation. Do not use tabs. |
| 12 | + |
| 13 | +#### 1.2. Bracing |
| 14 | +- Use the Allman style for braces. The opening and closing braces for a block |
| 15 | + are placed on their own lines and are vertically aligned. |
| 16 | + |
| 17 | + ```c |
| 18 | + if( condition) |
| 19 | + { |
| 20 | + ... |
| 21 | + } |
| 22 | + ``` |
| 23 | + |
| 24 | +#### 1.3. Spacing |
| 25 | +- **Control Structures & Function Calls:** No space between a |
| 26 | + keyword/function name and the opening parenthesis. A space follows the |
| 27 | + opening parenthesis. |
| 28 | + - `if( condition)` |
| 29 | + - `while( i < n)` |
| 30 | + - `[self doSomethingWith: arg]` |
| 31 | +- **Expressions:** No spaces immediately inside parentheses for grouping |
| 32 | + expressions. |
| 33 | + - `x = (a + b) * c;` |
| 34 | + |
| 35 | +#### 1.4. Columnar Alignment |
| 36 | +- Align similar elements vertically for readability, especially assignment |
| 37 | + operators. |
| 38 | + |
| 39 | + ```c |
| 40 | + char *a; |
| 41 | + int b; |
| 42 | + |
| 43 | + a = "foo"; |
| 44 | + b = 1848; |
| 45 | + ``` |
| 46 | + |
| 47 | +#### 1.5. `return` Statements |
| 48 | +- Parenthesize the return expression: `return( expr);`. |
| 49 | +- Do not use parentheses when there is no return value: `return;`. |
| 50 | + |
| 51 | +### 2. Declarations & Initialization |
| 52 | + |
| 53 | +#### 2.1. Variable Declaration |
| 54 | +- Declare all local variables at the top of the function block (C89 style). |
| 55 | +- Sort variable declarations alphabetically by name. |
| 56 | +- Declare one variable per line, except for trivial counters |
| 57 | + (e.g., `int i, n;`). |
| 58 | + |
| 59 | +#### 2.2. Initialization |
| 60 | +- Variables should not be initialized at declaration by default. |
| 61 | +- **Exceptions:** |
| 62 | + 1. When initializing from a function argument, often involving a cast. |
| 63 | + 2. When initializing an aggregate type (struct, array) to zero, which |
| 64 | + replaces a subsequent `memset(..., 0, ...)`. If an aggregate is |
| 65 | + initialized this way, the corresponding `memset` must be removed. |
| 66 | + |
| 67 | +### 3. Control Flow |
| 68 | + |
| 69 | +#### 3.1. `case` and `goto` Labels |
| 70 | +- `case` statements and `goto` labels are not indented relative to the |
| 71 | + block they are in. They align with the opening brace of the `switch` or |
| 72 | + function block. |
| 73 | + |
| 74 | + ```c |
| 75 | + switch( value) |
| 76 | + { |
| 77 | + case 0: |
| 78 | + ... |
| 79 | + break; |
| 80 | + |
| 81 | + default: |
| 82 | + ... |
| 83 | + break; |
| 84 | + } |
| 85 | + ``` |
| 86 | + |
| 87 | +#### 3.2. Single-Statement Blocks |
| 88 | +- Avoid single-line statements for blocks (e.g., `if(condition) statement;`). |
| 89 | +- Use braces for all blocks, except for single-line statements, when it has |
| 90 | + no continuation and its visual association with the control structure is |
| 91 | + unambiguous. |
| 92 | + |
| 93 | +## 4. Objective-C Specific |
| 94 | + |
| 95 | +### 4.1. General coding style |
| 96 | + |
| 97 | +- Do not use `-retain` or `copy` or `-release` or `autorelease` (exception: inside `-init` and `-dealloc` and "accessor" methods) |
| 98 | +- Do not use `+alloc` `-init` use `+instance` or an appropriate factory method like `-[NSMutableArray array]` |
| 99 | + |
| 100 | +### 4.2. Property Access |
| 101 | +- Do not use dot-syntax for property access. |
| 102 | +- Prefer non-mutable state in properties, prefer `(copy)` over `(retain)` when possible |
| 103 | +- Do not use strong use retain or copy instead |
| 104 | +- Do not use weak, use assign instead |
| 105 | +- Convert all property reads and writes to explicit message sends. |
| 106 | + - **Read:** `[self property]` instead of `self.property`. |
| 107 | + - **Write:** `[self setProperty:value]` instead of `self.property = value`. |
| 108 | + |
| 109 | + |
| 110 | +## Tip: |
| 111 | + |
| 112 | +NSString, NSArray and most other Foundation classes are not available |
| 113 | +with mulle-objc/objc-developer, you want foundation/objc-developer for that. |
0 commit comments