Member-only story
Imperative vs. Declarative Swift Programming
Let the worlds collide!
Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 11.4.1, and Swift 5.2.2
Prerequisites:
- Coding in Swift Playgrounds (guide HERE)
- Using SwiftUI in Playgrounds (guide HERE)
- Map is used, as are Arrays and for loops
Terminology
Declarative programming: a programming paradigm that uses statements that change a program’s state
Imperative programming: a programming paradigm that expresses the logic of a computation without describing the program flow
The Swift example
In Swift you can generalize (American spelling, sorry mum) that you can code either in an Imperative
or a Declarative
style.
Let us see a rather simplistic example, in the abstract then using a concrete example, and then in rather more concrete code.
So Imperative
can be thought of as telling the machine how to do something step-by-step, and revealing the result. In contrast, a Declarative
style opens the door to using a declarative abstraction to produce a result.