SlideShare uma empresa Scribd logo
1 de 31
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 :-)

Mais conteúdo relacionado

Mais procurados

JavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetJavaScript Object Oriented Programming Cheat Sheet
JavaScript Object Oriented Programming Cheat SheetHDR1001
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painSander Mak (@Sander_Mak)
 
Power Leveling your TypeScript
Power Leveling your TypeScriptPower Leveling your TypeScript
Power Leveling your TypeScriptOffirmo
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Divedavanum
 
Introduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviIntroduction to TypeScript by Winston Levi
Introduction to TypeScript by Winston LeviWinston Levi
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practicesIwan van der Kleijn
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for IntermediatesAnkit Agrawal
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponLaurent Duveau
 
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 TypeScriptGil Fink
 
JavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closuresJavaScript global object, execution contexts & closures
JavaScript global object, execution contexts & closuresHDR1001
 

Mais procurados (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
 

Destaque

Prototype Pattern
Prototype PatternPrototype Pattern
Prototype PatternIder Zheng
 
Recommendations for Building Machine Learning Software
Recommendations for Building Machine Learning SoftwareRecommendations for Building Machine Learning Software
Recommendations for Building Machine Learning SoftwareJustin 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 PerspectiveJustin Basilico
 
Learning a Personalized Homepage
Learning a Personalized HomepageLearning a Personalized Homepage
Learning a Personalized HomepageJustin 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/2017Liviu Tudor
 
Builder pattern vs constructor
Builder pattern vs constructorBuilder pattern vs constructor
Builder pattern vs constructorLiviu Tudor
 

Destaque (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
 

Semelhante a Groovy / comparison with java

Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with GroovyDhaval 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
 
go language- haseeb.pptx
go language- haseeb.pptxgo language- haseeb.pptx
go language- haseeb.pptxArsalanMaqsood1
 
Functional Groovy - Confess
Functional Groovy - ConfessFunctional Groovy - Confess
Functional Groovy - ConfessAndres Almiray
 
Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshopadam1davis
 
"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 NelsonGWTcon
 
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 developmentJayaprakash R
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonDomingo Suarez Torres
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageHoat Le
 
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 languageHossam 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 DummiesDarren Cruse
 

Semelhante a 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
 

Último

Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburgmasabamasaba
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 

Último (20)

Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

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 :-)

Notas do Editor

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