Server as a function with Kotlin – http4k

Server as a function with Kotlin – http4k

Functional Programming, Java, Kotlin, Software Development, Tutorial, Web
Server as a function with Kotlin - http4k Have you ever heard about the concept of "Server as a Function"? The idea is that we write our server application based on just ordinary functions, which is based on a concept outlined in the paper Your Server as a Function written and published by Twitter/Marius Eriksen. In the Kotlin world, the most prominent implementation of this concept is http4k, which the maintainers describe as an "HTTP toolset written in Kotlin with a focus on creating simple, testable APIs". The best part about it is that http4k applications are just Kotlin functions that we can test straightforwardly. Take a look at this first example: First http4k server example val app: HttpHandler = { request: Request -> Response(OK).body(request.body) } val server = app.asServer(SunHttp(8000)).start()…
Read More
How Kotlin makes me a more productive software developer

How Kotlin makes me a more productive software developer

Functional Programming, Java, Kotlin, Software Development
How Kotlin makes me a more productive software developer I've been writing JVM code for more than seven years now, and I did so mainly using Java. This changed about two years ago when I picked up Kotlin. By now, I managed to drop the Java language more-or-less entirely in favor of Kotlin. I did this because I feel much more productive with the language. It lets me focus more on the business logic rather than forcing me to write boilerplate code over and over again. In this post, I tell you how Kotlin makes me a more productive developer. I certainly know Kotlin much better than I ever knew Java. FWIW, I had been certified as a Java expert by Oracle some years back. Still, Kotlin became my native…
Read More
From Java Builders to Kotlin DSLs

From Java Builders to Kotlin DSLs

Android, Java, Kotlin, Tutorial
From Java Builders to Kotlin DSLs Introduction DSLs - Domain Specific Languages - are an ever trending topic in Kotlin circles. They allow us to flex some of the most exciting language features while accomplishing more readable and maintainable solutions in our code. Today I'd like to show you how to implement a certain kind of DSL - we're going to be wrapping an existing Java Builder in Kotlin. No doubt you've come across the builder pattern in Java before, for example if you're an Android developer, you must've used an AlertDialog.Builder, an OkHttpClient.Builder, or a Retrofit.Builder at some point. Wrapping a builder like this is a good exercise in just pure DSL design. All you have to worry about is designing the API you provide with your wrapper, since…
Read More
Kotlin Nullability Features

Kotlin Nullability Features

Functional Programming, Java, Kotlin, Software Development, Teaching, Tutorial
Null-Safe Programming - The Kotlin Way Disclaimer: This ktor article was originally published in the Dzone Java Guide 2018, which can be downloaded here. In this article, we will review the problems that may be caused by null pointers and how to avoid them in Java. After that, the article demonstrates how Kotlin nullability features work and how they improve your code. As Java developers, we're very accustomed to NullPointerExceptions (NPE) that are thrown at the runtime of an application. This almost always happens unintentionally in consequence of a bug, which is based on unrecognized references to null. The null reference is often used to indicate absent values, which isn't obvious to the programmer in many cases. Although Java relies on strong static typing, it doesn't let you distinguish between…
Read More
Run Kotlin Scripts (kts) from regular Kotlin Programs

Run Kotlin Scripts (kts) from regular Kotlin Programs

Gradle, Java, Kotlin, Software Development
Run Kotlin Scripts from Kotlin Programs This article presents a way to run Kotlin scripts from Kotlin programs in order to leverage the power of DSLs. Kotlin can be used as a scripting language. Simply write top-level executable code inside a file with .kts extension and run it with the kotlinc as described in the documentation. That's also the format of Gradle build files that are used in combination with the Gradle Kotlin DSL like this gradle.build.kts. Gradle shows a fantastic example of a domain specific language that can be written standalone in .kts files to be read by the gradle tool later on. When we try to find a way to do the same with custom DSLs (Tutorial can be found here), we first need to know how to…
Read More
Publish Kotlin Library on Bintray using Gradle Kotlin DSL and Travis CI

Publish Kotlin Library on Bintray using Gradle Kotlin DSL and Travis CI

Gradle, Java, Kotlin, Software Development, Tutorial
Distribute a Library on Bintray using Gradle Kotlin DSL In my latest blog post, published a few weeks back, I informed about the usage of the Gradle Kotlin DSL and how it helps with describing build scripts. In another earlier post, I introduced a small library that can be utilized for simplifying the creation of TLS/SSL sockets using a custom Kotlin DSL: SeKurity. In this post, we'll investigate how such a library can be made available to others that actually want to make use of it inside other projects. Ultimately, it should be possible to list the SeKurity library as a simple dependency in a build script like Maven or Gradle. Since the library itself is already backed by Gradle, I'll show a way of publishing the resulting artifacts at…
Read More
Kotlin Features I miss most in Java – Kotlin vs Java

Kotlin Features I miss most in Java – Kotlin vs Java

Functional Programming, Java, Kotlin, Software Development
PLEASE find an updated version of this on my Medium: https://medium.com/@s1m0nw1/the-7-features-i-miss-most-when-going-back-to-java-after-spending-time-with-kotlin-2b3a35e0b13f. Let's write an article that covers "Kotlin vs Java" topics - I want to tell you which Kotlin features I miss most when going back to Java. My Life as a Java Dev Although I'm a big supporter of the Kotlin programming language, I still do a lot of Java programming on a daily basis for my employer. Since I'm aware of the great functionalities of Kotlin, I'm often struggling with Java as it has some "pitfalls", requires additional boilerplate and misses many features. In this post, I'd like to describe which Kotlin features I miss most when coding in Java. new and Semicolon Ever since I'm doing Kotlin, there are two things I always forget when coding in…
Read More
Create a DSL in Kotlin

Create a DSL in Kotlin

Functional Programming, Java, Kotlin, Software Development
Kotlin as a programming language provides some very powerful features, which allow the creation of custom internal Domain Specific Languages (DSL).  One of these features, I also wrote about on this blog, is called Function Literals with Receiver, others are the invoke convention or infix notation. In this article, I will show how to create a Kotlin DSL by introducing a library that exposes a DSL as its API. I've often been struggling with Java’s API when I had to set up SSL/TLS connections in scenarios where I e.g. needed to implement HTTPS communication. I always felt like wanting to write a little library that can support me with this task, hiding away all the difficulties and of course the boilerplate needed for it. (more…)
Read More
Kotlin Coroutines Guide – Concurrent Programming in Kotlin

Kotlin Coroutines Guide – Concurrent Programming in Kotlin

Functional Programming, Java, Kotlin
updated 10/29/2018 Introduction and Motivation In this article, you will learn about Kotlin Coroutines: What they are, what they look like, and how they work. The demonstrated code examples were tested with Kotlin 1.3.0 and kotlinx.coroutines 1.0.0. Kotlin coroutines are one of the "bigger features" as indicated by the following quote, taken from JetBrains' blog: We all know that blocking is bad under a high load, that polling is a no-go, and the world is becoming more and more push-based and asynchronous. Many languages (starting with C# in 2012) support asynchronous programming through dedicated language constructs such as async/await keywords. In Kotlin, we generalized this concept so that libraries can define their versions of such constructs, and async is not a keyword, but merely a function. This design allows the…
Read More
Kotlin Generics and Variance (Compared to Java)

Kotlin Generics and Variance (Compared to Java)

Java, Kotlin, Software Development
This article covers the concepts of Generics and Variance in Kotlin and compares it to Java. Kotlin Generics differ from Java's in how users can define how they behave in sub-typing relationships. As opposed to Java, Kotlin allows defining variance on declaration-site, whereas Java only knows use-site variance. Kotlin Generics - What is Variance? Many programming languages support the concept of sub-typing, which allows implementing hierarchies that represent relationships like "A cat IS-an animal".  In Java, we can either use the extends keyword to change/expand behavior of an existing class (inheritance) or use implements to provide implementations for an interface.  According to Liskov’s substitution principle, every instance of a class A can be substituted by instances of its subtype B. The word variance, often referred to in mathematics as well,…
Read More