Introduction
When learning to code, you’ll often come across the terms declarative programming and imperative programming. These two styles of coding represent different ways of telling the computer what you want it to do.
In this article, we’ll break down both approaches in simple words, use real-world analogies, and show practical Swift and SwiftUI examples so you’ll never confuse them again.
What is Imperative Programming?
Imperative programming is like giving the computer a set of explicit instructions on how to do something step by step.
👉 Think of it like giving driving directions:
- “Turn left at the signal.”
- “Go straight for 2 km.”
- “Turn right near the park.”
You are focused on the procedure — how to get to the destination.
Example in Swift (Imperative):
var numbers = [1, 2, 3, 4, 5]
var doubled: [Int] = []
for number in numbers {
doubled.append(number * 2)
}
print(doubled) // [2, 4, 6, 8, 10]
Here, we’re telling Swift how to loop and how to append items.
What is Declarative Programming?
Declarative programming focuses on what you want to achieve, not how. You describe the end result, and the system figures out the steps.
👉 Think of it like using Google Maps:
- You only say “I want to go from A to B.”
- The app figures out the best path for you.
Example in Swift (Declarative):
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
print(doubled) // [2, 4, 6, 8, 10]
Here, you just declare that you want to double each number — Swift handles the details.
Example with UI Interaction: Counter App
This is where the difference really shines. Let’s say we want to update a counter when the user taps a button.
Imperative Approach (UIKit-style)
class ViewController: UIViewController {
var count = 0
let label = UILabel()
let button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
label.text = "Count: \(count)"
view.addSubview(label)
button.setTitle("Increase", for: .normal)
button.addTarget(self, action: #selector(increaseCount), for: .touchUpInside)
view.addSubview(button)
}
@objc func increaseCount() {
count += 1
label.text = "Count: \(count)" // manually update UI
}
}
👉 You explicitly:
- Create the label and button.
- Connect the button tap to a function.
- Manually update the label’s text every time the count changes.
Declarative Approach (SwiftUI-style)
struct ContentView: View {
@State private var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
Button("Increase") {
count += 1
}
}
}
}
👉 Here:
@State
holds the value (@State
is a Property Wrapper; learn more about property wrappers here).- When the button is tapped,
count
updates. - The Text automatically updates — no manual UI code required.
✅ Key Difference:
- In imperative (UIKit), you manage UI updates step by step.
- In declarative (SwiftUI), you just declare that
Text
depends oncount
. SwiftUI re-renders automatically whencount
changes.
✅ Summary
- Imperative = You give step-by-step instructions (how).
- Declarative = You describe the result (what).
- Swift supports both — you can write imperative loops or declarative functional code.
- SwiftUI is a declarative framework where the UI updates automatically based on state.
By understanding both paradigms, you’ll become a more flexible and effective developer.