Post on 22-Jan-2018
transcript
Groovy by … comparison(with Java)
Ads Engineering
Liviu Tudor
Because we use it with Gradle, Spock
And some of our ETL’s
Why
Groovy?
Contents.
● Classes / Variables / Properties (set/get) / Methods
● Closures (lambdas)
● How to write less code :-)
Groovy is Java but more concise.
(You can in fact write Java in all
your Groovy classes and it will
work!)
Classes
Similar to Java
● But no need for public – public by default.
● Or semi-colon. Ever!
Variables
Similar to Java
● No need for type declaration (though you can) – figured out by compiler when you
use def
● Strings can use both ‘ and “
● String expansion bash-style
Variables
Generated by the compiler (not the IDE)
● public by default
● Concise
● Can use with def – and let the compiler figure out the type (great for when
refactoring: no need to go and change method signatures or usage)
Methods
Similar to java
● public by default
● No need for return keyword: return result of last executed statement in method
● Don’t need return type, can use instead def – and let the compiler figure out the
type (great for when refactoring: no need to go and change method signatures or
usage)
Closures are for Groovy what
lambda’s are for Java.
Except closures came first :-)
Closures
Similar to java
● Declare the parameters in between { and -> e.g. { a-> , { a, b, c, -> etc
● Can omit the variable name(s) in which case “it“ is implied
● Can use a closure wherever a Java lambda would work
Closures
Similar to java
● Declare the parameters in between { and -> e.g. { a-> , { a, b, c, -> etc
● Can omit the variable name(s) in which case ”it“ is implied
● Can use a closure wherever a Java lambda would work
Don’t need System.out either!
Closures
Special syntax when used as the last parameter in a method
● Brackets in Groovy are optional for method calls
● (Though needed for function calls where you need the results!)
● Allows for closure to be extracted outside the list of parameters for more readable
code
Closures
Can be used as an object
● (Similar to JavaScript)
● Can do that in Java too but you would have to declare an anonymous inner class
(e.g. new Runnable() { public void run() { ….} })
Closures
Closure code is executed against a delegate
● By default the delegate is the owner of the closure
● But can be changed via the .delegate property
Closures
Used all the time in gradle :-)
Project == delegate
Groovy is Java but more concise.
Less is more.
Less is More: Properties
● All members are properties by default (auto-generated get/set)
● @TupleConstructor
● @EqualsAndHashCode
● @ToString
● (Can achieve similar in Java via the likes of Project Lombok)
Less is More: Properties and
Maps
● Similar syntax for accessing data in maps and properties
● Can use the dot notation .propertyName
● Or the indexed notation [“propertyName”]
Less is More: Strings
Supports ‘ “ and “””
● Bash style strings (GString :-o)
● Can use $, ${} or ${ ->} for closures
Less is More: “==“ == “equals”
Very intuitive :-)
● == instead of equals()
● is() instead of ==
Less is More: Object init
Useful for classes with setters but not builders/constructors which can take all properties
in one go
● Caveat: doesn’t work with builders
Less is More: .with
Use with a closure for which the object is the delegate
● Useful for assigning properties
● As well as invoking methods
Less is More: multiple assign
Shorter code
● Caveat: can be more difficult to understand variable types when mixing different
types in one assignment
Less is More: multiple
assignments and with
● This works with builders too! (as long as the object has setters)
Less is More: default params
Syntactic sugar
● … but it makes code more readable
● Yet concise!
Less is More: Groovy truth
Similar to JavaScript, Groovy evaluates objects as “truthy” if they are
● Non-null
● non-empty (for arrays, collections and strings)
● Non-zero numbers
● Matching regex
● etc
Less is More: Elvis
More syntactic sugar
● Upcoming version of Groovy also offers ?=
● Called “Elvis operator” because of this
Less is More: Safe dereference
Easy object traversal
● Returns null if any of the objects in the chain is null
● Without throwing a NullPointerException !
null if f is null null if
parentFile is
null
null if path is
null
Less is More: Memoization
Automatic caching of methods and closures results
● Use @Memoized annotation
● Or invoke .memoize() to create proxy
Less is More: Spaceship
Shorthand for comparison (compareTo)
● Handles null values
● Labeled “Spaceship” because of … Star Wars :-)
System.exit 0 :-)