Hello,
â
This is one of the common questions about [SwiftUI](=). Let me try to explain the difference between declarative and imperative programming in this email.
â
Like Java, C++, PHP, and C#, Swift is an imperative programming language since its release. SwiftUI, however, is proudly claimed as a declarative UI framework that lets developers create UI in a declarative way. What does the term âdeclarativeâ mean? How does it differ from imperative programming? Most importantly, how does this change affect the way you code?
â
If you go up to Wikipedia and search for the terms, you will find these definitions:
In computer science, imperative programming is a programming paradigm that uses statements that change a programâs state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. In computer science, declarative programming is a programming paradigmâa style of building the structure and elements of computer programsâthat expresses the logic of a computation without describing its control flow.
â
Itâs pretty hard to understand the actual difference if you havenât studied Computer Science. Let me explain the difference this way.
â
Instead of focusing on programming, letâs talk about cooking a pizza (or any dishes you like). Assuming you ask your helper to prepare a pizza, you can either do it imperatively or declaratively. To cook the pizza imperatively, you tell your helper each of the instructions clearly like a recipe:
â
- Heat the over to 550°F or higher for at least 30 minutes
- Prepare one-pound of dough
- Roll out the dough to make a 10-inch circle
- Spoon the tomato sauce onto the center of the pizza and spread it out to the edges
- Place the toppings (including onions, sliced mushrooms, pepperoni, cooked sausage, cooked bacon, diced peppers) and cheese
- Bake the pizza for 5 minutes
â
On the other hand, if you cook it in a declarative way, you do not need to specify the step by step instructions but just describe how you would like the pizza cooked. Thick or thin crust? Pepperoni and bacon, or just a classic margherita with tomato sauce? 10-inch or 16-inch? The helper will figure out the rest and cook the pizza for you.
â
Thatâs the core difference between the term imperative and declarative. Now back to UI programming. Imperative UI programming requires developers to write detailed instructions to layout the UI and control its states. Conversely, declarative UI programming lets developers describe how the UI looks like and what you want to do when a state changes.
â
The declarative way of coding style would make the code more easier to read and understand. Most importantly, the SwiftUI framework allows you to write way less code to create a user interface. Say, for example, you are going to build a heart button in an app. This button should be positioned at the center of the screen and is able to detect touches. If a user taps the heart button, its color is changed from red to yellow. When a user taps and holds the heart, it scales up with an animation.
â
â
â
Take a look at the figure. Thatâs the code you need to implement the heart button. In around 20 lines of code, you create an interactive button with a scale animation. This is power of this declarative UI framework and SwiftUI.
â
If you are new to programming, you probably donât need to care the difference because everything is new to you. However, if you have some experience in Object-oriented programming or developed with UIKit before, this paradigm shift affects how you think about building user interface. You may need to unlearn some old concepts and relearn the new ones.
â
In the next email, I will show you how to get started with SwiftUI if you still haven't tried it out. Should you have any questions, please hit Reply and write me back.
â
Regards,
Simon
â
P.S. As a reminder, our special launch sale of our Mastering SwiftUI book ends on Oct 4. You can now use the discount code "swiftui2" at check to receive an extra discount. If you want to learn SwiftUI, [don't miss this opportunity to get the book](=).
â
â
â
â
â
â
â
â[Unsubscribe]( | [Update your profile]( | Unit 1104, 11/F Crawford House, 70 Queen's Road Central, Hong Kong, n/a n/a