Daniel Bauke
Posts / Swift

What's wrong with Swift's enums?

Swift

TL;DR

It’s all about the choice between imperative conditions (enums just enforce them) and object-oriented design (here: protocols).

Enums are data. By introducing them, you say: I emit the data, and I need to process it in every place where it is used. I will handle endlessly expanding conditional checks every time a new kind of data comes along.

Protocols are behaviors. By introducing them, you say: I want it to do something, but I don’t care how. Once I define inputs and outputs, it’s up to the implementation to fulfill the contract.

So, think of it like choosing a parenting style: strict control versus independence. Supervising every single action will eventually exhaust you and probably won’t help your kid, while setting clear boundaries and trusting things to work themselves out lets your system grow naturally. And much like raising a family, with each new project you will probably become a calmer, more relaxed parent who learns when to let go.

Conditional Statement Aversion

I suffer from self-discovered and self-diagnosed Conditional Statement Aversion.

It’s a serious condition, since it is triggered by any use of if-else or switch statements in the code. So, basically, if you want my attention in any code review, just include one, and I won’t be able to walk past it without trying to remove or simplify it.

Hence this post: I’m trying to ease my pain.

I know, it sounds silly, but it has deeper roots. It’s not just about conditionals, but about how to build object-oriented software correctly, which ties directly into the “Tell, Don’t Ask” rule.

So, let’s dive in! Let me tell you what is wrong with enums.

Enums

Enums are a valuable tool in Swift. They define a type with a finite set of values, which adds safety and clarity to our code. Swift also makes them very powerful: they can represent errors, carry associated data and even be riddled with their own functions.

That’s why the saying “just because you can doesn’t mean you should” matters so much with enums. They are powerful, but sometimes they are not the best choice.

Software keeps changing, and enums can quietly couple unrelated concepts. As requirements evolve, the dependencies they create can be hard to untangle.

Enums can be useful early in a design, because they help us understand the problem and make it work. But they can become a bottleneck as the codebase grows. They’re great for building something quickly, but as the software scales and grows more complex, their limits show, and they can even cause unexpected breaking changes.

Protocols

In Swift, protocols act as blueprints that define what a conforming type can do. Rather than holding concrete data, a protocol outlines requirements that any adopting type must fulfill: methods, properties and initializers, plus any requirements inherited from other protocols. Unlike enums, they don’t contain a closed set of values. They provide a contract for a behavior, and it doesn’t matter how that behavior is implemented.

Let’s look at an example. Suppose we’re building an app with different routes between screens, and some of them can also be shared as links. We may be tempted to use an enum to represent these routes:

enum AppRoute {
    case home
    case profile(userId: Int)
    case settings(section: String?)

    var shareURL: URL? {
        switch self {
        case .profile(let userId):
            URL(string: "https://example.com/users/\(userId)")
        case .home, .settings:
            nil
        }
    }
}

func navigateTo(route: AppRoute) {
    switch route {
    case .home:
        // Pop to the root screen
    case .profile(let userId):
        // Push the profile screen for userId
    case .settings(let section):
        // Present settings modally, scrolled to section if given
    }
}

func share(route: AppRoute) {
    guard let url = route.shareURL else { return }
    // Present the share sheet with url
}

This approach works fine until we have a new screen that takes additional parameters. Every time we add a route or change its parameters, we have to revisit both navigateTo(route:) and shareURL. Plus, it couples unrelated routes together, creating dependencies that can make our code harder to manage. Every case also has to answer every question: home and settings can’t be shared, so shareURL becomes optional and share(route:) has to check it at runtime.

With protocols, we can make our routes more flexible and decoupled:

protocol AppRoutable {
    func navigate()
}

protocol Shareable {
    var shareURL: URL { get }
}

struct HomeRoute: AppRoutable {
    func navigate() {
        // Pop to the root screen
    }
}

struct ProfileRoute: AppRoutable, Shareable {
    let userId: Int

