OOP compared with other paradigms
Essence
A paradigm is a way to organize code and state. Swift is multi-paradigm, so the interview question usually becomes "when do you use which".
- Procedural programming: a sequence of steps over shared data; functions mutate state. Simple for scripts and algorithms, scales badly because of implicit dependencies through global state.
- OOP: state and behaviour are bundled into objects, interaction through messages, polymorphism through subtypes. Models entities with a lifecycle well (screens, connections) but tends towards deep hierarchies and shared mutable state.
- Functional (FP): pure functions, immutable data, composition; side effects pushed to the edges. Easy to test and parallelize. In Swift:
map/filter/reduce,let, value types, Combine. - Protocol-oriented (POP): Swift's own answer. Behaviour is described by protocols and their
extensions rather than base classes; data are value types; polymorphism comes from generics and protocols.
One task in three styles
let prices = [120.0, 80.0, 45.0]
// Procedural: step-by-step mutation
var sum = 0.0
for p in prices where p > 50 { sum += p }
// Functional: pure transformations, no mutation
let sum2 = prices.filter { $0 > 50 }.reduce(0, +)
// Protocol-oriented: behaviour attached to a protocol, works for any Sequence
extension Sequence where Element == Double {
func total(over threshold: Double) -> Double {
filter { $0 > threshold }.reduce(0, +)
}
}
print(sum, sum2, prices.total(over: 50)) // 200.0 200.0 200.0
How it shows up in an iOS project
- UIKit is classic OOP: the
UIResponder→UIViewhierarchy, delegates, subclasses. - SwiftUI is declarative and functional: a
Viewis a value type andbodyis a pure function of state. - Swift Concurrency brings actors: OOP objects with state isolation, i.e. OOP crossed with the FP idea of immutability (
Sendable).
What the interviewer looks for
- Understanding that paradigms are not mutually exclusive: the model is value types and pure functions, infrastructure is objects and protocols, UI is declarative.
- The main practical takeaway: minimize shared mutable state; it is what makes OOP code hard to test and to make concurrent.