Groovy, Kotlin, and Java

ᎣᏏᏲ. ᏙᎯᏧ? Hey, welcome back!

I was reading this article which discusses the pros and cons of Kotlin vs Groovy. It’s a good article and worth the read. It’s been a few years since this was published, but I’m going to comment anyway.

Disclaimer: I’ve used Groovy since 2005, have been a Java developer since 2000, and use Grails on a daily basis. And I’ve used IntelliJ (and other IDEs from JetBrains since 2002).

Kotlin has more of a Typescript feel to it when coding.

If I’m making Gradle files I stick with Groovy. That’s pretty much because I’ve used it for so long. Had I learned Kotlin first I would use it over Groovy.

Groovy does have some annotations for compiling static @CompileStatic – at one time there was even a Groovy type-safe library.

I have never had any issues with Groovy code in large-scale production applications. I’ve used Groovy as a compiled language and scripting language in everything from games to j2ee sites.

I will say that when it’s used as a scripting language the calling code can be a bit of a slowdown. It will take longer to run Groovy script than it does Groovy compiled code.

Also, since I use IntelliJ and CLI for building, if I’m running Groovy code in the IDE I make sure to edit the configuration so it doesn’t build first. This makes prototyping a lot faster.

I’ve never used Kotlin for Closures (Lambda expressions) – but using Groovy I use Closures a lot, especially for testing.

The other advantage I have with Groovy on applications is Java developers. I can have Java devs start writing code immediately in Groovy classes and scripts and, for the most part, there are no changes that need to be made.

If I want to use Java and Kotlin they have to be separate files. I haven’t ever tried to use Kotlin and Groovy.

It depends on what you’re used to with a language. I can write a quick Groovy script that reads in a file, parses the lines, and gives me output in just a couple minutes. With Java, it takes a lot longer. I’ve not tried that with Kotlin.

As well, there are some syntactic advantages I have using Groovy over Java. For example, the ability to skip writing getters, setters, and different constructors.

class Example {
    def exampleProperty
    def exampleProperty2

//auto constructor
def example = new Example(exampleProperty2:"example property 2")
def example = new Example(exampleProperty: "1", exampleProperty2: "2")
def map = [:]
map.exampleProperty = "a"
map.exampleProperty2 = "b"
def example = new Example(map)

//auto setter
example.exampleProperty = "example property"

//auto getter
println example.exampleProperty

Some other point I’ll go over is the Groovy goodness. This wasn’t intended to be a persuasive paper just a recap of what I use each language for… which, if I can help it I never use Java directly.

This has been way longer than I wanted it to be. In the end, I break my projects down like this:

1) Android I’ll use Groovy with Gradle, Kotlin, and Java for the app (I’ve used Groovy I prefer not to)

2) If it’s a Grails, J2EE, or my own code app I’ll use as much Groovy as I can fit unless I need to perform some set of code that would be the equivalent of games writing in ASM. Then the performant dependent code I write in Java.

3) I’ll probably introduce more Kotlin to my development because it is more like TypeScript in many ways and I use a ton of TypeScript.

There’s more I have to say and have written about meta classes, etc.

Until next time. Dodadagohvi. ᏙᏓᏓᎪᎲᎢ.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: