Settings

Theme

The Hitchhiker's Guide to Kotlin

nathan-contino.github.io

93 points by minnca 4 years ago · 33 comments

Reader

nassimsoftware 4 years ago

Kotlin is a really nice language. I wish it was used more especially that now it isn't just limited to the Android and Java ecosystems.

Kotlin can now compile to native binaries : https://kotlinlang.org/docs/native-overview.html#sharing-cod...

Kotlin can now compile to Javascript : https://kotlinlang.org/docs/js-overview.html Edit : You can even use React with Kotlin : https://kotlinlang.org/docs/js-get-started.html

Kotlin can now be used for datascience as well (You can run kotlin code in a jupyter notebook) : https://kotlinlang.org/docs/data-science-overview.html#libra...

  • pjmlp 4 years ago

    Kotlin/Native is hardly a stable target, given that they botched the implementation by going with an incompatible memory model as Java, and have spent the last year fixing it by rebooting it with a proper GC implementation.

    Hardly any better than just using GraalVM or any other JVM AOT compiler, some of which go back to early 2000's even if only available in commercial JDKs.

    The JavaScript compilation target still needs to do some catching up with TypeScript.

    • erpellan 4 years ago

      I feel the non-JVM targets are a real distraction from what is a lovely language. I’d rather they focussed more on fixing the issues with kotlin in IntelliJ - you’d think controlling both the language and the IDE would result in a less buggy experience than the current situation.

      • pjmlp 4 years ago

        I agree, to me it feels just like JetBrains turned Borland just before the Inprise Corporation makeover.

      • zorr 4 years ago

        Could you elaborate on specific issues you have with Kotlin/IntelliJ? I use both as my daily work environment and I barely have issues.

        • erpellan 4 years ago

          It frequently loses track of libraries, claiming large chunks of the codebase won’t compile until caches are cleared and it is restarted.

          It gets stuck indexing forever, losing all code completion and navigation abilities until it’s restarted.

          Git integration sometimes stops working. It will list the files to be committed but does nothing when you click commit, until it’s restarted.

          Exiting presentation mode leaves the font set to a huge size (until it’s restarted).

    • lokedhs 4 years ago

      My personal open source evening hacking project for the last year or two has been a Kotlin multiplatform project. It's language interpreter that runs on the JVM, Native and Javascript.

      Since the project is an interpreter, it's easy to do performance testing between the different platforms. I just write some program in my language, and run it on each implementation.

      The results are interesting. The JVM target is consistently 20 times faster than native. Now some of this is because memory allocations are so fast in the JVM, so if your programs does less memory allocations the difference won't be that great. However, if you compile Kotlin to native code, one has to accept that it's much slower.

      Another problem with native compilation is that it's quite slow. I wouldn't want to do all my development using native, but rather develop using the JVM and then compile to native for the use cases that need it.

      The only time I think that native would make sense is if you absolutely cannot run the JVM for some reason. One such reason would be for embedded (which would have to be ARM, since that's the only embedded architecture that's supported).

    • nassimsoftware 4 years ago

      What catching up is required compared to TypeScript? Could you go in bit more details on that, I'm curious.

      • pjmlp 4 years ago

        Ecosystem, tooling, libraries.

        • smallerfish 4 years ago

          It's actually very good - debugging from the IDE now even works. I'd recommend building around KVision rather than raw KotlinJs (https://kvision.gitbook.io/kvision-guide/).

          The main thing that can be annoying is figuring out module exports from a js library that you want to use (which is probably easy enough if you have a modern javascript background). I haven't found a library that's stumped me yet, but some are more annoying than others. Jetbrains have a tool called Dukat which will supposedly one day automate this, but for now it's not there.

          Distribution size can be an issue, but it's not terrible. My current (moderately complex) app is floating at around 2MB. I haven't tried splitting it up, though that is possible.

          • pjmlp 4 years ago

            Except the platform is JavaScript, and TypeScript is basically Web IDL aware compiler on steroids, no need to use anything that adds even more layers.

            • smallerfish 4 years ago

              That depends. If the rest of your application is in Kotlin, you're reducing cognitive overhead. Same language, same compile, same distribution. You can share view models between frontend and backend. It's much closer to what GWT promised to be than GWT ever was.

              • pjmlp 4 years ago

                Guest languages are always an impedance mismatch with the underlying platform.

                I don't buy into sharing source files instead of clean separation via Web APIs, the additional tooling complexity isn't worth the pain.

                I did vote against stuff like GWT already several times, and every time it has been proven to have been the right decision, when things went hot.

                • smallerfish 4 years ago

                  > I don't buy into sharing source files instead of clean separation via Web APIs, the additional tooling complexity isn't worth the pain.

                  Fair enough, that's a different argument from the one you posed upthread though. The ecosystem and tooling are very much there.

                  Happy to disagree on architectural merit of the approach. Personally I'm done with javascript/typescript for any future products/teams I build, and am glad to have a viable solution.

  • ParanoidShroom 4 years ago

    I do kotlin daily, almost exclusively including side projects.

    Kotlin native has been rocky personally, I'll check back in a year.

    Kotlin on Android is a must compared to Java, else you're a fool. Haven't seen any exceptions in the industry.

    Kotless is okay, works, but early days.

    I've seen decent backends on quarkus and kotlin.

    It's a fun language, the couroutine development is pretty nice. Flow is a lot easier than Rxjava.

    • bestinterest 4 years ago

      Coroutine stuff worries me in Kotlin, what happens the minute Project Loom comes out? Even more fracturing of styles?

  • vips7L 4 years ago

    Does kotlin native have a std lib yet? Last I checked you couldn’t even read a file.

  • BoxOfRain 4 years ago

    I absolutely love Kotlin for backend web dev, I started using it as "Java but without the things in Java that piss me off" but the more I used its non-Java features the more I grew to love it. Sadly I've moved jobs so I'm not using it professionally any more but it powers my side project so I'm keeping my skills fresh.

    I'd recommend it to anyone where they'd traditionally use Java or another similar language.

travisgriggs 4 years ago

I do a bit of Kotlin on Android. Two decent sized native apps. We do no Java, but use some Java libraries.

I like Kotlin enough. But I don’t love it. Like Swift (and maybe Python yet), it’s a language that tacks on more stuff every release in interest of grabbing yet another programming model niche. It’s like a Swiss Army knife with a _lot_ of blades and gadgets and tools. It’s like a VC funded start up that throws features at the wall to see what sticks and placates.

I want my languages to be built on simple unifying principles. Kotlin’s principles often seem to be:

- we get rid of boilerplate . . . when ever we can, but not really consistently. Removing boiler plate trumps consistency in code communication

- you have a rising idea over there in your ecosystem? we will add it in ours

- by keeping our documentation very simple (nee spartan), we can claim our language is simple

This year it is coroutines, tomorrow it will be sealed classes, or who knows.

dehrmann 4 years ago

I tried Kotlin, but I went back to Java. Interacting with Java libraries can be OK, but it can also get ugly. Using companion classes for loggers seems overkill. I didn't like having to explicitly cast ints to longs.

I ended up going back because Java's getting better--var and records help with some of the pain--the tooling is simpler and more reliable, and working with something like Hadoop was significantly harder.

  • bberrry 4 years ago

    Why would you use companion objects for loggers? The logging library should only create one instance when you do getLogger({name or class}).

smackeyacky 4 years ago

Has anybody switched to mixing in Kotlin code within an existing Java Android app? I'm not a huge fan of Java but I don't know whether it will be too jarring for maintenance tasks if I have both languages as code. There is a lot of XML floating around in my app (as most Android apps have) but switching between editing that and the Java is fine.

I'm keen on the idea that there would be less code and my app is getting a little creaky and could use a refresh, but does Kotlin make the boring stuff easier? Things like inflating classes from REST json calls, interacting with the SQLite database.

How does it do on the more "exciting" (ahem) parts of Android like Bluetooth? I lean heavily on RxAndroidBLE to cope with the rubbish bluetooth stack and I think the code around that could do with being simplified.

  • EdwardDiego 4 years ago

    I haven't used Kotlin on Android, but on the server side, it does make some of the boring stuff easier if you're stuck on an old JDK - e.g., Jackson supports serialising/deserialising straight into Kotlin data classes, you don't need this if you can use Java 14+'s records, but if you're still on Java 8, it's far nicer than all the boilerplate POJOs.

    As for the DB layer, I quite like JDBI which integrates well with Kotlin also: https://jdbi.org/#_kotlin

    I'm not familiar with RxAndroidBLE, but they have some Kotlin examples on GH: https://github.com/Polidea/RxAndroidBle/blob/master/sample-k...

  • KiranRao0 4 years ago

    I'm (slowly) in the process of migrating an Android app from Java -> Kotlin. The compatibility with Java is pretty amazing. Mixing the two languages feels almost the same as two java files.

    There is a bit of cognitive overhead at first, but that's the overhead of learning a new language. Plus I immediately see the benefits, specifically WRT optionals (?. and ?: operator).

  • pjmlp 4 years ago

    On Android, it is kind of clear, Google is doubling down on Kotlin and the team is hardly motivated to move Android Java into proper Java, so long term Kotlin seems to be the only path forward.

    Still cherry picking features from Java 11 when Java 17 just got released, https://android-review.googlesource.com/q/project:platform%2...

    • Ologn 4 years ago

      I agree. Two years ago a slide at a Google I/O talk said "Coroutines first", and coroutine scopes like lifecycleScope are Kotlin only.

      Jetpack Compose is the future of Android UI and is Kotlin only.

      Insofar as bringing it into the app - from most apps I know of, new code is usually written in Kotlin, and the Java code becomes legacy (which is sometimes rewritten in Kotlin). It works well enough, compatibility with existing Java was important to the Kotlin team (for adoption and other reasons).

      Read the Kotlin docs for more https://kotlinlang.org/docs/jvm-get-started.html . You definitely see some of the things mentioned when using both languages, like the JvmStatic annotation etc.

      • pjmlp 4 years ago

        The problem with that strategy is that as the JVM gets Loom, SIMD, JNI replaced by Panama, bytecodes that don't translate to DEX,... the amount of Java libraries available to Android developers diminishes.

        So doing Kotlin in Android, with an anti-Java agenda, reduces Kotlin to use pure Android libraries, or pre-history Java ones that can still be called from Android Java.

    • smackeyacky 4 years ago

      Oh really? Is this still a hangover from the Oracle lawsuit stuff?

      I might give it a whirl and start replacing the easy stuff first. In my system the worst part is the Android app and anything that makes it easier to maintain in the long term is welcome.

      edit: Not sure how I missed this announcement: https://www.zdnet.com/article/google-were-using-kotlin-progr...

      • pjmlp 4 years ago

        Oracle has nothing to do with it, given that Kotlin and Android Studio are fully dependent on the JVM ecosystem.

        If that was the reason Dart would have been a better option, but I bet the Android team is not that keen on having Fuchsia take away their party, hence JetPack Composer.

      • jamesfinlayson 4 years ago

        Google can say they get less bugs, and that's probably true, but I'm sure it was partly a strategic hedge in case they lost the lawsuit with Oracle as well.

  • jamesfinlayson 4 years ago

    Not personally but it's happening in a few apps at my current company. In my job I mix Java and Groovy and at home I've mixed Java and Kotlin and it mostly just works (I don't think you can inherit Java -> other language -> Java because things get compiled in order but that has only been an issue for me once).

davidatbu 4 years ago

My biggest gripe with Kotlin is ... it's raison d'etre, that it's designed to incentivize me to use IntelliJ, and IntelliJ support for Kotlin is currently miles ahead of the more open alternatives.

I really wanna keep using neovim and LSPs ...

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection