SlideShare a Scribd company logo
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 :-)

More Related Content

What's hot

JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
HDR1001
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScript
Offirmo
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
Ats Uiboupin
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
SadhanaParameswaran
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
davanum
 
Typescript
TypescriptTypescript
Typescript
Nikhil Thomas
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
Collaboration Technologies
 
TypeScript 2 in action
TypeScript 2 in actionTypeScript 2 in action
TypeScript 2 in action
Alexander Rusakov
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
Winston Levi
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
Iwan van der Kleijn
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
Ankit Agrawal
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
Aniruddha Chakrabarti
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
Ruby
RubyRuby
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
Rajat Pratap Singh
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
Gil Fink
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
akhilsreyas
 
JavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closuresJavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closures
HDR1001
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
Bhanuka Uyanage
 

What's hot (20)

JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat Sheet
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScript
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
 
Typescript
TypescriptTypescript
Typescript
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
 
TypeScript 2 in action
TypeScript 2 in actionTypeScript 2 in action
TypeScript 2 in action
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston Levi
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
Ruby
RubyRuby
Ruby
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
JavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closuresJavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closures
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 

Viewers also liked

Prototype Pattern
Prototype PatternPrototype Pattern
Prototype Pattern
Ider Zheng
 
Recommendations for Building Machine Learning Software
Recommendations for Building Machine Learning SoftwareRecommendations for Building Machine Learning Software
Recommendations for Building Machine Learning Software
Justin Basilico
 
Past, Present & Future of Recommender Systems: An Industry Perspective
Past, Present & Future of Recommender Systems: An Industry PerspectivePast, Present & Future of Recommender Systems: An Industry Perspective
Past, Present & Future of Recommender Systems: An Industry Perspective
Justin Basilico
 
Learning a Personalized Homepage
Learning a Personalized HomepageLearning a Personalized Homepage
Learning a Personalized Homepage
Justin Basilico
 
Ads personalization / Netflix Ad Tech Event Nov/2017
Ads personalization / Netflix Ad Tech Event Nov/2017Ads personalization / Netflix Ad Tech Event Nov/2017
Ads personalization / Netflix Ad Tech Event Nov/2017
Liviu Tudor
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructor
Liviu Tudor
 

Viewers also liked (6)

Prototype Pattern
Prototype PatternPrototype Pattern
Prototype Pattern
 
Recommendations for Building Machine Learning Software
Recommendations for Building Machine Learning SoftwareRecommendations for Building Machine Learning Software
Recommendations for Building Machine Learning Software
 
Past, Present & Future of Recommender Systems: An Industry Perspective
Past, Present & Future of Recommender Systems: An Industry PerspectivePast, Present & Future of Recommender Systems: An Industry Perspective
Past, Present & Future of Recommender Systems: An Industry Perspective
 
Learning a Personalized Homepage
Learning a Personalized HomepageLearning a Personalized Homepage
Learning a Personalized Homepage
 
Ads personalization / Netflix Ad Tech Event Nov/2017
Ads personalization / Netflix Ad Tech Event Nov/2017Ads personalization / Netflix Ad Tech Event Nov/2017
Ads personalization / Netflix Ad Tech Event Nov/2017
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructor
 

Similar to Groovy / comparison with java

Groovy intro
Groovy introGroovy intro
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
Dhaval Dalal
 
Little Did He Know ...
Little Did He Know ...Little Did He Know ...
Little Did He Know ...
Burt Beckwith
 
Groovy best pratices at EWAY
Groovy best pratices at EWAYGroovy best pratices at EWAY
Groovy best pratices at EWAY
Đào Hiệp
 
Groovy and noteworthy
Groovy and noteworthyGroovy and noteworthy
Groovy and noteworthy
Izzet Mustafaiev
 
go language- haseeb.pptx
go language- haseeb.pptxgo language- haseeb.pptx
go language- haseeb.pptx
ArsalanMaqsood1
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - Confess
Andres Almiray
 
Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshop
adam1davis
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson
GWTcon
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
Jayaprakash R
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
Domingo Suarez Torres
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageHoat Le
 
JavaScript Variables
JavaScript VariablesJavaScript Variables
JavaScript Variables
Charles Russell
 
API Design
API DesignAPI Design
API Design
Tim Boudreau
 
Groovy features
Groovy featuresGroovy features
Groovy features
Ramakrishna kapa
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...
Baltasar García Perez-Schofield
 
Groovy Metaprogramming for Dummies
Groovy Metaprogramming for DummiesGroovy Metaprogramming for Dummies
Groovy Metaprogramming for Dummies
Darren Cruse
 
Viva file
Viva fileViva file
Viva file
anupamasingh87
 

Similar to Groovy / comparison with java (20)

Groovy intro
Groovy introGroovy intro
Groovy intro
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Little Did He Know ...
Little Did He Know ...Little Did He Know ...
Little Did He Know ...
 
Groovy best pratices at EWAY
Groovy best pratices at EWAYGroovy best pratices at EWAY
Groovy best pratices at EWAY
 
Groovy and noteworthy
Groovy and noteworthyGroovy and noteworthy
Groovy and noteworthy
 
go language- haseeb.pptx
go language- haseeb.pptxgo language- haseeb.pptx
go language- haseeb.pptx
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - Confess
 
Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshop
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson
 
Exploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App developmentExploring Kotlin language basics for Android App development
Exploring Kotlin language basics for Android App development
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming Language
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
JavaScript Variables
JavaScript VariablesJavaScript Variables
JavaScript Variables
 
API Design
API DesignAPI Design
API Design
 
Groovy features
Groovy featuresGroovy features
Groovy features
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming languageSwift Tutorial Part 2. The complete guide for Swift programming language
Swift Tutorial Part 2. The complete guide for Swift programming language
 
Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...Post-graduate course: Object technology: Implementation of object-oriented pr...
Post-graduate course: Object technology: Implementation of object-oriented pr...
 
Groovy Metaprogramming for Dummies
Groovy Metaprogramming for DummiesGroovy Metaprogramming for Dummies
Groovy Metaprogramming for Dummies
 
Viva file
Viva fileViva file
Viva file
 

Recently uploaded

A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 

Recently uploaded (20)

A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 

Groovy / comparison with java

  • 1. Groovy by … comparison (with Java) Ads Engineering Liviu Tudor
  • 2. Because we use it with Gradle, Spock And some of our ETL’s Why Groovy?
  • 3. Contents. ● Classes / Variables / Properties (set/get) / Methods ● Closures (lambdas) ● How to write less code :-)
  • 4. Groovy is Java but more concise. (You can in fact write Java in all your Groovy classes and it will work!)
  • 5. Classes Similar to Java ● But no need for public – public by default. ● Or semi-colon. Ever!
  • 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
  • 15. Closures Used all the time in gradle :-) Project == delegate
  • 16. Groovy is Java but more concise. Less is more.
  • 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 :-)

Editor's Notes

  1. Last executed statement – if an early “if” statement causes exiting the method that return value will be returned