In this article, we will look at comparing SwiftUI with some of the other ways to build applications for the Apple ecosystem. No more having to learn four different frameworks if you want to build an app that has components on Apple Watch, Apple TV, MacBook Pro, and finally your iPhone. Fancy. You might have even searched for what those terms actually mean. That is four different combinations. What if we now have three, four, or five switches? The structure and meaning are separated. With Android development, the common, imperative way is to use XML layout files to declare the UI components, then we mutate and manipulate them inside the Java/Kotlin activity/fragment/others classes. In this example, we see the @State attribute at line 2. It allows for your code to visually convey structure and meaning. Let us note that lines 18 and 22–27 are the most important for this post. Enter your email address to follow this blog and receive notifications of new posts by email. For example, a simple light switch can have two states, on or off. Yet again, where there is will there is away. However, since the rise of React, the mobile development is also slowly shifting its paradigm towards declarative style. Instead, it is built on top of providing and an additional layer of abstractions for us. In React, you only need to describe the final state of your UI in the render() method, without worrying about how to transition from previous UI state to the new UI state. Declarative style UI lightens the burdens on developers from manually defining the steps to construct and mutating the UI. With imperative style we will have something like this: One thing distinguishes the example above is that declarative UI typically doesn’t require you to assign a unique selector (or ID) to the UI component. This is called a two-way binding. The difference between what and how seems to be the simplest way to explain the declarative vs imperative paradigm. What if the user entered invalid information? From React Native, to Google’s Jetpack Compose, Flutter, and to Apple’s SwiftUI. I also have a personal blog here. A screenshot of the application follows. While good ole Bob Ross isn’t exactly commanding, he is giving them step by step directions to get the desired result. The difference between what and how seems to be the simplest way to explain the declarative vs imperative paradigm. However, this can become a problem as we have less control, but not everything can perfect. There’s no XML layout file anymore, and to me they’re so much easier to read. Declarative UI on the other hand, encourages you to construct the UI tree by building the UI blueprint first instead, where each UI component is immutable, and simply reacts to the change of its binding data model and re-rendered accordingly. Here is a quick code to use for comparison between declarative and imperative examples using UIKit for iOS and SwiftUI. What if we have two light switches and each of the combinations cause different actions depending on their state. It’s the how. It calculates factorial using recursion:But despite the high popularity level, we have to admit that imperative programming languages can be non-scalable and sometimes to… To explain further, we are not telling the components of a declarative UI when they should show or hide, we just tell them all the rules we want them to follow and it worries about following them. “In computer science, imperative programmingis a programming paradigmthat uses statementsthat change a program’s state. Conversely, the Java example has commands in it (create new toolbar, create new button, set the button label, and so on). SwiftUI was introduced in WWDC19 and instantly took the swift community by storm. The following definition is given in Wikipedia: In contrast to imperative programming, declarative programming, we can tell our applications what it should do and look like in different states and let itself handle moving in between those states. Change ), You are commenting using your Google account. In much the same way that the imperative moodin natural languagesexpresses commands, an imperative program c… If we bring these definitions back to UI development, what does it mean to declare/state a user interface, compared to commanding a user interface? Even though, it is a simple application it really allows us to see the benefits of both approaches. This matches this cool formula I found on Flutter’s “Start thinking declaratively” documentation: What’s the difference between declarative and imperative?