Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
170: Code Review Checklist for Idiomatic Swift
By the time you're performing code reviews for a professional Swift project, you've likely mastered the syntax. But there is a massive gulf between code that compiles and code that is idiomatic. When I review a PR, I'm not just looking for bugs; I'm looking for "Swiftiness." I want to see if the developer is fighting the language or leaning into it. If I see code that looks like it was written in Java or C++ and then translated literally into Swift, that's where I start leaving comments.
Escaping the Pyramid of Doom
One of the first things I look for is the "Pyramid of Doom"—those deeply nested if let statements that push your actual logic halfway across the screen to the right. It happens because we're trying to be safe with optionals, but it makes the "happy path" of the function harder to find.
// The Naive Way: Deep Nesting
func processUserAvatar(user: User?) {
if let user = user {
if let profile = user.profile {
if let url = profile.avatarURL {
// Finally, the actual logic
downloadImage(from: url)
}
}
}
}
When I see this, I'll ask you to flip the logic using guard. The trade-off here is clarity versus verbosity. While guard requires an explicit return or throw, it allows the happy path to stay flush against the left margin. It turns the function into a series of requirements that must be met before the real work begins. I find this much easier to scan during a review because the edge cases are handled upfront and then forgotten.
// The Idiomatic Way: Guard Statements
func processUserAvatar(user: User?) {
guard let user = user,
let profile = user.profile,
let url = profile.avatarURL else {
return
}
downloadImage(from: url)
}
Filtering Noise without the Boilerplate
Another common pattern I see from developers transitioning to Swift is the manual for-in loop used for transformation or filtering. It's not "wrong," and in some complex cases, it's actually more performant, but for 90% of our daily work, it's just noise. It forces the reader to track the state of a mutable temporary array.
// The Naive Way: Imperative Filtering
var activeEmailAddresses: [String] = []
for user in users {
if let email = user.email, user.isActive {
activeEmailAddresses.append(email)
}
}
I prefer using functional chains like compactMap and filter. The beauty of compactMap is that it handles the optional unwrapping and the filtering of nils in one go. By chaining these, you describe what you want to happen rather than how to loop through the memory. It transforms the code from a set of instructions into a declaration of intent.
// The Idiomatic Way: Functional Chaining
let activeEmailAddresses = users
.filter { $0.isActive }
.compactMap { $0.email }
Value Semantics over Class Hierarchies
Finally, I keep a sharp eye on the use of class versus struct. A lot of engineers default to classes because that's what they're used to in OOP. They create complex inheritance trees for things that are essentially just data containers. This introduces unnecessary heap allocation and the headache of reference counting (ARC).
If you have a class UserProfile that just holds a name, an age, and an email, you're paying a performance tax for no reason. Unless you specifically need identity (where two objects are the "same" even if their values are identical) or you're working with a framework like UIKit that requires NSObject, use a struct. Value types are safer in multi-threaded environments because you're passing copies, not pointers. When I see a class that doesn't manage a shared state or a complex lifecycle, I'll almost always suggest a struct.
📋 Practical Task
Refactoring the Legacy Order Processor
You have been assigned to review a piece of legacy code in the OrderManager class. The current implementation is functional but is considered "non-idiomatic" Swift. Your task is to rewrite the summarizeOrder function to be more idiomatic.
Current Implementation:
class OrderItem {
var name: String?
var price: Double?
var isTaxable: Bool = false
init(name: String?, price: Double?, isTaxable: Bool) {
self.name = name
self.price = price
self.isTaxable = isTaxable
}
}
func summarizeOrder(items: [OrderItem]?) {
if let items = items {
var taxableTotal: Double = 0
for item in items {
if let price = item.price {
if item.isTaxable {
taxableTotal += price
}
}
}
print("Taxable total is \(taxableTotal)")
}
}
Your Requirements:
- Convert the
OrderItemfrom aclassto astruct. - Replace the nested
if letinsummarizeOrderwith aguardstatement. - Replace the
for-inloop and nestediflogic with a functional chain (usingfilter,compactMap, orreduce). - Ensure the final
taxableTotalis calculated in a concise, declarative manner.
There are no comments for now.