diagram

Hi I’m Kody O’Connell, Optimizely’s Developer Experience Advocate. I recently interviewed open source contributor Patxi Bocos about the Kotlin programming language and his contribution to open source with his project Poetimizely. Listen and/or read below to learn what Kotlin is, who is using it and why you should care.

Poetimizely on Github

Kody [00:00:02] Hi, my name is Kody O’Connell and I’m a developer experience advocate at Optimizely. Today I’m with Patxi, Patxi Bocos right? Am I saying that right, Patxi?

Patxi [00:00:11] Yes, that’s correct.

Kody [00:00:14] Patxi is a developer who recently contributed an open source project: Optimizely’s ability to integrate with Kotlin. So I’d love to hear you, Patxi, talk about your journey in programming and and how that brought you to Kotlin.

Patxi [00:00:35] Cool. Yeah, so my name’s Patxi. I’ve been a software engineer for like ten years? It’s been a long time and I’ve been working in many areas like mobile development, back end, front end, so I’m kind of “full stack”. I don’t like that word, but you can kind of put it like that. During my most recent experience, I’ve been working with Android and the Kotlin language in particular, although during my career I’ve been working with other languages such as C++, Java, C#, Groovy, etc.

Kody [00:01:15] Oh, great! I love working in different programming languages too when I get a chance. C++ is one of my favorites… um because of its structure? I have a background in mechanical engineering and of course C++ is used in hardware programming… but yeah that’s really great! So, maybe talk a little bit about how you first got into Kotlin and then um… can you help us understand what Kotlin is? I know it’s a… it’s an extension of Java right?

Patxi [00:01:50] Yes, some people say that (I think this is not very fair) but some people say that Kotlin is like “a better Java”. But it’s not fair to say that because in the end, Java needs to be retro-compatible with lots of versions, and Kotlin just started from scratch, some years ago. But in the end, Kotlin makes an easier to use language compared to Java, because we have type inference and many features that make simpler. So, for example in Java, you may need to write like four lines to make one thing and in Kotlin it could be one line.

Kody [00:02:34] Right, awesome. Yeah, I read that it was more concise and kind of easier to work with than Java. I bet anyone who has programmed in Java knows what we’re talking about… there’s a lot of syntax in Java, there is type safety, but I guess (not like in Kotlin) there’s not null, null-pointer type safety right? Which Kotlin has? Can you talk a little bit about that?

Patxi [00:03:00] Yes, yeah. In the end in Java, any object can be assigned to a null value, and there is no guarantee that a value will be null or not. There are annotations that will help you at compile time saying, “OK, this variable may be null”. In Kotlin this is built in with the language.

Patxi [00:03:21] So when you declare any type, if you want that value to be nullable, you must explicitly say so. There are different types. For example, there is a type ‘String’ and there is another type, which is ‘nullable String’. So, this way it forces you to check if a value is null or not. So, if you have a value of type ‘nullable String’, you can’t make a call like ‘myValue.length()’. You will need to first check ‘if value != null, then -> do this’.

Kody [00:03:57] I see, so the compiler will force you to make that type check before actually compiling. It kind of reminds me of optional types in Swift? (For any Swift developers out there!) Would that be a fair comparison?

Patxi [00:04:12] Yes, Java already introduced the Optional type, which is a wrapper on top of a value. But even a type ‘Optional<String>’ can be assigned to null. So there’s no guarantee that the optional type itself is not going to be null!

Patxi [00:04:29] So you end up having nulls somewhere. It’s also true that when you define a nullable type in Kotlin, you can “make the hack” to assume that it’s not going to be null. So you can also get null pointer exceptions, but that’s usually not “good practices”.

Kody [00:04:48] Right, haha. There’s a way to accomplish anything, I guess, in programming. So now that we have talked a little bit about how Kotlin is adding, I guess maybe value? (you could say) to Java for people who are trying to be more productive with Java? By adding in these safety features and these changes to the conciseness of the language. So, would you say it’s a better Java? Or is it just a Java for certain people writing certain kinds of apps?

