Kotlin vs. Swift, Not So Different After AllFeatured
As someone with a degree in Math and Business, I have not always worked in tech (nor did I initially expect to). My first fulltime job was actually at a financial services company in Canada. Fast forward to after I finished my Master's, I jumpstarted my tech career in web development, then ultimately transitioned to iOS. My introduction to Android development didn't occur until a few years after that, while in my current role as a Software Engineer at Postmates.Even before I started my journey into Android, I already knew on a high level that Swift and Kotlin were very similar. But why would those similarities matter, as a Software Engineer or Developer or otherwise? Before I answer that, let's take a step back to first define what these languages are independently.Kotlin is an open source programming language developed by JetBrains. It runs on the Java Virtual Machine (JVM) and is 100% interoperable with Java. Swift, on the other hand, is an open source programming language developed by Apple. It uses the ObjectiveC runtime library which allows C, ObjectiveC, C++, and Swift code to run within one program.Wait... It sounds like we're talking about two different languages, but here I am claiming that they're actually similar. To see where I'm coming from, let's check out some examples.VariablesWe'll start with the basics: variables. For variables that are set once (also known as constants), we use the keyword `val` in Kotlin and `let` in Swift. Otherwise, we use `var` for mutable or changeable values.That was pretty easy, right?TypesWhat about types? Perhaps the simplest or most familiar one to understand is automatic type inference. In the first example below, the variable `automatic` will be determined to be a `String` type.The next example shows implicit type conversion. The variable is declared as a `Double`, but is not explicitly initialized as one.Of course, if there's implicit type conversion, then there must also be explicit type conversion. Here, we cast the type of the number 21 to be a `String`.Finally, both Kotlin and Swift allow for optional types, meaning variables can be null or some nonnull value.String InterpolationString interpolation or variable substitution is a way to inject values into a format string. The final result in the example below is, of course, "Hello, World!"CollectionsFor Collections, we'll take a look at maps first. Like their ancestral languages, it is possible to initialize maps on the fly in Kotlin and Swift. In the following example, we're mapping letters to their corresponding number.A map is only one example of a collection, of course. Other collections to explore are arrays, lists, and sets.FunctionsPutting together some of the concepts we talked about, we can write functions a group of related statements that perform a specific task. Here we have a `who` argument and we use string interpolation to create a result string. Calling the function yields "Hello, World!".Perhaps the previous examples were a bit too simple or boring for you, so let's look at something a bit more complicated.Unwrapping OptionalsLet's say we have a constant, `world`, that has an optional string type. For safety, the compiler only allows us to use `world` after unwrapping it; this is done by using `let` with the Elvis operator ("?:" get it?) in Kotlin and `if let` in Swift. As you can see, `world` is not null, so we'll end up with "Hello, World!"Using the same example, we can see how the result changes when we have a null value instead. Now we'll end up getting "womp womp"LambdasUsing lambdas is a way to create small functions without names. In the following example, `map` is used on a list `sides` and applies a cube operation to each value in the list. The result is a new list of values, where each value of `sides` is cubed. Other lambdas you may want to explore are `filter` and `reduce`, which can be combined along with `map`.ConclusionsThat may have been a lot to take in, but there are some useful conclusions we can draw.When we compare Kotlin and Swift to their ancestor languages, Java and ObjectiveC:1. They're safer. Remember the unwrapping example? We're able to catch and fix more issues in compile time.2. They're less verbose. They have leaner syntax, making them more convenient and readable, particularly for developers newer to the languages.3. They both have functional programming support which is useful if you're familiar with other functional languages, such as Lisp or Haskell.4. They're still interoperable with their ancestor languages, so it's possible to use Java and Kotlin in the same code base, or ObjectiveC and Swift. This makes it easier to develop new projects or maintain older ones.Now that we've compared Kotlin and Swift to their ancestral languages, how do they compare to each other? Actually, that part is easy. Syntactically, they're close enough! You can see that in the examples above. Functionally, they're also close enough! Semantically, they share the same ideology since they solve the same problems created by their ancestorlanguages.So what does that mean for you? Well, there are benefits to learning and understanding bothlanguages or at least the ideology they come from.● It can improve your skills and experience, giving you a stronger understanding of programming concepts.● Based on that, it can also be easier to make the leap from iOS to Android, or vice versa.● It aids in native development, so it becomes faster and easier to develop, test, and maintain both clients.● It encourages cross team collaboration and app parity, yielding a potentially more efficient work environment.● As a bonus, iOS apps can be built using Kotlin, and Android apps can be built using Swift.Opening this up for discussion, what other benefits do you notice?Bianca is a Software Engineer at Postmates where she works on the iOS and Android apps. Although she is currently located in the Bay Area, she is originally from Canada and earned her B.Math. and M.Eng. degrees there. Her past work experience includes Fandango, Flixster/Rotten Tomatoes, and Sun Life Financial. Outside of work, Bianca is an active member of the tech community and particularly enjoys working with and mentoring students, early career professionals, and people newer to the tech space.