In this section, you'll learn what declarative syntax is and what immediate benefit it offers for writing clean code. You'll also learn about its counterpart imperative syntax in order to gain understanding from both paradigms.
The declarative syntax is a programming paradigm that allows you to write code in a more formal and procedural way. In essence, the declarative syntax is a way of describing the code you want to write, without having to worry about how it's going to be implemented.
The following is an example of declarative syntax if it was said in spoken language:
This is more of a statement than written logic as we are asking for something rather than being concerned about how we are going to get it.
Let's take our first look at SwitUI syntax. Here, we have created a Text Label with the value "Learn SwiftUI" and guess what... that's all we need to do:
Text("Learn SwiftUI")
As you may recall from Chapter 1, Getting Started with SwiftUI, and how we created Objects such as an instance of a UIButton or a UILabel, the example involves a lot less code; we don't need to jump through hoops to tell SwiftUI what we want to create – we simply ask for it.
Many languages already make use of declarative programming. A more commonly known language is the SQL syntax that's used in database queries and stored procedures.
The following is an example of a SQL stored procedure:
CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;
As you look at each line, you will see it's more of a statement than actual logic: you ask to CREATE PROCEDURE, without having to worry about how the creation is done, you SELECT * FROM Customers, without the need to get the Customer list, and even know how SELECT is performed for setup.
Now that we have a good idea of the concept behind declarative syntax and how it works as part of the programming paradigm, let's take a look at how we might use this in the real world by getting out project up and running in Xcode and seeing it for ourselves.