I quit using Swift on backend after only 2 weeks

Is Swift any good for backend?

Josip T
4 min readOct 27, 2023

After I got tired of endless gradle builds and heavy runtimes, I’ve started looking for a good compiled backend language. I was never an IOS developer nor did I wanted to become one and I knew absolutely nothing about Swift, the only reason I started learning it was because I saw this video from ThePrimeagen in which two things specifically pushed me into learning swift:

  1. Error handling —It seemed more practical
  2. Expressiveness — Since my primary language has been Kotlin witch is very expressive I thought it would be easier for me to learn swift than something more minimal like Go.

Frameworks

I did some research and found out swift actually has a pretty good backend framework (for simple backends) Vapor. After I familiarize myself with the language I started using Vapor. It’s great, except for when you want to do something they haven’t foresee. I wanted to configure the HTTP client (witch Vapor also provides) differently for every request. Long story short, it’s not possible, at least not without some refactoring witch I don’t want to do.

Enought about the framework, let’s talk about the language itself. For swift I would say it really feels the community wants to push it more than Apple is intersted in. It sucks. Like why I have to install Xcode on my Mac just to use swift, it’s three gigabytes, not all of us are swimming in a pool full of free storage especially not with prices Apple charges for an SSD upgrade. Alright, I deleted some node modules and installed Xcode, not a big deal.

Swift 5.9 Released | ThePrimeTimeagen

Learning swift wasn’t difficult for me. It’s mostly object oriented, but with less strict rules. It has it’s own package manager witch is pretty well thought out in my opinion. It’s also not boilerplate driven…

Error handling

The swift feature I like the most is error handling. If a function can throw it must be specified as such. A caller of a throwing function must handle the error in one of three ways: forward it to upstream function, handle it in a do-catch block or explicitly ignore it. It’s better that a standard try-catch, but I wish they’ve also made the compiler check what specific errors a function can throw, more like Zig. In general after all using this mechanism I started handling errors more individually instead of just saying “An error occurred. Please be prepared to spend the whole night debugging.” and I thing this way of error handling should be more common.

Concurrency model

Swift has introduced standard async/await keywords for managing concrrency just in version 5.5. I can’t understand how they haven’t thought of this earlier. In versions below 5.5, concurrency management is done through EventLoopFuture. Now we are stuck having some functions using EventLoopFuture and some async/await.

A good thing about swift concurrency are compilation time checks. The compiler check if a variable can be accessed by two separate threads at the same time and throws an error appropriately. But since swift is interoperable with Objective-C++ it doesn’t have its own lock implementation, instead it uses one from Obj-C resulting in variables accessed using locks still causing a compiler error. To get around this you can either mark the function as unsafe and make all race condition checks useless or you can use DispatchQueues and just get a warning, a warning that says: “this is an error in Swift 6", So good luck migrating.

Conclusion

Swift is definitely not a backend ready language, that’s not to say it won’t be in the future, but in it’s current state it’s still just a language developed by Apple for apple devices without much intentions to go any further. The community can do some stuff, but we need the language itself to become more polished before it can do serious backends.

I now know swift is not good for backend, but I don’t regret any minute spent learning it. It was just fun. I haven’t started seriously learning a new language for a while and if I haven’t tried it I wouldn’t every now what it’s like. So if you really want to use swift on your backend or you just have something simple to do, go ahead, learn it, it may have changed since I’ve written this. Don’t think too much, just start doing a project, you can always rewrite it. If your goal is to learn something, rewriting will be no problem, infact, that’s how I approached learning swift, I just rewrote an existing project.

--

--