Patxi [00:05:34] I think that if I were choosing which language to use between even Java 14, the latest version of Java and Kotlin, at this point, even in the context of mobile or back end, I would go either way with Kotlin. For example: in the world of Android Development, Kotlin is officially supported since two or three years ago when Google announced at Google I/O that Kotlin was a supported language.

Patxi [00:06:01] In the context of back end, for example in Spring Boot, which is a very popular web framework, Spring Boot also officially supports Kotlin. So if I were starting from scratch, and deciding between Kotlin and Java, I would always (at this point) go with Kotlin. Because the way JetBrains is evolving the language is super awesome.

Kody [00:06:30] That’s really interesting, I’ll have to talk to my Java friends about that and see what they think about Kotlin!

Patxi [00:06:39] Yeah, something I usually hear is that “all the people that try Kotlin, they never go back to Java”.

Kody [00:06:47] Haha, so Patxi are you trying to say that “once you go Kotlin, you don’t go back”?

Patxi [00:06:51] Yes…

Patxi [00:06:54] Haha, or maybe there are use cases… I think one important thing when you are using Kotlin is understanding what is the bytecode being generated under the hood. So it’s like, I’m using this feature from Kotlin like ‘object’, then I think it’s very important to understand what the code, or the equivalent code, in Java, is being generated by the compiler. Because sometimes it’s a lot of magic under the hood, and you really need to understand what’s being generated.

Kody [00:07:20] Right, yeah, that’s one of the things I saw in my reading-up about Kotlin… JetBrains contributes mostly to Kotlin, right? Of course they have their IDE, and I saw they have built in a lot of great functionality with the Android studio, too, with that kind of like equivalent display of the code in Kotlin, like while you’re writing it in Java. I thought that was really cool to see.

Patxi [00:07:46] Yes, yes, you can look at the bytecode and then you can see the Java equivalent code for that bytecode, which is super helpful, and you have that integrated in the IDE.

Kody [00:07:56] Awesome. Yeah so I also wanted to get into some of your favorite things or quirks about Kotlin, we maybe talked about a couple of them already, but I’m curious what your thoughts are.

Patxi [00:08:09] Yeah. One thing I think is super powerful is the type system. Because, in Kotlin in particular, almost everything is an expression. So what does this mean? You can differentiate two things, expressions and statements. So, statements are things that are usually producing side effects, and they don’t return any value. For example, printing a line to the console is a statement because it doesn’t return anything, it just produces this side effect. And for example, in Kotlin an “if/else” statement is not a statement, it’s an expression. So you can assign whatever the “if/else” returns to any value. You could write ‘val name = if (this == null) “a” else “b”. This can be applied to many other things like ‘when’ statements or even, this is more crazy… The ‘return’ statement in Kotlin returns ‘Nothing’. So when you say ‘return 0′, this ‘return 0′ can be assigned to a value.

Kody [00:09:28] Oh, I see what you mean. OK.

Patxi [00:09:29] Or even when you throw an exception, the ‘throw statement’ is returning ‘Nothing’, which is a type in the type system of Kotlin.

Kody [00:09:42] Is it actually called ‘Nothing’?

Patxi [00:09:43] Yes! There is a type called ‘Nothing’, and ‘Nothing’ is a subtype of every type. If we look at the hierarchy of Kotlin, or if we compare it to Java, in Java on top we have the ‘Object’ type. And then there are many types classes implicitly extending that ‘Object’, and then we build complex hierarchies. In Kotlin, the Java ‘Object’ is called ‘Any’. There is a bottom type in Kotlin, that can be considered to be extending all the types, and this type is ‘Nothing’.

Kody [00:10:13] Wow, that is pretty uh, meta when you think about it.

