Swift Anti-Patterns Overview
Swift Anti-Patterns Overview
Force Unwrapping Optionals
Force Unwrapping Optionals
!
can lead to runtime crashes. Use optional binding (if let
), nil coalescing (??
), or guard statements instead.Implicitly Unwrapped Optionals
Implicitly Unwrapped Optionals
var name: Type!
) should be avoided when possible. Use proper initialization or lazy properties instead.Massive View Controllers
Massive View Controllers
Not Using Swift's Type System
Not Using Swift's Type System
Overusing Singletons
Overusing Singletons
Not Using Result Type for Error Handling
Not Using Result Type for Error Handling
Result
type for clearer and more consistent error handling in asynchronous code.Not Using Swift's Access Control
Not Using Swift's Access Control
private
, fileprivate
, internal
, public
, open
) to hide implementation details and create clear APIs.Capturing Self Strongly in Closures
Capturing Self Strongly in Closures
[weak self]
or [unowned self]
in closures to avoid reference cycles and memory leaks.Not Using Swift's Collection Features
Not Using Swift's Collection Features
map
, filter
, reduce
, compactMap
, and flatMap
for cleaner, more expressive code.Using Force Try
Using Force Try
try!
as it can cause crashes. Use proper error handling with do-catch
blocks.Not Using Swift's Property Observers
Not Using Swift's Property Observers
willSet
and didSet
) to react to property changes automatically.Not Using Swift's Error Handling
Not Using Swift's Error Handling
throws
and do-catch
for error handling instead of returning nil or optional values.Not Using Swift's Value Types
Not Using Swift's Value Types
Not Using Swift's Lazy Properties
Not Using Swift's Lazy Properties
lazy
properties for expensive resources that might not be needed immediately or at all.Not Using Swift's Protocol Extensions
Not Using Swift's Protocol Extensions
Not Using Swift's Modern Concurrency
Not Using Swift's Modern Concurrency
Not Using Swift's Property Wrappers
Not Using Swift's Property Wrappers
Not Using Swift's Result Builders
Not Using Swift's Result Builders
ViewBuilder
) for declarative, composable code.