CodingStyle.md 2.28 KB
Newer Older
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
# Coding Rules

This document defines Swift and Objective-C code style rules for this project.

## Table of Contents
* [Swift](#swift)
* [Objective-C](#objective-c)
# Objective-c code style

## Swift

The swift code style rules are defined by [this set of swiftlint rules](../.swiftlint.yml)

## Objective-C

Variable names should use capitalization on all word expect the first, and never '_' ie

**Right:**
```obj-c
BOOL isFirstTimeReading;
int age;
NSArray *myArray;
```

**Wrong:**
```objc-c
BOOL is_first_time_reading;
int Age;
int isFirstTimeReading_ever;
```

Objective-C Instance variables must be prefixed with _

**Right:**
```obj-c
@interface Object {
	  BOOL _isFirstTimeReading;
	  int _age;
}
```

**Wrong:**
```objc-c
@interface Object {
	  BOOL isFirstTimeReading;
	  int age;
}
```

Pointer * must be preceded with a space and with no space after ie

**Right:**
```obj-c
void *pointer;
```

**Wrong:**
```objc-c
void * pointer;
void* pointer;
void*pointer;
```

If, switch, and other keyword that are not function but takes parameter should have a space before ()

**Right:**
```obj-c
if (a) {
  ...
}
function();
```

**Wrong:**
```objc-c
if( a )
if ( a )
if(a)
function ();
```

Brackets usage

**Right:**
```obj-c
if (a) {
   ...
}
while (a) {
   ...
}
void function()
{
   ...
}
- (void)functionWithParameter:(BOOL)parameter
{
   ...
}
```

**Wrong:**
```objc-c
- (void)function {
   ...
}
if (a)
{
   ...
}
```

Prefer early return ie:

**Prefer:**
```obj-c
if (!a)
    return;
```

**Over:**
```objc-c
if (a) {
  ...
  ...
}
```

Objective C code - Don't call multiply the same method.

**Right:**
```obj-c
NSWindow *window = [self window];
NSRect frame = [window frame];
frame.origin.x = 0;
[window setFrame:frame display:NO];
```

**Wrong:**
```objc-c
NSRect frame = [[self window] frame];
frame.origin.x = 0;
[[self window] setFrame:frame display:NO];
objectAtIndex is gone - keep it like this
```

**Right:**
```obj-c
NSArray *array;
filled with lots of stuff
id object = array[index];
```

**Wrong:**
```objc-c
NSArray *array;
…filled with lots of stuff
id object = [array objectAtIndex:index];
NSArray literals improve readability - use them
```

**Right:**
```obj-c
NSArray *array = @[obj1, obj2, obj3];
```

**Wrong:**
```objc-c
NSArray *array = [NSArray arrayWithObjects: obj1, obj2, obj3, nil];
```