In the beginning were the webmasters, who knew about PHP and JavaScript. Then front-end and back-end tribes, and each followed their own path, differentiating and specialising. Finally, new programming languages ​​and frameworks arrived, with the promise to write concise and expressive code, in safe mode.

Among the technologies that have emerged in recent years are, in particular, Kotlin and React. Developed internally by Jetbrains and Facebook respectively, released open-source and adopted by big companies all over the world, the two projects were born with similar purposes – offer a language / framework that allows to develop with more simplicity – and initially they were placed at the antipodes of the computer science rainbow. Kotlin, born to be an alternative to Java, wants to be an industrial-strength object-oriented language; React extends the JavaScript standard features to build user interfaces (within both browser and mobile OSs).

Two incompatible worlds? Not at all, two complementary worlds that, thanks to Kotlin’s multi-platform peculiarities, are co-existing as a practical and winning solution to write both (web-based) front-ends and back-ends using one language.

A valid alternative on server side

At Codemotion Milan 2018, Davide Cerbo has introduced the advantages of using Kotlin for the realization of full-stack projects, showing how effective, in particular, is the current Kotlin-React combination.

Kotlin is a modern language for modern multi-platform applications. It is characterised in particular by the concise syntax and to be easily adopted by those who already know Java (it is not by chance that it runs on the JVM), but at the same time to offer some interesting solutions to carry out more solid projects. An example is the distinction between nullable and non-nullable data types and the obligation to use “null-safe” operators for nullable objects.

It is a language particularly suited for server side programming. Due to its JVM-based nature, it allows gradually migrating large codebases from Java to Kotlin, relying on existing frameworks such as Spring or Java EE Http servlets.

Recently, however, it has added the ability of transpilling the Kotlin code into JavaScript, in particular client-side JavaScript.

Modern web with Kotlin and React

Kotlin’s attention to the JavaScript world and in particular client-side JavaScript on browsers, manifests itself on several fronts. For example, in the DSL to build HTML in the DOM and in its ability to generate JavaScript that is compatible with the Asynchronous Module Definition (AMD), the CommonJS and the Universal Model Definition (UMD).

Not only. It is also possible to include and extend existing JavaScript libraries as they were Kotlin classes.

For a modern web application you need a modern framework and React is the most obvious choice for the Kotlin world. There are several Kotlin wrappers to common JS and React libraries such as React itself, Mocha and Redux.

Of course, there are some subtle differences writing React code in Kotlin compared to JavaScript. For instance, being a strictly-typed language, type annotations for props are not optional.

Surely interesting, as highlighted by Davide Cerbo in his talk, is the possibility of “reusing” the same classes and methods in the front-end and in the back-end, with the certainty of having the same behaviour in both cases. Of course, the actual code will run, for instance, over JVM on server side and inside the browser’s JavaScript engine on client side.

Do not forget, however, that JavaScript code is transpilled from original Kotlin code and that, in some respects, Kotlin is very far from how JavaScript works. In particular, it should not be overlooked that, while Kotlin is a static typed language, JavaScript is dynamic-typed. This entails particular attention in using certain advanced constructs.

The path of Kotlin and React has just begun, but it’s already promising for multi-platform and full-stack development situations.

#kotlin ·
#react ·