13. #1 USE DESCRIPTIVE NAMES
NAMES SHOULD BE…
▸ Intention-revealing: Why it exists, what it does, how it is
used
▸ Pronounceable: Easy to read, not cryptic, using standard
acronyms
▸ Consistent: Choose a single word for a domain concept
and stick with it
14. #1 USE DESCRIPTIVE NAMES
RENAMING IS CONSIDERED REFACTORING
▸ Finding the right name the first time is rare.
▸ Names can and should be revised as needed.
▸ Xcode and AppCode have some automated tools to help
with this process.
15. #1 USE DESCRIPTIVE NAMES
REFACTORING NAMES: XCODE
▸ ⌃ + ⌘ + E → Rename Variable
16. #1 USE DESCRIPTIVE NAMES
REFACTORING NAMES: APP CODE
▸ Shift + F6 → Rename (on pretty much anything)
17. #1 USE DESCRIPTIVE NAMES
func calculate(date1: Date, date2: Date) -> String {
var diff: TimeInterval = 0
if (date2 > date1) {
diff = date2.timeIntervalSince(date1)
} else {
return "No time remaining"
}
let s = Int(diff) % 60
let m = Int(diff / 60) % 60
let h = Int(diff / 3600) % 24
let d = Int(diff / 86400)
return "(d) days, (h) hours, (m) minutes, and (s) seconds remaining"
}
18. #1 USE DESCRIPTIVE NAMES
func calculate(date1: Date, date2: Date) -> String {
var diff: TimeInterval = 0
if (date2 > date1) {
diff = date2.timeIntervalSince(date1)
} else {
return "No time remaining"
}
let s = Int(diff) % 60
let m = Int(diff / 60) % 60
let h = Int(diff / 3600) % 24
let d = Int(diff / 86400)
return "(d) days, (h) hours, (m) minutes, and (s) seconds remaining"
}
let date1 = Date(timeIntervalSince1970: 1484360070) // Jan 14 2017 11:14:30 AM
let date2 = Date(timeIntervalSince1970: 1484557200) // Jan 16 2017 18:00:00 PM
calculate(date1: date1, date2: date2) // 2 days, 6 hours, 45 minutes, 30 seconds
calculate(date1: date2, date2: date1) // No time remaining
19. #1 USE DESCRIPTIVE NAMES
func formatRemainingTime(pastDate: Date, futureDate: Date) -> String {
var secondsDifference: TimeInterval = 0
if (pastDate < futureDate) {
secondsDifference = date2.timeIntervalSince(date1)
} else {
return "No time remaining"
}
let secondsRemaining = Int(secondsDifference) % 60
let minutesRemaining = Int(secondsDifference / 60) % 60
let hoursRemaining = Int(secondsDifference / 3600) % 24
let daysRemaining = Int(secondsDifference / 86400)
let formattedRemainingTime =
String(daysRemaining) + " days, " +
String(hoursRemaining) + " hours, " +
String(minutesRemaining) + " minutes, and " +
String(secondsRemaining) + " seconds remaining"
return formattedRemainingTime
}
20. #1 USE DESCRIPTIVE NAMES
func formatRemainingTime(pastDate: Date, futureDate: Date) -> String {
guard (futureDate > pastDate) else {
return "No time remaining"
}
let secondsDifference = futureDate.timeIntervalSince(pastDate)
let secondsRemaining = Int(secondsDifference) % 60
let minutesRemaining = Int(secondsDifference / 60) % 60
let hoursRemaining = Int(secondsDifference / 3600) % 24
let daysRemaining = Int(secondsDifference / 86400)
let formattedRemainingTime =
String(daysRemaining) + " days, " +
String(hoursRemaining) + " hours, " +
String(minutesRemaining) + " minutes, and " +
String(secondsRemaining) + " seconds remaining"
return formattedRemainingTime
}
22. #2 ORGANIZE YOUR OBJECTS
USE ‘MARK’ ANNOTATIONS FOR CODE ORGANIZATION
▸ // MARK: -
▸ Can be used for:
▸ Defining common parts of objects
▸ Protocol Conformance
▸ Private Methods
32. EVERY TIME YOU
WRITE A COMMENT,
YOU SHOULD
GRIMACE AND FEEL
THE FAILURE OF
YOUR ABILITY OF
EXPRESSION.
Uncle Bob
33. CREATE A NEW
METHOD INSTEAD
When you feel the need to write a comment…
#3 AVOID COMMENTS
34. A SIMPLE COMPARISON
OR
// Check to see if the employee is eligible for full benefits
if (employee.isHourly && employee.age > 65) {
}
if (employee.isEligibleForFullBenefits()) {
}
#3 AVOID COMMENTS
35. HOW ABOUT DATES?
// Jan 14 2017 11:14:30 AM
let date1 = Date(timeIntervalSince1970: 1484360070)
let Jan_14_2017_11_14_30_AM = Date(timeIntervalSince1970: 1484360070)
OR
#3 AVOID COMMENTS
36. IS THERE SUCH THING AS A GOOD REASON FOR COMMENTS?
▸ Documentation for frameworks or libraries.
▸ Clarification outside of your control (e.g. library usage).
▸ Warning of consequences.
▸ See ‘Clean Code’ for a few more examples.
#3 AVOID COMMENTS
37. WRITING CLEAN CODE IN SWIFT
SUMMARY
1. Use Descriptive Names
2. Organize Your Objects
3. Avoid Comments