Skip to content

Commit 4c61db3

Browse files
committed
Update existing guideline to emphasize usage of "let" over "var"
- Update language to place more emphasis on using let - Add "Preferred" and "Not Preferred" examples so that the guideline stands out - Update a few other examples to use let instead of var
1 parent 909496b commit 4c61db3

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

README.markdown

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Swift types are all automatically namespaced by the module that contains them. A
9797
```swift
9898
import MyModule
9999

100-
var myClass = MyModule.MyClass()
100+
let myClass = MyModule.MyClass()
101101
```
102102

103103
You **should not** add prefixes to your Swift types.
@@ -338,9 +338,25 @@ In Sprite Kit code, use `CGFloat` if it makes the code more succinct by avoiding
338338

339339
### Constants
340340

341-
Constants are defined using the `let` keyword, and variables with the `var` keyword. Any value that **is** a constant **must** be defined appropriately, using the `let` keyword. As a result, you will likely find yourself using `let` far more than `var`.
341+
Constants are defined using the `let` keyword, and variables with the `var` keyword. Always use `let` instead of `var` if the value of the variable will not change.
342342

343-
**Tip:** One technique that might help meet this standard is to define everything as a constant and only change it to a variable when the compiler complains!
343+
**Tip:** A good technique is to define everything using `let` and only change it to `var` when the compiler complains!
344+
345+
**Preferred:**
346+
```swift
347+
let myClass = MyClass()
348+
let sectionCount = 5
349+
let request = NSMutableURLRequest(URL: myURL)
350+
request.HTTPMethod = "GET"
351+
```
352+
353+
**Not Preferred:**
354+
```swift
355+
var myClass = MyClass()
356+
var sectionCount = 5
357+
var request = NSMutableURLRequest(URL: myURL)
358+
request.HTTPMethod = "GET"
359+
```
344360

345361
### Optionals
346362

@@ -392,13 +408,13 @@ Use the native Swift struct initializers rather than the legacy CGGeometry const
392408
**Preferred:**
393409
```swift
394410
let bounds = CGRect(x: 40, y: 20, width: 120, height: 80)
395-
var centerPoint = CGPoint(x: 96, y: 42)
411+
let centerPoint = CGPoint(x: 96, y: 42)
396412
```
397413

398414
**Not Preferred:**
399415
```swift
400416
let bounds = CGRectMake(40, 20, 120, 80)
401-
var centerPoint = CGPointMake(96, 42)
417+
let centerPoint = CGPointMake(96, 42)
402418
```
403419

404420
Prefer the struct-scope constants `CGRect.infiniteRect`, `CGRect.nullRect`, etc. over global constants `CGRectInfinite`, `CGRectNull`, etc. For existing variables, you can use the shorter `.zeroRect`.
@@ -412,13 +428,13 @@ Prefer compact code and let the compiler infer the type for a constant or variab
412428
**Preferred:**
413429
```swift
414430
let message = "Click the button"
415-
var currentBounds = computeViewBounds()
431+
let currentBounds = computeViewBounds()
416432
```
417433

418434
**Not Preferred:**
419435
```swift
420436
let message: String = "Click the button"
421-
var currentBounds: CGRect = computeViewBounds()
437+
let currentBounds: CGRect = computeViewBounds()
422438
```
423439

424440
**NOTE**: Following this guideline means picking descriptive names is even more important than before.
@@ -482,12 +498,12 @@ The only exception to this rule is the `for-conditional-increment` construct, wh
482498

483499
**Preferred:**
484500
```swift
485-
var swift = "not a scripting language"
501+
let swift = "not a scripting language"
486502
```
487503

488504
**Not Preferred:**
489505
```swift
490-
var swift = "not a scripting language";
506+
let swift = "not a scripting language";
491507
```
492508

493509
**NOTE**: Swift is very different to JavaScript, where omitting semicolons is [generally considered unsafe](http://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript)
@@ -498,12 +514,12 @@ Use US English spelling to match Apple's API.
498514

499515
**Preferred:**
500516
```swift
501-
var color = "red"
517+
let color = "red"
502518
```
503519

504520
**Not Preferred:**
505521
```swift
506-
var colour = "red"
522+
let colour = "red"
507523
```
508524

509525
## Smiley Face

0 commit comments

Comments
 (0)