    var shareURL: URL {
        URL(string: "https://example.com/users/\(userId)")!
    }

    func navigate() {
        // Push the profile screen for userId
    }
}

struct SettingsRoute: AppRoutable {
    let section: String?

    func navigate() {
        // Present settings modally, scrolled to section if given
    }
}

func navigateTo(route: any AppRoutable) {
    route.navigate()
}

func share(item: any Shareable) {
    // Present the share sheet with item.shareURL
}

Now, each route is a struct that conforms to the AppRoutable protocol. AppRoutable does one thing: it navigates. It knows nothing about sharing. Sharing lives in a separate, small Shareable protocol, adopted only by routes that can actually be shared. That’s the Interface Segregation Principle: HomeRoute and SettingsRoute don’t have to pretend they have a URL, and share(item:) accepts only things that can be shared. The compiler now checks what the enum version had to check at runtime.

Each route also carries only what it needs. HomeRoute has no properties at all, ProfileRoute has a userId and a share link, and SettingsRoute has an optional section. Each route is responsible only for its own screen, so changing how settings open touches SettingsRoute and nothing else.

To add a route, we just create a new struct that conforms to AppRoutable. The navigateTo(route:) function doesn’t need to change because it operates on the AppRoutable protocol, not a specific enum. This approach decouples the routes and provides flexibility.

Imagine adding a new route, SearchRoute, that requires a query parameter and can be shared as well:

struct SearchRoute: AppRoutable, Shareable {
    let query: String

    var shareURL: URL {
        URL(string: "https://example.com/search")!
            .appending(queryItems: [URLQueryItem(name: "q", value: trimmedQuery)])
    }

    func navigate() {
        // Push the search results for trimmedQuery
    }

    private var trimmedQuery: String {
        query.trimmingCharacters(in: .whitespacesAndNewlines)
    }
}

let searchRoute = SearchRoute(query: "Swift Protocols")
navigateTo(route: searchRoute)
share(item: searchRoute)

In this case, we’ve simply created a new struct, SearchRoute, that conforms to AppRoutable and Shareable. It even has its own private helper, trimmedQuery, that no other route or caller needs to know about. Neither navigateTo(route:) nor share(item:) needs to change. With the enum, the same addition means a new case and an update to every switch: navigateTo(route:), shareURL, and any other one added since.

Errors as enums

Errors as enums with internal conditionals are deeply baked into everyday Swift practice. So challenging that paradigm is like tilting at windmills, but I will do it anyway.

Let’s use this snippet for the rest of the discussion:

enum AppError: Error, CustomStringConvertible {
    case fileNotFound(name: String, path: String)
    case invalidData(url: URL)
    case decodingFailed(error: Error)

    var description: String {
        switch self {
        case .fileNotFound(let name, let path):
            "File `\(name).json` could not be found at path: \(path)"
        case .invalidData(let url):
            "Failed to load data from URL: \(url)"
        case .decodingFailed(let error):
            "Failed to decode JSON: \(error.localizedDescription)"
        }
    }
}

This small piece of code uses conditions to unpack hidden rules from a type with a closed set of variants, and it breaks several SOLID and OOP principles.

  1. Single Responsibility Principle. You define one type with a closed set of variants and use it in different, potentially unrelated places, so this one type couples them all together for no good reason.
  2. Open-Closed Principle. There are actually three independent issues here, but they are all related to OCP:
    • When you need to add another error (variant/case), you can’t add it freely as an independent one somewhere in the code. You have to change the existing enum. And what if someone from outside the team, or even the organization, wants to do it? Not a chance! Enums are not extensible by design.
    • When you need to handle a property other than description, you have to add another switch statement and implement it for every case (or cheat with a default: case). This duplication piles up quickly.
    • Dedicated types (structs) are much easier to extend when you need more than one extra attribute or some specific logic (for example, FileOperation in FileNotFoundError below).
  3. Tell, Don’t Ask. It is a core OOP principle. You should tell the object to perform an action rather than ask about its state and then decide what to do (in this case, querying the data to get the description).

