Groovy Syntax Sugar

In Teneo Studio the scripts are written in Groovy , which is a dynamic programming language and easier-to-learn than Java. Although you can use Java code in Teneo Studio in most of the cases , we encourage you to use Groovy which will make your code shorter and more readable. If you are a skilled Java developer but do not have much experience in Groovy, this post can give you some ideas about the syntax sugar Groovy provides.

Default imports

In Groovy, some of the commonly used packages and classes are imported by default, i.e. you do not have to use an explicit import statement to use them:

  • java.io.*
  • java.lang.*
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.net.*
  • java.util.*
  • groovy.lang.*
  • groovy.util.*

Omit the semicolons

In Java you must use a semicolon at the end of each line, while in Groovy the semicolon is optional. We suggest you to use semicolons only when you need to separate between statements within one line of code, e.g. for (int i = 0; i < myList.size(); i++)

Easy syntax for print() and println()

Print and Println are used to display a text on the console, which are probably most used methods in programming. In Java it is obligatory to use System.out.print() and System.out.println(), while in Groovy you can simply use print() and println(). The parentheses are optional, so both of the following code will work: print("Message") and print "Message".

Define variables with implicit type

When you are defining a variable in Java, you should define explicitly the data type such as String name = "Tommy". While in Groovy, you can define a variable without defining explicitly the data type by using the def keyword. The data type will be determined by the value of the variable, see the examples below:
image

These variables defined with implicit type can be easily switched to another data type by assigning a new value, for example:
image

Define array with square brackets

In Java you define an array using curly brackets, while in Groovy you need to use square brackets, for example int[] array = [1,2,3]. The long syntax for array in Java like int[] array = new int[]{1,2,3} is still valid in Groovy, but the short syntax like int[] array = {1,2,3} is not valid. If you want to copy your Java code into Teneo Studio, you need to replace these curly brackets with square brackets in array creation.

Alternatives for 3-statement For loops

Although the standard 3-statement For loop in Java is still valid in Groovy, Groovy provides you with many simpler alternatives. For example, you have the following loop in Java:

for(int i = 0; i <= 5, i++){
    System.out.print(i)
}

Below are four alternatives which will generate the same result as the 3-statement loop above:

  • Use groovy.lang.IntRange class, which is written as x…y, representing from x to y with both x and y included:
    for(i in 0..5){
        print(i)
    }
    
  • Use the upto method of Integer written as x.upto(y), where x and y are both integers and y must be greater than x. It will loop from x to y with both x and y included, for example:
    0.upto(5){
        print(it)
    }
    
  • Use the times method of Integer written as x.times(), where x should be an integer greater than 0. It will loop x times (i.e. from 0 to x-1), for example:
    6.times(){
        print(it)
    }
    
  • Use the step method of Integer written as x.step(y, z), where x, y and z are integers. It will loop from x to y with y excluded, taking a step of z, for example:
    0.step(6,1){
        print(it)
    }
    

Simple For-each loops

A For-each loop is a popular traversing technique to iterate over an array or arraylist. The following is a standard For-each loop in Java:

for (type var: array){
    statements with var;
}

While in Groovy, you can either use the same expressions or in a simpler, Groovy Syntax Sugar, way:

for (var in array){
    statements with var
}

Conclusion

This post provides you some examples of syntax sugar that Groovy provides compared to Java . You can find more differences between Java and Groovy here. Hope it can help when you are writing code in Teneo Studio!

1 Like

Hi Chunlin, great stuff!
Question, which groovy version is within Teneo Studio, 3 or 4 ? where we can see it .

Thanks
Enrique

1 Like

Hi @enrique_medina ,

since my colleague @chunlin.wang is currently on holiday, I’m leaving you here already the answer.
Teneo Studio is using Groovy 3 since the release of Teneo Studio version 6.0.
You can find the announcement here under Teneo Platform Telease 6.0: Reference documentation T6 | Teneo Developers and generally speaking an overview of all current dependencies and licenses can be found here: Dependencies and Licenses | Reference documentation | Teneo Developers
Hope this helps, and glad you enjoyed Chun-Lin’s code snippet! :slight_smile:
/Benjamin

Thanks Benjamin, is there a plan in the near future to upgrade to the groovy version 4 ?

my best regards,
Enrique

Hi @enrique_medina,

There are no plans at the moment to upgrade from Groovy 3 to 4, was there a particular feature of Groovy 4 you were interested in?

And for where you can see it - as Benjamin says it is available in the documentation - or if you want a more hands on option, you can use GroovySystem.version in a solution to output the version number

Roger

Thanks Roger,

I was asking bc I have installed the 4 version in my laptop… So I assume I have to consider when having to test code snippets…

regards,
Enrique