Any tips for a Kotlin presentation?... and thinking in Kotlin

I'm giving a short presentation on Kotlin tomorrow at my company, and this got me thinking about what to talk about, mainly which advantages/features of Kotlin might be useful/appealing to our development team.   

So I’ve been thinking about is what are the greatest advantages of using Kotlin, and I think the biggest is the functional paradigms.  But as a Java developer, I don’t have a lot of experience in functional programming.  I started reading the book “Functional Programming in Java”, and this was somewhat helpful.  Does anyone have any other good resources for learning good functional programming paradigms?  Along the way I found this pretty good article by Bruce Eckel, the author of Thinking in Java: http://www.artima.com/weblogs/viewpost.jsp?thread=328540  It is about Scala, but I guess it can largely apply to Kotlin as well.  It would be great if someone wrote an article like this that talks about Kotlin.

Anyway, here is the outline of the topics I was planning to talk about for this presentation:

Kotlin features

- functional programming (first class functions, delegation, function literals, higher-oder functions, infix functions/LINQ-like queries) - extension functions means you can add useful methods to classes (like C#, and categories in Objective-C) - null-safety (no NPEs), properties, operator overloading, local type inference - inline functions: performant control structures - build infrastructure (modules) as part of the language

Kotlin advantages
- the upshot of all the features above: better, more readable code - plays well with Java.  Seamless integration with Java means that you can use it where it's useful/needed, but keep java code you already wrote, and refactor code slowly instead of having to throw out all your java code at once. - first-class IDE developed by JetBrains

Here’s what I was NOT planning to talk about… Builders, declaration/use-site variance, reified generics.  I could talk about them, but I don’t think the developerment team would care that much about it and honestly I don’t understand these features very well myself… although they are definitely kool features :slight_smile:

If anyone has any ideas, tips for a presentation, please reply.  Also, does anyone have any resources I can use for a presentation? (good code examples, a slide deck, etc.) that could save me some time for this.  Thanks!

That all sounds good to me. Some other benefits of Kotlin are:

  • compiles both to JVM bytecode and to JavaScript (admittedly the latter needs some work). e.g. http://kotlin-demo.jetbrains.com/
  • simple & very easy to learn and read (e.g. compared to Java or Scala)
  • much fast compiler than scala and small runtime footprint compared to scala & groovy
  • eventually a much more stable binary story than Scala

I wouldn't push the functional aspect too much (if at all) because of all the baggage that comes with it and also because Kotlin doesn't go as far into functional programming as a few other languages.

I think the main value of Kotlin is that it’s an incremental improvement over Java. The fact that some of these improvements are rooted in functional programming probably doesn’t need to be mentioned if you are presenting to a crowd that is not already sold on the need for a better language.

Start by showing some paint points in Java and then show the equivalent Kotlin code, this should be enough to gain some attention.

  • Less code
  • Traits
  • Pattern Matching

I am building 101 samples for Kotlin as part of my learning here https://github.com/dodyg/Kotlin101. You might find them useful.

Thanks for these additional points James, will defnitely will talk about these things too, I forgot to add javascript compilation to my outline.  The only issue I have with "simple and very easy to learn" is that this a subjective point, but I agree that it is simpler and I personally like the Kotlin syntax a lot!

I noticed is that using Kotlin-style code with higher order functions, functions as parameters to other functions, etc. takes some time to adjust the brain.  So while I agree it makes the code more readable ultimately, the declarations of functions can look pretty complicated.  So I question if Java developers will find this functional code hard to read when they were always used to dealing with OO code.

One more question: what do you mean by “more stable binary story” than scala… are you talking about compiling Kotlin to a binary executable via LLVM or similar?  If so, how feasible is this already, i.e. what prevents the implementation of this right away, aside from developerment bandwidth of course ;)  Just wondering because it would be pretty sweet if it were possible to create an android app and iOS app from the same codebase :slight_smile:

Good point, Cedric.  I agree (having just replied above that functional syntax in Kotlin hurts my brain sometimes... starting to get used to it).  Thanks for the feedback.  

I don't see the big deal with pattern matching.  Is this just the "is" keyword? What is the big idea behind that?

Thanks for the 101 samples, I have to spend time later looking at them, but looks good!

It's a neat feature that is useful in working with tuple. I have no idea about the big idea behind it. I quote this sample from http://confluence.jetbrains.net/display/Kotlin/Control+structures

``

when (tuple) {
  is #(1, 2) -> ...
  is #(val a, 3) -> print(a) // binding a to the first element of the tuple
  !is #(*, 1100) -> ...
  else -> ...
}

Unfortunately the complex pattern like above is not working yet with the current M2.

Another thing that I love is the when shortcut

``

fun main (args : Array<String>){
  var x = 101
  val greater = { (x : Int ) -> x > 100 }

  when {
  x in 1…50 -> print(“In range”)
  greater(x) -> print(“Great”)
  else -> print(“Outside range”)
  }
}

The ideas behind this concept are "algebraic data types" and "pattern matching".

I believe the Kotlin team has plans to extend this in future milestones with… I’m not sure the term they used, “value classes”?

I guess you could leave out the binary stability of scala issue; its only really applicable if you're talking to a scala crowd. But with scala whenever the compiler changes you pretty much have to have all your scala based dependent libraries rebuilt with the same version of the compiler & you have to upgrade all scala dependencies at the same time; so there's this big dependency-re-release dance which I don't really see scaling in the enterprise - though its fine for greenfield startup type environments. Typically folks in scala use sbt so that they re-release each release of a jar compiled against lots of different scala compiler versions. From Kotiln 1.0 this shouldn't be an issue; mostly as Kotlin avoids traits with fields & reuses the JDK's collections.

Ok, got it.  I don't have any experience developing with scala so maybe I shouldn't talk about it, but that reminds me of the pain of dealing with incompatible jar dependencies in big maven projects. And that reminds me of another point, kotlin modules :)

Ok, thanks for explaining this Dody and Cedric, I guess I should look into it more.  I asked what the big idea was because I've never thought to myself "gee, I wish I could use pattern matching in my Java code" so I wonder how useful it is, but maybe once I started using it then I would understand its usefulness.

Another nice benefit of Kotiln is nicer API documentation which is searchable (try typing "find" into the Search box top right for example) generated using wiki notation (markdown the default on github) and reusing markdown ReadMe.md files in package source directories  so that things look nice in github when looking at the source or in the API documentation when looking at a package description.

Also the API documentation allows inclusion of blocks of code from test cases to help illustrate methods via the @includeFunction macro; so its easy to create testable documentation which is refactoring safe.

Finally the API documentation includes a source link both to the implementation of a method and its test case example code by default. For example if you hover over the Iterable<T>.filter() method description, notice the source links on the right which take you straight to the relevant lines of code in the github source code or test case.

I know this is an old topic, but is there such a thing as “thinking in Kotlin” presentation? I’m still new to this (just two days in) and at this point in time I do not see much value proposition for this.

  • Java may be clunky but it is established and it is easier to source people that know the language for much cheaper.
  • I do like the idea of “compile to JavaScript” since that means you have one language for both Web/Node JS and Java application server side. However, value proposition again, JavaScript developers are more common hence cheaper to get.

As far as NPEs go, a typical programmer that you may get from a “professional” firm that where they’d likely not see the value of bothering to write a proper repeatable JUnit test (it’s pretty common sight to see in larger companies and government projects) they’ll likely find a workaround for it and you’d just see !! everywhere.

Lambda expressions and closures are cool, and less writing than that of writing a for loop. But the for loop is generally easier to understand for more developers.

Mind you don’t get me wrong, I see some potential with the language as I plow through it, but the thing that’s going on in my mind were the fact that you need people to ramp up and learn it (and try to do that with larger companies with people who just want to day done).

But one thing I liked is are the Basic types especially with the strings and numbers and the “templates” except for the ${'$'} escape. Hopefully there would be more basic types in the future such as versions (v1.0.0) local dates ({2011-01-01}), timestamps {2011-01-01T10:22:11.32322Z}, local times({14:33:23.0000001}) and local date times{2011-01-01T10:22:11.32322} since those would be closer to a “business” terms rather than pure technical.

I also liked the Data class and Wrapper classes since they gets rid of a lot of boiler plate code.

Still I wouldn’t mind seeing a “thinking in Kotlin” guide if it makes sense as part of the official website. Personally going through https://kotlinlang.org/docs/reference/comparison-to-java.html I don’t think it’s enough of a wow for me to push to anyone.