DevPrep

All topics › Object-oriented programming (OOP)

OOP compared with other paradigms

Junior

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".

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

What the interviewer looks for

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store