The Swift programming language took development for Apple’s MacOS and iOS by storm after being initially rolled out, with enhancements permitting the release of version 4.0 in September 2017. The benefits of Swift began finding their way to server-side applications soon after it entered the open source community. Programmers that consider using server-side Swift have lots to think about before making the decision.

“The promise of server-side Swift is that it has the speed and safety of Java with the expressiveness and flexibility of JavaScript,” said James Dickman, TDK Web & Mobile Team Lead. “Does it deliver on that? Maybe. It appears Swift may be able to provide this value. But it’s a new language used by new frameworks and is still evolving.”

What is Swift?

Swift was introduced in June of 2014 as Apple’s replacement for Objective-C, the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch. Dickman said many programmers find Objective-C to be a difficult language to use.

“Apple was aware that trying to develop an iOS application in Objective-C was pretty painstaking. Swift was really their initiative to make the iOS and Apple development platform more developer-friendly,” Dickman said.

Swift contains major changes from the older Objective-C approach and delivers a more modern programming language. Dickman said developers seem to be embracing Swift. Some of those improvements relative to Objective-C include:

Cleaner Syntax

  • Swift eliminates confusing smalltalk-like bracket and semicolon approach to methods and parameters.
  • Swift replaces dot notation, namespaces and other features found in common object-oriented languages like Java and C#.

Use of Patterns to Solve Recurring Software Development Problems

  • Delegation patterns use Swift protocols (blueprints of methods, properties, and other requirements that suit a particular task or piece of functionality).
  • Preferred to sub-classing, for greater composability, flexibility and extensibility.
    • A type which conforms to a protocol is guaranteed to implement that functionality.
  • Protocols can be used to respond to actions or retrieve data from an external source without needing to know anything about that source.
  • Using extensions adds new functionality to types.
    • You can even extend protocols, for example to provide a default implementation of a protocol method.
  • Types can be decorated with multiple protocols, allowing developers to describe what objects can do without the complexities of multiple inheritance or the pitfalls of massive base and abstract classes.

Swift is a multi-paradigm language that permits functional programming. But Dickman noted that since it is still growing, programmers may encounter obstacles if they are accustomed to features they like in other languages. For example, he said it’s more difficult to accomplish dependency injection and object-relational mapping when using Swift compared to what is possible in Java because Java-like annotations don’t exist (yet) in Swift.

“Like any language there are pitfalls and quirks. One of those we’ve encountered involves optional types. While these are integral to some of Swift’s most powerful and expressive features, developers can write dangerous code using optional types. For example, optionals can be ‘force unwrapped’ which allows the optional to be used without checking if it’s set. This is intended to reduce boilerplate code, such as when working with objects instantiated by Interface Builder like a UIButton, but it can easily be abused. But that’s no different than any other language, in that if you don’t follow best practices, you can find yourself in situations where errors are created,” Dickman said.

Open Source Helps Swift Grow – Become a Server-side Option

About a year after its introduction, Apple released Swift to the open-source community. Dickman said that move, in December 2015, helped accelerate its growth. Apple’s stated goal was to “make Swift the best general-purpose programming language available everywhere.”

“Instead of focusing just on the Apple platforms, they made a strategic decision to make it easier to be ported to other platforms. And very quickly after going open source, it was ported to Linux,” Dickman said. “That became the beginning of Swift running on servers, since Linux is the most widely used operating system in cloud environments.”

The open source approach brings in not just more developers to help contribute, but it can also bring in more companies. IBM, which partnered with Apple on enterprise mobility initiatives in 2014, came on board almost immediately after Swift went to the open source community and started developing products. A host of other companies have also contributed as well.

Initially Swift’s performance lagged that of Java. But it began improving after becoming an open source product and is roughly on par with Java. One benchmark where Swift seems to have a significant advantage is that it has a significantly lower Memory Footprint.

“If all you care about is performance, you are probably writing code in C or C++. But it’s safe to say that Swift is on par with Java. You are not gaining or sacrificing performance by switching between Swift or Java in typical web service or server-side applications,” Dickman said.

Most developers and businesses are probably more concerned with productivity and ease of using the language than with performance. That’s where Swift seems to have an advantage. Since 2015, Swift has ranked in top 4 in “most loved” programming languages in the Stack Overflow Developer Survey.

“Developers have rallied around Swift. They really enjoy using it,” Dickman said. “The open source community has done the same. There are now thousands of Swift-based libraries, plugins and frameworks that do all sorts of things.”

Server-side Framework Options

Several frameworks have emerged for using the Swift language in server applications. The four most prominent, in terms of the number of developers and companies that are using them, are described here. They are outlined with GitHub Star rankings (which indicate how many people have bookmarked the open source repository for easier access or to show appreciation for the work being performed) and Github Commits (the number of individual changes to a file or set of files by open source contributors).

Zewo

  • 1,712 Stars, 377 Commits
  • Modeled after the Go programming language

Perfect

Vapor

IBM Kitura

  • 5,983 stars, 966 commits
  • Only server-side Swift framework backed by major corporation.
  • Think Angular (Google) and React (Facebook).

“One of the things you have to ask with new technologies is ‘Are these frameworks enterprise-ready?’,” Dickman said. “You also need to think about which one is going to ‘win the war’ and build up enough support to survive long-term. It’s still early in the game.”

Framework performance when compared to Node.js, considered to be an equivalent technology stack, is one benchmark for making decisions. Here the frameworks are compared against a benchmark for retrieving information from data storage and serving it up as JavaScript Object Notation (JSON).

“This is one benchmark, but one that resembles a real-world situation. Both Perfect and Kitura are outperforming Node.js on this one,” Dickman said. “Developers need to weigh how much stock to put into performance compared to productivity."

One open-question for using Swift on the server-side is whether it can become a preferred choice for writing applications that can be hosted in the cloud for web services or to create a REST API. Dickman said server-side use is unlikely to become popular as rapidly as Swift itself, which had a relatively captive audience for open source developers in the iOS world.

“It will frankly have to forge its own path forward by being a better option,” Dickman said. “So, it will be interesting to see how many plug-ins and libraries that make development so much easier come into play on the server-side.”

Server-side Swift will also have to overcome a significant hurdle. Development currently can only take place on a Mac using Xcode and be deployed only on OS X or Linux. Arguably it will need to become more platform and IDE agnostic before Swift can start competing with other major server-side frameworks like Java, .Net and Node.

 

Sources referenced in this article:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID267

https://krakendev.io/blog/subclassing-can-suck-and-heres-why

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html#//apple_ref/doc/uid/TP40014097-CH24-ID151

https://medium.com/@rymcol/updated-benchmarks-for-the-top-server-side-swift-frameworks-vs-node-js-9da4a0491eca

https://softwareengineering.stackexchange.com/questions/339372/swift-like-extensions-in-java-using-inheritance

https://www.apple.com/newsroom/2014/07/15Apple-and-IBM-Forge-Global-Partnership-to-Transform-Enterprise-Mobility/

https://insights.stackoverflow.com/survey/2015#tech-super

https://insights.stackoverflow.com/survey/2016#technology-most-loved-dreaded-and-wanted

https://insights.stackoverflow.com/survey/2017#most-loved-dreaded-and-wanted

https://benchmarksgame.alioth.debian.org/u64q/swift.html

An IT pro who'll take the time to learn my business. Is that too much to ask?