There’s also a good discussion on the Swift Forums about it, which comes down to these points:

  1. Adding a new case is a source-breaking change for clients using exhaustive switching
  2. Adding associated values to an existing case is an API-breaking change
  3. Adding extra fields is cumbersome compared to structs

The usual answer to the first point is @unknown default. It softens the break: a missing case becomes a warning instead of an error. But the code still compiles, and at runtime the new case falls into a catch-all branch that knows nothing about it. You trade a compile error for a silent fallback, and every switch still needs a fix once someone reads the warnings.

Errors as structs

Here’s the same set of errors, split into one struct per error behind a shared protocol:

protocol AppError: Error, CustomStringConvertible {}

struct InvalidDataError: AppError {
    let url: URL

    var description: String {
        "Failed to load data from URL: \(url)"
    }
}

struct DecodingFailedError: AppError {
    let error: Error

    var description: String {
        "Failed to decode JSON: \(error.localizedDescription)"
    }
}

struct FileNotFoundError: AppError {
    struct FileOperation: OptionSet {
        let rawValue: Int

        static let read = FileOperation(rawValue: 1 << 0)
        static let write = FileOperation(rawValue: 1 << 1)
        static let delete = FileOperation(rawValue: 1 << 2)
    }

    let name: String
    let path: String
    var operations: FileOperation = .read

    var description: String {
        "File `\(name).json` could not be found at path: \(path) during \(operations)"
    }
}

Limitations in errors as structs

One limitation of typed throws in Swift 6: a function can name only one error type. throws(FileNotFoundError) works fine, but you can’t write throws(any AppError). A function that can fail in several ways falls back to plain throws, and the caller has to cast the error back with as? any AppError. An enum doesn’t have this problem, since all its cases share one type.

But where typed throws really helps is generic code. A function that takes a closure can declare throws(E) and pass on the closure’s error with its exact type, the same way Result<Success, Failure> does. That works equally well with enums and structs.

Where do enums shine?

Enums have their pitfalls, but they’re not inherently bad. They excel at representing a well-defined set of related values, ideally non-public and on a small scale. They are very useful for representing a fixed set of options, such as API responses:

enum APIResponse {
    case success(data: Data)
    case failure(error: Error)
}

They can also be handy for tasks like filtering data:

enum FilterOption {
    case date
    case name
    case location
}

func filter(by option: FilterOption) -> APIResponse {
  let endpoint = ProductsEndpoint(filter: option)
  return apiClient.list(from: endpoint)
}

In these scenarios, enums provide a closed set of values that makes sense for the use case and adds clarity and safety to the code.

Conclusion

Enums turn your code into a helicopter parent. Every call site needs a switch that asks the value “which case are you?” before it can do anything. The logic for a single case ends up scattered across navigateTo(route:), shareURL, description, and every other switch in the codebase. Add a case, and each of those switches has to change. If the enum is public, that’s a source-breaking change for every client.

Protocols let you step back. The protocol defines the contract, each conforming type implements it, and the caller just calls route.navigate() without knowing which type it holds. A new route is a new struct, and no existing code has to change. That’s the Open-Closed Principle at work. Small protocols like Shareable also move checks from runtime to compile time: there’s no optional shareURL to unwrap, because only types that can be shared conform to it.

Enums still have their place, the same way a toddler next to a busy road still needs a hand to hold. Take success and failure: a textbook closed set, where an exhaustive switch is exactly what you want. Except it almost never stays at two. Soon someone needs .cancelled, because a user leaving the screen shouldn’t trigger an error alert. Then comes .notModified for a cached response, and .unauthorized to send the user back to login. Every new case lands in every switch, and every switch grows its own branch of logic. The toddler grows up, and holding hands stops working.

Keep the enum only when the set is truly closed and will stay that way. Everywhere else, stop asking and start telling.

References