groovy / comparison with java

31
Groovy by comparison (with Java) Ads Engineering Liviu Tudor

Upload: liviu-tudor

Post on 22-Jan-2018

185 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Groovy / comparison with java

Groovy by … comparison(with Java)

Ads Engineering

Liviu Tudor

Page 2: Groovy / comparison with java

Because we use it with Gradle, Spock

And some of our ETL’s

Why

Groovy?

Page 3: Groovy / comparison with java

Contents.

● Classes / Variables / Properties (set/get) / Methods

● Closures (lambdas)

● How to write less code :-)

Page 4: Groovy / comparison with java

Groovy is Java but more concise.

(You can in fact write Java in all

your Groovy classes and it will

work!)

Page 5: Groovy / comparison with java

Classes

Similar to Java

● But no need for public – public by default.

● Or semi-colon. Ever!

Page 6: Groovy / comparison with java

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

Page 7: Groovy / comparison with java

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)

Page 8: Groovy / comparison with java

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)

Page 9: Groovy / comparison with java

Closures are for Groovy what

lambda’s are for Java.

Except closures came first :-)

Page 10: Groovy / comparison with java

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

Page 11: Groovy / comparison with java

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!

Page 12: Groovy / comparison with java

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

Page 13: Groovy / comparison with java

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() { ….} })

Page 14: Groovy / comparison with java

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

Page 15: Groovy / comparison with java

Closures

Used all the time in gradle :-)

Project == delegate

Page 16: Groovy / comparison with java

Groovy is Java but more concise.

Less is more.

Page 17: Groovy / comparison with java

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)

Page 18: Groovy / comparison with java

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”]

Page 19: Groovy / comparison with java

Less is More: Strings

Supports ‘ “ and “””

● Bash style strings (GString :-o)

● Can use $, ${} or ${ ->} for closures

Page 20: Groovy / comparison with java

Less is More: “==“ == “equals”

Very intuitive :-)

● == instead of equals()

● is() instead of ==

Page 21: Groovy / comparison with java

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

Page 22: Groovy / comparison with java

Less is More: .with

Use with a closure for which the object is the delegate

● Useful for assigning properties

● As well as invoking methods

Page 23: Groovy / comparison with java

Less is More: multiple assign

Shorter code

● Caveat: can be more difficult to understand variable types when mixing different

types in one assignment

Page 24: Groovy / comparison with java

Less is More: multiple

assignments and with

● This works with builders too! (as long as the object has setters)

Page 25: Groovy / comparison with java

Less is More: default params

Syntactic sugar

● … but it makes code more readable

● Yet concise!

Page 26: Groovy / comparison with java

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

Page 27: Groovy / comparison with java

Less is More: Elvis

More syntactic sugar

● Upcoming version of Groovy also offers ?=

● Called “Elvis operator” because of this

Page 28: Groovy / comparison with java

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

Page 29: Groovy / comparison with java

Less is More: Memoization

Automatic caching of methods and closures results

● Use @Memoized annotation

● Or invoke .memoize() to create proxy

Page 30: Groovy / comparison with java

Less is More: Spaceship

Shorthand for comparison (compareTo)

● Handles null values

● Labeled “Spaceship” because of … Star Wars :-)

Page 31: Groovy / comparison with java

System.exit 0 :-)