Patxi [00:10:16] Yeah, it’s like you have a type which extends every other type, when you still don’t know all the types… It’s not exactly like that, but you can understand it like that.

Kody [00:10:27] It sounds almost like a cheat code, for the type system, if you needed to use it that way.

Patxi [00:10:33] Yes. This feature allows you to do things like… So let’s say you want to assign a value if some other value is not null, and otherwise, throw an exception. In Kotlin you can write, ‘val myName = if (otherValue != null) otherValue else throw Exception()’. And you are assigning to the value one thing, of type ‘String’, and otherwise an exception. And you can do that because the common type between a ‘String’ and ‘Nothing’ is a ‘String’. Because ‘Nothing’ extends the ‘String’. So in the end, the common type between any arbitrary type, and ‘Nothing’, is that type. Because ‘Nothing’ is more concrete than the given type. It’s kind of crazy.

Kody [00:11:30] That is pretty cool, yeah. When you get into those ways to implement a programming language, the concepts and the choices that you would make, it strikes me as something that maybe only an IDE company could so successfully create, this kind of unique language… Because I imagine they do have a really good insight into, you know, developers actually producing code.

Kody [00:12:05] Are there any other favorites you have?

Patxi [00:12:09] Yeah, I also like how much less verbose Kotlin is compared to Java, with the type inference. In Java, in the latest versions, they introduced type inference, which is cool, but also something interesting from Kotlin is that it makes less verbose, declaring types which are ‘final’ or not. So in Kotlin, if you want to define a value which is not mutable, then you don’t need to set the ‘final’ keyword before defining the value. In Kotlin, there are only ‘val’s or ‘var’s. ‘Val’s, or values, are immutable, and ‘var’s are mutable. So in Java, there is the ‘final’ keyword for defining when values are not mutable. But in Kotlin it’s just ‘val’ or ‘var’. These kind of things… And also one interesting thing from Kotlin is the standard library for using collections. So in Java we have a stream API that I think was introduced in Java 8. So it allows you to get the collection and then make a map, flat map, or filter etc. I think that the API in Java, is kind of, not as nice as the Kotlin one. And Kotlin collections are super awesome. There’s a lot of operators built into the language that are super handy.

Kody [00:13:41] So it sounds like everywhere they could, they added more usability and those kinds of things that developers might enjoy, into the extension from Java.

Patxi [00:13:54] Yes. Another thing, I think in Kotlin that is also very, very good, is that it’s very functional. Because you can define a function where an argument of that function is another function. In Java, there are the lambdas, but the syntax is not as nice as in Kotlin. Because in Kotlin, you can just say, ‘OK. This function is receiving a string as the first argument and the second argument is a function that, given a string, returns an integer. In Java, the syntax is not as easy to write as in Kotlin.

Kody [00:14:36] Yeah, I can speak from experience on the difference in a language that makes it easy to pass functions as arguments and those that don’t. I mean just considering callbacks as one of those situations.

Patxi [00:14:51] Yes, because in Java there are the functional interfaces, the consumer interface… but it’s not as nice as Kotlin, which has it in one place.

Kody [00:15:08] Well, you’re making me become really interested in Kotlin… Even more than I was before. I might have to jump in and write a sample app or something. Maybe I could use your open source contribution! But before we talk about that, do you know in the enterprise and in the industry, are people really adopting Kotlin really quickly, already?

Patxi [00:15:32] I think one thing for sure is that Kotlin probably became so popular because of the Android world. Because even before Google announced that Kotlin was a supported language, a lot of people were doing Kotlin in Android with less support than today, and probably Google announcing that Kotlin was an official Android language made Kotlin super famous. And I don’t have much context in the back end word, if people are using Spring Boot with Kotlin. There is also another framework called Ktor, which is maintained by JetBrains, and it’s becoming very popular. But I don’t have a lot of context there.

Kody [00:16:19] I see, OK. Well, yeah, that makes sense, I did see a lot about Kotlin on the Android website, but I did also see on their project page they are trying to make Kotlin available for compiling to native and for data science and server side applications. So that’s really interesting.

Kody [00:16:40] So I also wanted to talk to you about this process of evolving languages and where (as a community of programmers and language creators) are we going. Do you think that it’s possible to move from Java to Kotlin as a community? Or is it just that the inertia in Java is so large that Java will always, you know, kind of fork in its own way and Kotlin will fork in its own way?

Patxi [00:17:18] I think that having the option to choose going with Java, or Kotlin or any other language, is great for the community because again, Kotlin is getting features from this other language and in the end we are getting improvements by having this variety of languages.

Patxi [00:17:41] So I don’t think that Java will disappear because in the end Kotlin is only possible because of the Java Virtual Machine (JVM). So I think Java is going to keep evolving and Kotlin is going to learn from Java, Java is going to learn from Kotlin. But evolving Java probably is harder than with Kotlin, because there are a lot of companies running Java 7, or even Java 6 or lower versions… So that’s why it’s not fair to say that Kotlin is a better Java. Of course, it’s better in a lot of things, but Kotlin doesn’t have as many problems as the Java community has.

Kody [00:18:18] Right. And is it true that Java is not open source??

Patxi [00:18:25] Yeah I’m not sure about that… I think it depends on the usage or, because there are like many licenses… I’m not sure about that.

Kody [00:18:33] Right. And then Kotlin is open source, but it extends Java… So I guess maybe we’re not clear on the legal aspects of that haha…

Kody [00:18:45] It’s really cool to hear your perspective on that. I think that rings true to what we see in reality is that, you know, even the smallest community around a certain programming language finds a way to keep it alive if that language is useful in any application. And so you find, like, I don’t know, like maybe the COBOL community, I’m not going to get hate tweets from people about calling that a small community haha. But um, yeah, I think it does make sense that these languages will persist as long as some people find them useful.

Patxi [00:19:18] Yeah, one thing I can say is that Kotlin is completely interoperable with Java. So having both Kotlin and Java running in the same place, there’s no problem with that. I think that makes it super easy to introduce Kotlin in a team. Because in the team I was working on, the codebase was completely in Java. There was not a single class in Kotlin. And then I joined the team and we started to introduce Kotlin… and I think a good way to introduce Kotlin could be writing tests in Kotlin. You have your code base in Java, and then maybe one day you wake up feeling motivated about Kotlin and then you start typing your first test in Kotlin.

Patxi [00:20:08] Another great thing is that Gradle (which is the build system used in Android; there are also alternatives to Gradle but Gradle is the most popular one), supports Kotlin. So the build scripts you would write for Android can be written in Kotlin, and probably that’s the preferred way.

Kody [00:20:27] Yeah, I was gonna ask, do you think Kotlin with it’s extra safety features helps you to write tests better or easier than in Java?

Patxi [00:20:40] I think that for me at least, it helps me in every way.

Kody [00:20:49] Haha, great!

Kody [00:20:51] So finally, I want to talk about your really wonderful contribution to open source (Poetimizely) that connects the usability of Kotlin with the usability of the Optimizely SDKs (which are also open source, but do operate with the Optimizely app). Which, I’m excited to say now does have a robust free plan that enables you to have free feature flags and one free experiment running at a time. So I think it would be really cool to hear why you decided to make this. Would it be a connector? Or what would you call it?

Patxi [00:21:39] Let me give you some context… for me the problem when we are programming is that everything can be safe until you need to deal with I/O, any input or output. Because dealing with code, you know that this is a string, or you know that this is a number, and then you can safely compare one type to another type. Because in the end, we are just calling functions, transforming inputs to outputs; it’s like a puzzle. But we need a different plan when we need to deal with any API or a database. And using Optimizely is like this, because the Optimizely SDK is making API calls under the hood. But you cannot trust that the API is going to return the type that you were expecting, because maybe you were expecting a boolean (in the JSON), but the boolean becomes a number, or some other type.

Kody [00:22:34] Right, because those values can change, they’re not static. They’re being updated by the SDK polling the Optimizely API, or using webhooks to deliver those new configurations. And yeah, that was kind of my curiosity about how that would work with the Kotlin type checking system. So Kotlin throws errors at compile time about type safety, right. So if you’re running this in production, your code is already compiled, so I’m kind of curious about how that works.

Patxi [00:23:07] But that’s going to be the same problem in any other language. The reason for me building this library was to avoid not having that type safely, because in the particular case of Optimizely we can get an experiment and for that experiment we can check which variation is the user on? And that variation is normally represented as a string. So it could be the original variation or any other one. But they are both strings. So when you are coding, you need to say, ‘OK, if the variation is this one…’ But then you can type the variation name wrong and then your code will not work as you were expecting, and it will compile! Because you are just comparing strings to strings. So for me, the type safety that my library is providing is by transforming the strings for each of the variations into an enumeration. That’s where the type safety comes from in this particular case.

Kody [00:24:08] I see, great. So that’s a very um, custom way that you were able to connect that into the Kotlin paradigm.

Patxi [00:24:23] For me, this can be thought of as instead of making an API call directly to a web service, if instead of that you are using an SDK which is providing you classes that are mapped to JSON responses, it would be similar to that.

Kody [00:24:43] OK, cool, cool.

Kody [00:24:45] So thank you for making this! And I’m wondering, are you using this? Were you using this, or was it just a fun project for you?

Patxi [00:24:58] I started it as a pet project because we were using Optimizely at the company I was working for, but one month ago I joined another company… But the reason for building this, my idea was to have this running on our continuous integration (CI) system. So we would get code generated for each of the experiments and features that we had on Optimizely and then our code would use that generated code and in case a variation, or an experiment or something, gets changed or deleted, then our code will stop compiling probably. And that’s good! Because instead of failing at runtime, we will see the bug before the code compiles.

Kody [00:25:52] Amazing.

Patxi [00:25:57] I haven’t used it in a production project yet, but I would encourage anyone using Optimizely to use the library. Because in the end, you are getting type safe code, so instead of checking if a variation has this key, you will just be checking a given type, a value of an enum.

Kody [00:26:16] Great, and it works in Android? That’s the domain you were designing for?

Patxi [00:26:22] You can use it on Android or any Java/Kotlin application. It could be on the back end or front end, it could be in any shape. It’s in the shape of a Maven plugin and a Gradle plugin. So Android developers can use the Gradle one, but if you are using Maven as the build system, you can grab the Maven plugin.

Kody [00:26:52] That’s fantastic!

Kody [00:26:55] Patxi, thank you so much for explaining Kotlin to us, and for doing this important opensource work. I think it’s amazing the world we live in today where, when one person solves a problem for themselves or their team, they are generous enough to make it public so that everyone else can benefit, and I think that is one of the best parts about working in the developer world.

Patxi [00:27:22] Yeah, thanks to you also. I hope that any people who want to contribute to Poetimizely, or who have any ideas, know that I’m completely open to to collaborate with them.

Kody [00:27:37] Excellent. Well, in that case, maybe if you want to share any ways that listeners can get in touch with you about this project, should they just go to the open source GitHub repo? Or get in touch with you on Twitter?

Patxi [00:27:54] Yes, yeah they can reach me via github or twitter.

Kody [00:28:01] Awesome, fantastic. Well, I’ll put some links for both of those in the blog post when we publish this interview. Just want to say thank you again for doing this, and then for talking to us about it! We definitely want to help people find out that this tool is out there, so this will help us to socialize it, so thank you so much.

Patxi [00:28:25] Thanks to you Kody.

Kody [00:28:27] Have a great day! I’ll talk to you later.

Patxi [00:28:30] OK Bye.