Swift Tips: Organization

Blake Merryman
Jul 24, 2017 · 3 min read

A well-organized project can save developers hours of frustration and lost productivity. There is no industry standard for organizing your project, but there are a few best practices that I like to follow.

Project Organization

When thinking about your project structure, group related ideas together. For example, you may want to keep all of your networking or Core Data code together. You could organize things based on the various architectural components, like views, view controllers, and data sources. It doesn’t really matter as long as there is a logic system in place. This makes it easy to onboard new developers and for existing developers to find things more efficiently.

Remember, files are (basically) free. Sometimes keeping roughly related classes in a file together can be convenient, especially during early prototyping, and Swift makes it so easy to do. However, in my experience, this raises the number of things you are thinking about when inside of a file and can lead to confusion and disorganization. Splitting large ideas (classes, structs, first order functions, etc.) into their own files helps lower the cognitive load when trying to understand what a file actually contains.

File Organization

Within a given file, there are a couple of handy tips for keeping things organized as well. Natasha The Robot’s great article Using Swift Extensions The “Wrong” Way covers my preferred method of organization.

For classes, structs, and enums, we can group related functionality together using Swift’s extension functionality. This is especially useful when conforming to protocols, as you get something that looks like this:

extension MyViewController: UICollectionViewDataSource {  func numberOfSections(in collectionView: UICollectionView) -> Int { /* … */ }  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { /* … */ }  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { /* … */ }// NOTE: Also keep other collection view data source helper 
// methods in here as well!
}

You can also use this system to group together private helpers or convenience methods. The end goal of organizing with extensions is to group large, logical chunks of code together in a way that stands out visually and improves readability.

To make these logical chunks even more noticeable, add // MARK: — <#Group_Of_Functionality#> above them so they appear in the jump bar.

Pro Tip: Use // MARK: — + <#ProtocolName#> to draw more attention to extensions used for protocol conformance. You can also drag these snippets into Xcode’s Snippet panel to make invoking them as simple as typing mark in your code.

Constants

Whether we’re building an API client or configuring a view controller, it’s good practice to keep important bits of configuration information as constants that are easy to find and change. However, constants can quickly pile up and pollute the namespace:

let APIConfigurationBaseURL = “…”
let APIConfigurationTimeout = …
let APIConfigurationEndptA = “…”
// etc. etc.

We can clean this up by repurposing Swift’s enum functionality to create our own namespace:

enum APIConfiguration {
static let baseURL = “…”
static let endPtA = “…”
static let timeout = …
// etc. etc.
}
// Invoked as:
let _ = APIConfiguration.baseURL

Note: A lot of people use structs for this, but a case-less enum has one key advantage: You can’t create instances of a case-less enum.

Getting in the habit of organizing your projects so that they are clean and consistent will make a world of difference to others collaborating on the projects. Are there project organization practices that you prefer? Tell us about them in the comments below or share them with us on Twitter.

Join us next Monday as we explore how Swift’s access control feature can help feed and hide information within your app.

For more insights on design and development, subscribe to BPXL Craft and follow Black Pixel on Twitter.

Black Pixel is a creative digital products agency. Learn more at blackpixel.com.

BPXL Craft

Design and technology articles from the Black Pixel team. blackpixel.com

Blake Merryman

Written by

 Software Engineer

BPXL Craft

Design and technology articles from the Black Pixel team. blackpixel.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade