Choosing between Kotlin vs. Groovy

[ad_1]

<img ” src=”http://cdn.datafloq.com/blog_pictures/kotlin-vs-groovy-which-language-to-choose.jpg” style=”display:none”>
A growing range of fields and general advancements in technologies push programming languages to constant improvement and adaptation to the present-day needs. Now we have a bunch of languages serving different purposes: some of them emerged as an independent project, while others bud off from established and well-known languages.

The colossus of Java, for example, has a number of offspring; some of them have proved to be a success. One of them, Kotlin, was backed by Google as the official language for Android development in 2017 and was reported to be the second most loved and wanted programming language in 2018 Stack OverFlow survey and remains in Top 5 in this year’s survey. Another successful member of Java-based languages is Groovy that is gaining popularity among developers. At the same time, the 2018 Stack OverFlow survey listed Groovy among the most dreaded languages. In this setting, it seems unfair to compare the languages, but let’s see whether Groovy is so dreadful compared to Kotlin and, generally, which of them to choose as another addition to your bag of skills.

Overview

Kotlin

Kotlin is developed by Jetbrains — a company well-known in the Java world for its IDE named IntelliJ IDEA — and open sourced in 2012. It is a high-level, statically typed programming language that runs on Java Virtual Machine (JVM) and can be compiled to JavaScript source code or handle the LLVM compiler infrastructure.

Though internally Kotlin is reliant on the present Java Class library, its syntax may not be specifically compatible with Java. Kotlin has aggressive type inference to decide the type of values and expressions for which type has been left unstated. This makes it less verbose comparing to Java.

Kotlin has a practical mix of features from Java, C# and other new languages. It generally shows many improvements over Java such as null safety or operator overloading, though lacking certain convenient Java properties such as ternary operator and checked exceptions. However, both languages are completely interoperable, so they can co-exist in the same application.

Besides, since Android Studio 3.0 (published in October 2017), Kotlin is also part of the Android SDK and is involved in the IDE’s installation package as an option to the standard Java compiler. The Android Kotlin compiler allows user to target Java 6, Java 7, or Java 8-compatible bytecode.

Kotlin is much appreciated by developers for its interoperability, code security, and accuracy.

Groovy

Groovy is an object-oriented programming language for Java platform that is distributed through the Apache License v 2.0.

The key feature of Groovy is that it aspires to combine the best of two worlds: it supports both static typing typical for Java and more relaxed dynamic typing similar to that of Python.

Moreover, it can be used as both a programming language and a scripting language for the Java Platform. Like Kotlin, Groovy is compiled to Java Virtual Machine (JVM) bytecode and interoperates seamlessly by different Java code and libraries.

Generally speaking, Groovy has a Java-like syntax, but it takes on the ease of more moldable languages, such as Python and Ruby. Groovy can be called a modern Java enhancer, since it provides greater flexibility and introduces special features to applications, such as safe navigation operator(?.), the concept of ClosuresTraits, runtime dispatching of methods, Groovy String, Array initialization and many others.

Groovy is a testing-oriented development language with syntax that supports running tests in IDEs, and Java build tools like Ant or Maven. Besides, it provides native support for markup languages like XML and HTML and domain specific languages.

What can also make it attractive to developers is its short learning curve: for Java developers, it is just a step away from the usual syntax, for new learners — it is relatively easy and modern.

Applications

Kotlin

Given the fact that Kotlin as an official Android development language at Google I/O, its most obvious application is Android development.

Speaking more generally, Kotlin is great for developing server-side applications, allowing developers to write concise and expressive code while maintaining full compatibility with existing Java-based technology stacks.

The most prominent applications of Kotlin are rather impressive and include the following giants:

  1. Pinterest moved away from Java to Kotlin for their Android Application Development
  2. Gradle built developing android files (APK files) for both IDEA and Eclipse
  3. Evernote integrated Kotlin in their android client
  4. Coursera built an online courses application for a range of courses
  5. Uber made internal tooling processes on Kotlin
  6. Atlassian and Trello did a full code conversion of the old codebase.
  7. Kickstarter helps find resources for people showcasing creativity.

Groovy

