How to become Effective with Kotlin? Answers from Marcin Moskala

jmfayard

Jean-Michel 🕵🏻‍♂️ Fayard

Posted on February 7, 2020

How to become Effective with Kotlin? Answers from Marcin Moskala

Marcin Moskala is an experienced developer and Kotlin trainer.

He is an official JetBrains partner for Kotlin training, founder of Kt. Academy and main author on Kt. Academy technical blog . He is a speaker at tech conferences. I met him at KotlinConf 2018 where he had talked about Effective Multiplatform Kotlin Development. He is the author of the book Android Development with Kotlin and recently released Effective Kotlin. We share a common curiosity about everything releated to learning and mentoring, so I asked him to answer a few questions for you.

Who are you?

Marcin, can you first tell us a bit about you personally? Who are you as a person and what makes you tick?

This is an interesting and important question I have been asking myself lately. What I think truly makes me tick is learning. I love learning and I spend a lot of time on that - researching, reading, learning from flashcards, trying new approaches… It makes me feel great and it makes me think. As a result, I am having a lot of ideas and knowledge to share, and when I have them I feel I need to check ideas and share the knowledge. So I write, teach, speak, etc. It is clearly not the only mechanism that drives me, but in retrospection, I think that this one is quite strong and in big part led my last years.

On Training

I feel that most IT companies are under-investing in Training. Sure, there is an infinite amount of information on the internet, and given an infinite amount of time, a developer team can learn everything on its own. But Time is, in fact, our most precious resource. Can you talk about your experience working as a Kotlin trainer?

Yes, training can speed-up the learning process.

What I think is even more important is that it triggers the whole team to start using a new tool. So practically all the teams I trained reported to me that they started using Kotlin everywhere straight after my workshop.

Another thing is that it motivates us to learn things people often forget about. Too often people just copy-paste without understanding.

A simple example: Say that you use Gradle. Learning how it works might probably make your life easier in the long term. It is possible - there are some good fee Gradle courses and materials on the internet. But people generally assume they don’t have time for that. Although if they join a workshop, they have time dedicated for that and they can truly learn that in depth.

How to write a programming book that stays relevant?

You just released your second book: Effective Kotlin. I know some devil advocates that say that programming books are useless because their content gets quickly obsoleted. I think they are wrong for example for beginners’ books, those are really useful. But your book is aimed at experienced developers. What strategy did you choose to make sure your book stays relevant as Kotlin evolves?

I was actually thinking a lot about it as I observed that most Effective Java items are not relevant in Kotlin or could be solved with good warnings instead of developers remembering the best practices.

Though then I noticed that my favorite items are not Java-specific at all:

  • Favor composition over inheritance,
  • Design, and document for an inheritance, or else prohibit it,
  • Minimize the accessibility of classes and members,
  • Minimize mutability.

So I decided to describe more general concepts that are important in Kotlin.

In most items, I am explaining general timeless rules and showing how do they apply in Kotlin.

Those who finished the first chapter on Safety might recognize that I present the idea of explicitly stated preconditions and postconditions and that in Kotlin we express them using require, check and assert.

Or the idea of defensive and offensive programming and how they are applied in Kotlin to eliminate nullability.

It is especially true for chapters 3 and 4 which describe many high-level concepts.

There are some items that might change over time.

For instance, since function interfaces were introduced to Kotlin, we need to rethink as a community when should we use function types and where function interfaces instead.

Effective Kotlin

After you gave me an instance of "Effective Kotlin" at KotlinConf, five people stopped me to ask where they can grab their versions. So I can attest from first-hand experience that there is a demand for your book. Where should my readers go to find it?

Polish

Andrey Breslav made you smile during his keynote by saying that the focus was now to make Kotlin more POLISH. Can you tell the non-Polish among us what the Kotlin community looks like in Poland?

Well, probably it was totally accidental, but as a polish, I had to catch that word-game. About the community, there are many great conferences organized in Poland. I think it is a perfect place for conferences: in the middle of Europe, part of the EU, highly civilized and English speaking, and yet relatively cheap.

Although I am a bit disappointed about the Warsaw community. There are many Kotlin-related events in Krakow and Wroclaw, but not so many in Warsaw. I hope to change it, and so I support local communities and I started organizing a new conference we call Kotlin Day and Night. We are now talking with sponsors, but it seems that we will be able to organize the first edition in Oct 2020.

Underhyped projects

Do you have any favorite-but-not-well-known Kotlin project that deserves in your opinion more publicity?

In general, I think that in our community we have an effective flow of ideas.

One project I think is underestimated is MockK which is a powerful and multiplatform alternative to Mockito. It’s the only problem is a few sec startup time, but except for that, it is much more convenient and featured than any other mocking framework I ever used.

There are a few nice project examples like Igor’s Android-Showcase or Saket’s Press.

GitHub logo mockk / mockk

mocking library for Kotlin

drawing drawing

Relase Version Change log codecov Android Matrix tests Open Source Helpers

Getting started

All you need to get started is just to add a dependency to MockK library.

Gradle/Maven dependency

Approach Instruction
Gradle
testImplementation "io.mockk:mockk:${mockkVersion}"
Gradle (Kotlin DSL)
testImplementation("io.mockk:mockk:${mockkVersion}")
Maven
 <dependency&gt
     <groupId>io.mockk</groupId>
     <artifactId>mockk-jvm</artifactId>
     <version>${mockkVersion}</version>
     <scope>test</scope>
 </dependency>
android Unit
testImplementation "io.mockk:mockk-android:${mockkVersion}"
testImplementation "io.mockk:mockk-agent:${mockkVersion}"
android Instrumented
androidTestImplementation "io.mockk:mockk-android:${mockkVersion}"
androidTestImplementation "io.mockk:mockk-agent:${mockkVersion}"

DSL examples

Simplest example. By default mocks are strict, so you need to provide some behaviour.

val car = mockk<Car>()

every { car.drive(Direction.NORTH) } returns Outcome.OK

car.drive(Direction.NORTH) // returns OK

verify { car.drive(Direction.NORTH) }

confirmVerified(car)
Enter fullscreen mode Exit fullscreen mode

See the "Features" section below for more detailed examples.

Spring support

Quarkus support

  • quarkus-mockk adds support for mocking beans in Quarkus. Documentation can be found here

Kotlin version support

From version 1.13.0 MockK supports Kotlin 1.4 and higher

Known issues

  • PowerMock needs a workaround to run together with MockK…

GitHub logo igorwojda / android-showcase

đź’Ž Android application following best practices: Kotlin, Coroutines, JetPack, Clean Architecture, Feature Modules, Tests, MVVM, DI, Static Analysis...

đź’Ž Android Showcase 2.0

Kotlin Version AGP Gradle

Codebeat Badge CodeFactor

The Android Showcase project exemplifies modern Android application development methodologies and provides comprehensive architectural guidance. By integrating popular development tools, libraries, linters, and Gradle plugins, along with robust testing frameworks and Continuous Integration (CI) setup, this project offers a holistic sample of a fully operational Android application.

The primary focus of this project lies in promoting a modular, scalable, maintainable, and testable architecture. It incorporates a leading-edge tech-stack and embodies the finest practices in software development. While the application may appear straightforward, it encompasses all the crucial components that lay the groundwork for a robust, large-scale application.

The design principles and architectural choices applied in this project are ideally suited for larger teams and extended application lifecycles. This application is not just about showcasing functionalities, but it is also a testament to how well-structured and well-written code serves as a stable backbone for scalable…

GitHub logo saket / press

Cross-platform markdown editor written in Kotlin Multiplatform (work in progress)

Press is a wysiwyg writer for crafting notes inspired by Bear. It uses markdown for styling and formatting text with a beautiful inline preview.

Press was created as a proof-of-concept for exploring Kotlin Multiplatform, as well as the author’s frustration from the lack of minimal markdown note taking apps that work on all platforms, especially Android and macOS. If you relate to either of these reasons, Press is looking for contributors.

Documentation

License

Copyright 2019 Saket Narayan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless
…

Contact

Where can people reach you if they want to know more?

For public questions, https://twitter.com/marcinmoskala is the best place. For private ones on conferences.

If you need technical help, I suggest Kotlin Slack public channels or Stack Overflow.

There are many experts there ready to help.

đź’– đź’Ş đź™… đźš©

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related