Kotlin and Swift. Is it a whole new era in Mobile Development?

Andrew Cherkashyn
codeburst
Published in
4 min readMay 22, 2017

--

When Google finally announced that they will officially use Kotlin for Android Development — me, as many other Android Developers, felt great relief. I revisited the official Kotlin site to recheck features/syntax and compare it to the latest Swift version I use for development, and suddenly felt this strange feeling — the moment when one era is passing by and new era begins, for mobile development at least…

Kotlin, as well as a Swift brings many new nice syntax structures as well as syntax sugar to do regular code routine smaller and shorter (syntax comparison table: http://nilhcem.com/swift-is-like-kotlin/). But what also makes me happy here — that they both brings out-of-the-box support for new programming paradigms, especially functional programming.

Functional programming principles indeed are not a new thing in software development, even quite old already. But now that it’s officially supported out-of-the-box in development for both iOS and Android — rises the bar even further.

When I was only starting my career as a mobile software engineer, everybody was writing loops like this:

Java:

String[] mixedArray = new String[] { "4", "5", "a", "-2", "Str" };
int results = 0;
for (String element : mixedArray) {
results += Integer.parseInt(element);
}

Now everybody use functional approach to do the same things in one call, and it is a much better approach:

Kotlin:

val mixedArray = arrayOf("4", "5", "a", "-2", "Str")
val results = mixedArray
.filter { obj -> obj.toIntOrNull() != null }
.map { x -> x.toInt() }
.reduce { acc, x -> acc + x }

Swift:

let mixedArray = ["4", "5", "a", "-2", "Str"]
let results = mixedArray
.filter({ (obj) -> Bool in return Int(obj) != nil })
.map { (obj) -> Int in return Int(obj)! }
.reduce(0, +)

Apple introduced Objective-C blocks in 2010 for iOS SDK 4.0, to improve developers experience and match Java’s Anonymous Classes that can be used as callback:

Objective-C block example:

void (^newBlock)(void) = ^{
NSLog(@"New block is called");
};

Java anonymous class example:

(new CallbackClass() {
@Override public void call() {
Log.i(StaticTag, "Callback is called");
}
});

Java’s Lambda expressions were introduces in 2014 as a part of JDK 8, but unfortunately it wasn’t available to Android Developers, because Android SDK supports only JDK version 7 (that’s why there are such libraries like retrolambda: https://github.com/evant/gradle-retrolambda to add this feature support to your project).

So now both languages have fully supported implementation of this approach: Swift uses closures (same as Objective-C blocks) and Kotlin now has lambdas support that works with Android SDK:

Swift closure example:

{ _ in
print("Closure is called!")
}

Kotlin lambda example:

{
println("lambda is called!")
}

Beginning from Xcode 4, somewhere in 2011 — Objective-C provides inline arrays and map(dictionary) initialization:

Swift inline initialization example:

let numbersArray = [2, 4, 1]
let dictionary = ["key1": "value1", "key2": "value2"]

In JDK there is only static/static-block initialization available, but no way to initialize Map inline. Map.of function that allows that was only introduced in JDK 9.

Java static initialization example:

// Array
private static final int[] numbersArray = new int[] {2, 4, 1};
// Map
private static final Map<String, String> map;
static
{
map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
}

But now Kotlin can do it:

mapOf<String, String>("key1" to "value1", "key2" to "value2")

One more thing I want to highlight too — Range operators, that makes your life much easier. So now instead of using for-loop to do simple enumeration:

for (int i = 0; i < N; i++) {
// Do something
}

You can do this in Kotlin:

for (i in 0..N-1) {
// Do something
}

Or this in Swift:

for i in 0..<N {
// Do Something
}

Also tuples gives a lot of freedom in defining components interaction, avoiding spawning of additional classes to define interfaces between components.

So combining all these new features and many many others things that left aside in this article — I would suggest that it’s a whole new era started already. All new developers coming into mobile development now will have all these features available out-of-the-box and will be able to reduce amount of routine code around business logic and application flow, which is more important than writing hundreds of lines to make a simple thing working. Of course before you could just install and setup some additional library like PromiseKit, ReactiveCocoa, RxJava and many other libraries. But I believe because these paradigms and principles now available from start — it will encourage new coming developers to use it, which moves us to a brighter and better future I believe :)

Thanks for reading! I hope you find it useful or at least it gives you some fresh ideas. Tried to keep it short, but if you want some more particular examples and/or have suggestions — Please let me know in comments below!

--

--

Passionate iOS/Android Software Engineer. Currently living in San Francisco