Since Groovy is so similar to Java, it is sometimes difficult to find a distinguishing application for it. One thing that is a definite benefit is that Groovy enables to write scripts besides classes, so you can write applications and scripting with the same language. Groovy scripts are a perfect fit for tasks that change often. Since Groovy is a part of JMeter distribution, it is a good idea to use it for scripting and possibly to migrate the scripting logic developed in other scripting languages, to Groovy.

Examples of using Groovy as a scripting language are rather numerous:

1. Netflix uses Groovy for server-side scripting to offer various levels of filtering Besides, Netflix Spinnaker is implemented in Groovy.

2. Oracle’s fusion middleware is using Groovy scripts in its business component suite.

3. LinkedIn use Groovy also in their “Glu” open source deployment & monitoring automation platform.

The other direction of practical applications for Groovy is to use it as an embedded business language (a Domain-Specific Language):

1. National Cancer Institute uses it to do scientific simulations

2. JPMorgan, MasterCard and other financial institutions use Groovy for its nice DSL capabilities.

Syntax

Kotlin

Kotlin is null-safe which is perhaps the most valued feature of Kotlin missing even in Java. Kotlin has two types of references: nullable and non-nullable, so you can compose code by limited NPEs. The null safety feature of Kotlin protects developers from accessing the properties of a null reference.

In Kotlin, every class is a function, and vice versa; besides Kotlin for Android has Optional types, which help with all the safety checkups. A class can be marked as a data class with the standard functionality and utility functions already present.

Kotlin has a lot of other characteristics, like smart casts, ADT (doc), type-safe builders, zero-cost abstractions and, rather importantly, great IDE support.

Groovy

Having no data class, Groovy 1.8 introduced a few alterations which involve,among others, a new class annotation `@Canonical` that is used to create mutable classes. This annotation allows you to write classes in this shortened form and loads all possible combinations of constructors, getters and setters, creating an analogue of the data class.

One of the key features of Groove is Groovy Closures that is an open, anonymous block of code that can take arguGroovyments, return a value and be assigned to a variable. In opposition to the formal definition of a closure, Closure in Groovy can also contain free variables which are defined outside of its surrounding scope that offers a variety of advantages.

Compilation

Kotlin

Kotlin is statically typed language, meaning that the type of a variable should be known at compile-time instead of at run-time.

Groovy

Groovy was created as a dynamic language, so code gets compiled to machine code on the fly while the program is running. Since Groovy 2.0, it is possible to enable static type checking using `@CompileStatic` annotation. It feels nevertheless a little bolted on and does not enforce people to code in a static way. Sometimes even with `@CompileStatic` annotation, Groovy seems to have some dynamic performance remained.

Alternatively, it is possible to designate static typing with the help of a configuration file specifying its name with the — configscript option of the Groovy compiler. It will apply the CompileStatic AST transformation annotation to all source files in the project.

Choosing between Kotlin vs. Groovy 1

Code Style Settings

Kotlin

One might think that Kotlin as the language developed by a company famous for its intelligens IDEs, will have limitless settings to configure code style. The reality, however, is that the code style settings offered for Kotlin in IntelliJ IDEA are significantly more limited compared to Java. The difference in code style settings is intentional to a degree. The company explains that in their vision they see Kotlin code using consistent coding styles (similar to PEP8 in Python) instead of supporting infinite configuration possibilities.

Groovy

Groovy takes a place somewhere in between Java with its extensive code style settings and Kotlin that has only limited possibilities. As compared to Java, Groovy seems to be 20–25% less flexible in terms of code formatting settings.

Performance

Kotlin

In most cases, Kotlin performance is the same as Java.

Groovy

Groovy even with `@CompileStatic` in most cases is slower than Java or Kotlin due to additional runtime checks and because of Closure which is much more expensive than Kotlin lambda.

Choosing between Kotlin vs. Groovy 2

Kotlin vs. Groovy Compared

Generally speaking, Kotlin and Groovy are both JVM languages and both are developer friendly. Their major difference lies not in syntax, but in compilation. Besides, notwithstanding their differences, they are total interoperable which indicates they can co-exist in the same application.

Some developers agree that Groovy is easier for quick scripts to do something simple or for testing, while Kotlin is a better choice to create something nontrivial without all the friction/ boilerplate of Java and for Android development.

Speaking about future prospective, Kotlin seems to already grant itself a place among Android languages, so mastering it might be a thoughtful decision.

Sciforce

WRITTEN BY

Sciforce

[ad_2]

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link