Make Your SwiftUI Code Clearer and More Organized with Section Dividers

Onur Uğur
3 min readMar 2, 2024

--

Have you ever found yourself getting totally lost while revisiting the code you wrote a while back? It’s pretty common to forget the logic, components, and architecture we used at the time when we revisit our past codes. But here’s a cool trick — by using Section Dividers, we can solve about 90% of that problem.

What are Section Dividers?

Imagine a well-written essay. It wouldn’t be a jumbled mess of words, right? Instead, it would be neatly divided into sections with clear headings, making it easier to understand and navigate. Section dividers in SwiftUI serve a similar purpose. They act as visual markers, grouping related functionalities and separating them from others. This makes your code more organized, easier to read at a glance, and ultimately, a joy to work with.

Benefits of Using Section Dividers

  • Enhanced Readability: Just like chapter breaks in a book, section dividers provide clear visual cues, making your code easier to scan and comprehend. It’s like having an outline for your code, allowing you to quickly grasp the overall structure and locate specific sections.
  • Improved Maintainability: When you revisit your code later, or when someone else needs to work on it, section dividers act as helpful guides. They make it much easier to identify specific areas and modifications needed, saving valuable time and effort.
  • Better Code Organization: Using section dividers encourages a more structured and organized coding style. This not only improves the immediate readability of your code but also promotes better long-term maintainability as your project grows.

How to use them?

To start using Section Dividers, we first need to enable the Minimap feature from the Adjust Editor Options button located at the top right corner within Xcode.

After turning it on, you’ll notice the minimap feature appearing on the right side of your code.

As a small tip, if you press the Command key while your mouse is over the minimap, you can view all the section dividers on the screen at the same time.

Here is the example code:

import SwiftUI

struct CounterExample: View {
// MARK: - Properties
@State private var count = 0

// MARK: - Body
var body: some View {
VStack(spacing: 20) {
headerView
mainContentView
incrementDecrementButtons
bottomView
}.padding()
}

// MARK: - View Components
private var headerView: some View {
Text("Simple Counter App")
.font(.largeTitle)
.padding()
}

As you can see, comments like // MARK: - Properties, // MARK: - Body, and // MARK: - View Components are used to mark different sections of the code. These comments are treated as section dividers by Xcode, creating a visual separation and making it easier to navigate the code structure using the sidebar navigation. So that you can easily navigate between your views with a mini-map by clicking on sections.

Remember:

  • Use descriptive section names that accurately reflect the code within that section. This adds clarity and makes it easier to understand the purpose of each section at a glance.
  • Maintain consistency in your naming conventions. This promotes better code readability and makes it easier for yourself and others to follow your coding style.
  • Consider using section dividers for larger areas of code, such as different views or functionalities within your app. This can further enhance the organization and maintainability of your project.

By incorporating section dividers into your SwiftUI coding practices, you’ll be well on your way to crafting cleaner, more organized, and ultimately, more enjoyable code.

--

--

Onur Uğur
Onur Uğur

Written by Onur Uğur

Unlock your potential with productivity and passive income tips. Discover the power of Notion and AI tools achieve financial freedom. Let's thrive together!

No responses yet