Reusing views code
In this section, we will learn a refactoring technique for SwiftUI views: extracting subviews.
You can structure your code any way you want, but if you allow your code to contain many duplicates for the same functionality, you will end up having to replace and correct multiple instances of the same fragments of code whenever you change anything. This is both boring and prone to error, and it involves having to work more in order to achieve little.
We want to show you an important “trick” that allows you to better structure your code. As you build larger and more intricate UI screens and apps, you will often notice that the same elements and groups of views will need to appear identical in several places inside the same app.
Of course, rewriting all of them from scratch every time and inside a content view would eventually create a giant, messy blob of difficult-to-understand code.
Let’s examine for instance a very simple view containing...