Understanding some of the Groovy language features by comparison with the Java language (and occasionally with JavaScript). This presentation explores closures, properties, some the Groovy language annotations and Groovy specifics such as Elvis operator, memoization etc.
6. 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
7. 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)
8. 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)
9. Closures are for Groovy what
lambda’s are for Java.
Except closures came first :-)
10. 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
11. 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!
12. 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
13. 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() { ….} })
14. 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
17. 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)
18. 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”]
19. Less is More: Strings
Supports ‘ “ and “””
● Bash style strings (GString :-o)
● Can use $, ${} or ${ ->} for closures
20. Less is More: “==“ == “equals”
Very intuitive :-)
● == instead of equals()
● is() instead of ==
21. 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
22. Less is More: .with
Use with a closure for which the object is the delegate
● Useful for assigning properties
● As well as invoking methods
23. Less is More: multiple assign
Shorter code
● Caveat: can be more difficult to understand variable types when mixing different
types in one assignment
24. Less is More: multiple
assignments and with
● This works with builders too! (as long as the object has setters)
25. Less is More: default params
Syntactic sugar
● … but it makes code more readable
● Yet concise!
26. 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
27. Less is More: Elvis
More syntactic sugar
● Upcoming version of Groovy also offers ?=
● Called “Elvis operator” because of this
28. 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
29. Less is More: Memoization
Automatic caching of methods and closures results
● Use @Memoized annotation
● Or invoke .memoize() to create proxy
30. Less is More: Spaceship
Shorthand for comparison (compareTo)
● Handles null values
● Labeled “Spaceship” because of … Star Wars :-)