Groovy - Agile Java | TechWell

Groovy - Agile Java

Groovy

Groovy's recent surge in usage (TIOBE index ranking from 26 to 12 between Feb. 2020 and Feb. 2021) could be attributed to the many benefits the language provides. Groovy, like Java, is compiled to JVM (Java Virtual Machine) bytecode. 

Integrable With Java

Groovy 3 added several new features from Java including do/while loop, enhanced classic for loop, Java-style array initialization and lambda expression, method references, nested code blocks, var reserved type, and interface default methods. Groovy supports 99% of Java's syntax. 

Agile Syntax

Groovy offers several style and other features that makes the language Agile and easier to use. 

Semicolons (;) while useful to demarcate code sections could clutter a program. Groovy doesn't require ; to be declared after each statement. 

Too many parentheses in method calls? Groovy allows omitting parentheses on the arguments of method calls in a top-level statement. With Groovy just don’t specify those parentheses, and the method calls could be linked. As an example get(a).put(b).send(c) could be stated as get a put b send c

The return statement in Groovy may be omitted, and the last expression within the method's {} block becomes the return value. 

Simplification Features

Import Aliasing is one such feature. When importing the same-named class from different packages it becomes essential in Java to specify the fully qualified name of the classes. Groovy offers import aliasing using which an import statement may include the as keyword to alias a class such as import java.hello.Hello as Hello, and java.hi.Hello as HiHello.

Flexible exception handling in Groovy is a feature that is useful during application development. Java requires that the catch block of a try-catch statement declare the exception classes that it could catch. Well, it may not be known during development what exception could need to be caught. Groovy has just the fix; just use catch(any) to catch any Exception

POJOs are Plain Old Java Objects with some field declarations and getter/setter method declarations for the fields. Groovy's POGOs, or Plain Old Groovy Objects, simplify declaring an object  even further. Just declare a variable and the compiler generates the field, and getter/setters. As an example, a Groovy class with just one line of code String str1 compiles to bytecode with a field declaration for str1, and getter/setter methods to access the str1 field. 

Agile Typed

Groovy is optionally typed which provides some benefits over the strongly typed Java. In Groovy a method may be defined without an explicit return type or parameter types.  

Groovy is easier to use with true/false type comparisons because every Groovy object evaluates to true or false. If an object is not null, void, equal to 0, or empty it evaluates to true

Java's switch statement supports only a limited data type, and Groovy’s switch supports almost any type. 
 
"Simplicity" is one of the principles of the Agile Manifesto. These are only some of the features that Groovy offers to provide a more Agile Java.  

Tags: 

Up Next

September 15, 2021

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every week.