Never miss a beat

Join my newsletter.

Tag: kotlin

Adapting Traits for Kotlin

Published: 4/10/2019

Traits are a pattern introduced in Scala (at least, this was my first introduction to them) that help avoid deep class hierarchies in favor of composition. If you don’t have Scala experience, but do have Java experience, you can treat this similarly to Java 8 Interfaces . However, I still prefer to call them Traits. Although Traits have first-class support in Scala, Kotlin does not directly have Traits. However, Kotlin does support default interface implementations , just like Java 8. It’s…

Comparing Kotlin to Scala

Published: 3/5/2019

Kotlin to Scala. Scala to Kotlin. Two contenders for my heart. In the left corner, we have the new comer! Weighing in at just over 1.2MB — The Ambassador of Android: Kotlin! And in the right corner, we have the long-time favorite, weighing in at just over 5.3MB — The Archduke of Akka, The Sultan of Spark: Scala! If you’ve made it this far through my silliness, you’re likely comparing Kotlin to Scala for your next project. There’s definitely some key takeaways from the above paragraph that may…

Formatter Objects: Testable Fragments

Published: 1/24/2019

Let’s face it — Fragments are a pain to test and it’s predominantly due to how the Android operating system works. Sometimes, you’ll find yourself writing business rules regarding how a view is displayed. Likely, some data will come back from a web server and you’ll have to format some text differently or color something differently. Nothing crazy, but all this code adds up and needs to be tested. Ideally, you’ll have some form of automation coverage to help tackle this problem, but Espresso…

The Facade Pattern for Simple Dependency Injection

Published: 1/16/2019

I’ve been working on a Ktor api for a while now and found myself needing to make sure my routes had access to my repositories. Naturally, I sought out the advice of anyone who’s implemented Dagger with Ktor. After digging for a bit, I realized that it was completely unnecessary and likely overkill. Dagger works great on Android because you are tied to the constraints of the platform and lifecycles, but with Ktor I have control of the entire scope of my application. So I considered trying…

Controllers in Ktor

Published: 1/10/2019

Ktor is a microframework written in Kotlin. It’s focus is on building asynchronous servers and clients in connected systems. I’ve been using it predominantly for the “server” slice of that statement. I call Ktor a “microframework” simply because, while it’s opinionated, you have a lot of freedom in the way that you accomplish tasks within the framework. If you’d like to follow along, you can scaffold out a basic Ktor project via Intellij Idea or the online tool for generating a seed project…

Lambda-Map Performance in Kotlin

Published: 11/19/2018

Working on an enterprise application written in Kotlin, I’ve recently noticed something that felt “off” to me. Perhaps you’ve seen it, perhaps you’ve written it — maybe even recently! It looks something like this (I call this a Lambda-Map): Sure, this plusOne function isn’t really necessary. We can just inline it in the Lambda, but imagine for just a moment that this function does much, much more than just add one to a number. It then feels just to have that code pulled out into a private…

Introducing KotlinToday.com

Published: 11/13/2018

When I was learning Clojure for the first time, I had two references that I found myself reading every single day . The first resource is a fantastic guide by Daniel Higginbotham called Clojure for the Brave and True . The second resource that I found myself using allowed me to immerse myself into the Clojure community and figure out where people were focusing their efforts – Planet Clojure . Planet Clojure is a metablog that pulls in blog posts from a vetted list of Clojure developers. In…

Sequence: A Kotlin Type

Published: 11/3/2018

Kotlin, despite being an object oriented language, offers a ton of support for Functional paradigms. One of the most common functional paradigms involves using a combination of map , filter , and reduce on Iterable types. However, Kotlin uses extension functions on Java Collections to create these functions in their standard library. That means that when you call map on a list, you’ll return a new list. This is usually intended, however, it can become problematic when chaining multiple…

BDD Assertions with Expekt for Kotlin

Published: 10/26/2018

Behavior-Driven Development (BDD) has became quite a popular strategy for writing tests in the past ten or so years. BDD is a style of test-writing that focuses on test cases that flow like natural English sentences. Expekt is one such library for writing JUnit assertions in Kotlin. Combining JUnit and Expekt you can achieve results like this: Adding Expekt to Your Project As of writing, the current version of Expekt is 0.5.0 so that’s the one we’ll include in our project. If you’re using…

Kotlin Sealed Classes

Published: 10/19/2018

Kotlin has a concept known as sealed hierarchies (several other languages implement this feature as well). In Kotlin, we can generate a sealed hierarchy through the use of sealed classes. What are Sealed Classes? Sealed classes are basically an extension to an enum class. They offer similar functionality with a few key differences. Similarly: The class can not be instantiated directly The type can be represented as a type from a limited set, but not from any type outside of the set. Differences…

Static Methods, Companion Objects, and Testing

Published: 7/18/2018

A companion object sits inside a class definition and houses all the static methods which that class will own. Let's convert a piece of Java code to Kotlin so you can see what I mean.

Using ButterKnife with Kotlin

Published: 5/16/2018

Butterknife is a simple but fantastic tool for Android development. When this was introduced, it really simplified my thought process regarding view bindings in my activities, fragments, or view holders. As Kotlin adoption grew, Jake Wharton (creator of ButterKnife) also created KotterKnife , for kotlin-esque view bindings. KotterKnife isn’t bad, but I prefer the annotation syntax of ButterKnife instead! Implementing ButterKnife with Kotlin has a few gotcha’s however, that may be a bit…