Using Enum Separation for Better Swift Code Organization

Cope With a Large Enum

Steven Curtis
3 min readJul 18, 2024
A sample large enum

A large enum for handling various kinds of user actions seems like a great idea.

However large enums in Swift can be split up to enhance the structure and manageability of our code.

So let us look at the enum again:

enum UserAction {
case login(username: String, password: String)
case logout
case signUp(username: String, password: String, email: String)
case updateProfile(name: String, email: String, bio: String)
case deleteAccount
case follow(userID: Int)
case unfollow(userID: Int)
case post(message: String)
case deletePost(postID: Int)
case likePost(postID: Int)
case unlikePost(postID: Int)
case sendMessage(userID: Int, message: String)
case deleteMessage(messageID: Int)
}

Sure, these enum values are all UserAction and they are all indeed user actions.

However we should be able to split this up into smaller, more focussed enums.

Splitting up the enum

Each of the values from the enum seems like a relatively simple task.

We get smaller, more focused enums for different actions:

enum AuthAction {
case login(username…

--

--

Responses (2)