Skip to content

Commit 5f238fd

Browse files
brycebostwickcezarywojcik
authored andcommitted
Remove Container-owned Constants Rules (#21)
* Remove Container-owned Constants Rules Modifies a few rules to match our switch away from keeping class-level constants in an intermediate container. The rule changes are fairly simple; additionally changes the format of 2.5's code example (things were about to get a bit weird with both comments and marks), and completely removes rule 3.1.16 (this rule was specifically a justification for keeping constants in an `enum` over `class` or `struct`, but without using containers the rule doesn't have much use) * Update README.md Update recommended case back to `camelCase` for constants * Better formatting + update "updated" line * Header changes * Fix remaining singletons reference
1 parent f50fa03 commit 5f238fd

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Make sure to read [Apple's API Design Guidelines](https://swift.org/documentatio
44

55
Specifics from these guidelines + additional remarks are mentioned below.
66

7-
This guide was last updated for Swift 4.0 on January 23, 2018.
7+
This guide was last updated for Swift 4.0 on February 14, 2018.
88

99
## Table Of Contents
1010

@@ -172,7 +172,7 @@ if x == firstReallyReallyLongPredicateFunction()
172172

173173
* **2.2** Use `PascalCase` for type names (e.g. `struct`, `enum`, `class`, `typedef`, `associatedtype`, etc.).
174174

175-
* **2.3** Use `camelCase` (initial lowercase letter) for function, method, property, constant, variable, argument names, enum cases, etc.).
175+
* **2.3** Use `camelCase` (initial lowercase letter) for function, method, property, constant, variable, argument names, enum cases, etc.
176176

177177
* **2.4** When dealing with an acronym or other name that is usually written in all caps, actually use all caps in any names that use this in code. The exception is if this word is at the start of a name that needs to start with lowercase - in this case, use all lowercase for the acronym.
178178

@@ -187,27 +187,26 @@ class URLFinder {
187187
}
188188
```
189189

190-
* **2.5** All constants other than singletons that are instance-independent should be `static`. All such `static` constants should be placed in a container `enum` type as per rule **3.1.16**. The naming of this container should be singular (e.g. `Constant` and not `Constants`) and it should be named such that it is relatively obvious that it is a constant container. If this is not obvious, you can add a `Constant` suffix to the name. You should use these containers to group constants that have similar or the same prefixes, suffixes and/or use cases.
190+
* **2.5** All constants that are instance-independent should be `static`. All such `static` constants should be placed in a marked section of their `class`, `struct`, or `enum`. For classes with many constants, you should group constants that have similar or the same prefixes, suffixes and/or use cases.
191191

192192
```swift
193+
// PREFERRED
193194
class MyClassName {
194-
// PREFERRED
195-
enum Measurement {
196-
static let buttonPadding: CGFloat = 20.0
197-
}
198-
enum SillyMathConstant {
199-
static let indianaPi = 3
200-
}
195+
// MARK: - Constants
196+
static let buttonPadding: CGFloat = 20.0
197+
static let indianaPi = 3
201198
static let shared = MyClassName()
199+
}
202200

203-
// NOT PREFERRED
201+
// NOT PREFERRED
202+
class MyClassName {
203+
// Don't use `k`-prefix
204204
static let kButtonPadding: CGFloat = 20.0
205-
enum SillyMath {
205+
206+
// Don't namespace constants
207+
enum Constant {
206208
static let indianaPi = 3
207209
}
208-
enum Singleton {
209-
static let shared = MyClassName()
210-
}
211210
}
212211
```
213212

@@ -479,8 +478,6 @@ do {
479478

480479
* **3.1.15** If you have a function that takes no arguments, has no side effects, and returns some object or value, prefer using a computed property instead.
481480

482-
* **3.1.16** For the purpose of namespacing a set of `static` functions and/or `static` properties, prefer using a caseless `enum` over a `class` or a `struct`. This way, you don't have to add a `private init() { }` to the container.
483-
484481
### 3.2 Access Modifiers
485482

486483
* **3.2.1** Write the access modifier keyword first if it is needed.

0 commit comments

Comments
 (0)