SlideShare a Scribd company logo
1 of 30
Download to read offline
Groovy as a
Dynamic Language
Darren Cruse
(with just a smidge of
metaprogramming)
Wednesday, April 22, 15
Agenda
• Has Groovy arrived?
(what do you guys think?)
• Groovy as a “Dynamic Language”
(groovy as part of the family of languages that includes
smalltalk, perl, javascript, ruby, etc)
• Some metaprogramming code examples:
Adding “Ruby style” new methods to groovy
(if time brief look at): Auto-generating UML sequence
diagrams from running a groovy script.
Wednesday, April 22, 15
Survey 1
Which is more important to your
company/projects:
a. Groovy as a language
b. Grails as a web framework
c. Both are equally important
Wednesday, April 22, 15
Survey 2
At my company we are using groovy:
a. Heavily as the primary language for a project.
b. Moderately as a complement to java.
c. Lightly for unit tests, throw away scripts, etc.
d. We are Evaluating groovy for possible future use.
e. We are Not Using Groovy.
Wednesday, April 22, 15
indeed.com
Wednesday, April 22, 15
indeed.com
Wednesday, April 22, 15
image from: http://whiteafrican.com/tag/crossing-the-chasm
Wednesday, April 22, 15
Ease of Adoption
(versus)
Benefits of Adoption
“Ease of
Adoption”
“Benefits of
Adoption”
“Sweet spot of
innovation”
Adapted From: The Myths of Innovation, by Scott Berkun
Wednesday, April 22, 15
Dynamic Languages
(are on my mind)
(Glenn Campbell song, 1973)
Wednesday, April 22, 15
What is a
“Dynamic Language”?
(Graeme Rocher and Bob Lee, from 2006)
http://graemerocher.blogspot.com/2006/03/groovy-beanshell-dynamic-vs-scripting.html
Wednesday, April 22, 15
What is a
“Dynamic Language”?
(Graeme Rocher and Bob Lee, from 2006)
http://graemerocher.blogspot.com/2006/03/groovy-beanshell-dynamic-vs-scripting.html
Wednesday, April 22, 15
“Dynamic Programming Language”
(according to Wikipedia)
“Dynamic Programming Language describes a class of high
level programming languages that execute at runtime
many common behaviors that other languages might
perform at compilation.”
Wednesday, April 22, 15
“Dynamic Programming Language”
(according to Wikipedia)
Most Dynamic Languages:
• Are Dynamically Typed
• Have functions as “first class citizens”
• Allow modifications to types at runtime
• Support eval
Wednesday, April 22, 15
“Dynamic Programming Language”
(according to Wikipedia)
And “Dynamically Typed” means:
“A programming language is dynamically typed when
the majority of type checking is performed at run-time
as opposed to compile time. In dynamic typing, values
have types but variables do not.”
Wednesday, April 22, 15
Some “Dynamic Languages”
(according to Wikipedia)
ActionScript
BeanShell*
Lisp
Groovy
JavaScript
Clojure VBScript
Matlab
Lua
Objective-C
Perl PHP
Python
Ruby
Smalltalk
Tcl
*I guess this means the bunny was right and wrong, at the very same time?
Wednesday, April 22, 15
Going Back in the Way-Back
Machine...
Wednesday, April 22, 15
Or: What I learned from Perl
(that I’ll admit to, and that served me well in
learning groovy, and javascript)
Back when I did C, I vaguely understood that among other
things a compiler lays out things so that generated machine
code knows the offset of fields within a structure.
This is what makes the generated code fast.
But it also makes it inflexible, because offsets are set in
stone at compile time.
Wednesday, April 22, 15
Statically Compiled Languages
(compile stuff into offsets that set like concrete)
struct student {
  int id;
  char *name;
  float percent;
};
offset field
+0 id
+4 name
+8 percent
C Code: Offsets used in generated code:
compiler
Wednesday, April 22, 15
But Perl had a very different
approach to classes and objects...
They were just a Map.
This seemed crude, but combined with first class functions,
and the speed of edit/test versus edit/compile/test - I grew
to like Perl over C for many tasks.
Wednesday, April 22, 15
$student = {
  id => 1,
  name => “Jason”,
percent => 100.0
};
key value
id 1
name Jason
percent 100.0
Perl Code:
Runtime data structure:
interpreter
Dynamic Languages
(classes in data structures you can manipulate at run time)
Wednesday, April 22, 15
I grew to feel...
Perl was to clay,
as C,
was to concrete.
Wednesday, April 22, 15
Modern languages still carry
these distinctions
(though a little blurrier)
Java is still a high performance compiled statically typed
language, but with more run-time-stuff than C had (e.g. the
Reflection api).
And I still relate to the metaprogramming possibilities of
Groovy (Javascript, Ruby, Python, etc) by thinking “oh
yeah, it’s basically just a Map”.
Wednesday, April 22, 15
Groovy is Special
(because it’s a hybrid)
Groovy combines a static language (java) and a Dynamic
(smalltalk/ruby/perl like) language into one.
Groovy’s classes wrap a Map-like soft and chewy
Dynamic Language coating - i.e. “Metaclass” -
around a hard candy Static Language core - i.e. a normal
java Class.
(it’s like an M&M but inside out)
Wednesday, April 22, 15
Java Class Core
(fast but not so flexible)
Groovy Metaclass Coating
(think map)
Wednesday, April 22, 15
FINALLY
(some code!)
(Glenn Campbell song, 1973)
Wednesday, April 22, 15
Exercise: Add “Factory New”
(or “Ruby Style New”)
Methods to Groovy
i.e. not:
def service = new ServiceClass();
but rather:
def service = ServiceInterface.new();
Wednesday, April 22, 15
Why?
•Some argue that the “new” keyword as practiced by
C, C++, and Java is flawed, e.g. see:
Java's new Considered Harmful
By Jonathan Amsterdam, April 01, 2002
http://drdobbs.com/184405016
•It’s an example of something that you can do pretty
easily in groovy that would be extremely difficult in
java.
•Esp. when you consider the result feels pretty much
like a native language feature to the programmer using
it.
Wednesday, April 22, 15
From “Java’s new Considered Harmful”
In a nutshell, the problem with
new is that it fails to encapsulate
object creation.
def service = new ServiceClass();
no encapsulation!
Wednesday, April 22, 15
So: let’s see if we can add “factory
new” methods to groovy classes.
Along with a smidge of what
Spring provides (e.g. configurable
singletons and prototypes)
Without using a container - just
groovy, from groovy scripts.
Wednesday, April 22, 15
Code samples for this talk
available at:
https://github.com/darrencruse/groovy-metaprogramming-samples
Wednesday, April 22, 15

More Related Content

What's hot

What's hot (20)

An introduction to go programming language
An introduction to go programming languageAn introduction to go programming language
An introduction to go programming language
 
Go lang
Go langGo lang
Go lang
 
Go language presentation
Go language presentationGo language presentation
Go language presentation
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Phpactor and VIM
Phpactor and VIMPhpactor and VIM
Phpactor and VIM
 
Google Go! language
Google Go! languageGoogle Go! language
Google Go! language
 
Golang
GolangGolang
Golang
 
Why you should care about Go (Golang)
Why you should care about Go (Golang)Why you should care about Go (Golang)
Why you should care about Go (Golang)
 
Introduction to Go-Lang
Introduction to Go-LangIntroduction to Go-Lang
Introduction to Go-Lang
 
Growing up new PostgreSQL developers (pgcon.org 2018)
Growing up new PostgreSQL developers (pgcon.org 2018)Growing up new PostgreSQL developers (pgcon.org 2018)
Growing up new PostgreSQL developers (pgcon.org 2018)
 
Quality Assurance in PostgreSQL
Quality Assurance in PostgreSQLQuality Assurance in PostgreSQL
Quality Assurance in PostgreSQL
 
Protocol buffers
Protocol buffersProtocol buffers
Protocol buffers
 
Golang #5: To Go or not to Go
Golang #5: To Go or not to GoGolang #5: To Go or not to Go
Golang #5: To Go or not to Go
 
Golang from Scala developer’s perspective
Golang from Scala developer’s perspectiveGolang from Scala developer’s perspective
Golang from Scala developer’s perspective
 
Stockholm JAM September 2018
Stockholm JAM September 2018Stockholm JAM September 2018
Stockholm JAM September 2018
 
The Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventureThe Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventure
 
Adopting language server for apache camel feedback from a java/Eclipse plugi...
Adopting language server for apache camel  feedback from a java/Eclipse plugi...Adopting language server for apache camel  feedback from a java/Eclipse plugi...
Adopting language server for apache camel feedback from a java/Eclipse plugi...
 
Golang workshop - Mindbowser
Golang workshop - MindbowserGolang workshop - Mindbowser
Golang workshop - Mindbowser
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go lang
 
Advantages of Python Learning | Why Python
Advantages of Python Learning | Why PythonAdvantages of Python Learning | Why Python
Advantages of Python Learning | Why Python
 

Similar to Groovy as a Dynamic Language

Intro To Ror
Intro To RorIntro To Ror
Intro To Ror
myuser
 
Unit 4 Assignment 1 Comparative Study Of Programming...
Unit 4 Assignment 1 Comparative Study Of Programming...Unit 4 Assignment 1 Comparative Study Of Programming...
Unit 4 Assignment 1 Comparative Study Of Programming...
Carmen Sanborn
 
Glenn Vanderburg — Learning to love JavaScript
Glenn Vanderburg — Learning to love JavaScriptGlenn Vanderburg — Learning to love JavaScript
Glenn Vanderburg — Learning to love JavaScript
atr2006
 

Similar to Groovy as a Dynamic Language (20)

Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Languages used by web app development services remotestac x
Languages used by web app development services  remotestac xLanguages used by web app development services  remotestac x
Languages used by web app development services remotestac x
 
Groovy And Grails
Groovy And GrailsGroovy And Grails
Groovy And Grails
 
Intro To Ror
Intro To RorIntro To Ror
Intro To Ror
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Lets Go - An introduction to Google's Go Programming Language
Lets Go - An introduction to Google's Go Programming Language Lets Go - An introduction to Google's Go Programming Language
Lets Go - An introduction to Google's Go Programming Language
 
Why don't you Groovy?
Why don't you Groovy?Why don't you Groovy?
Why don't you Groovy?
 
Unit 4 Assignment 1 Comparative Study Of Programming...
Unit 4 Assignment 1 Comparative Study Of Programming...Unit 4 Assignment 1 Comparative Study Of Programming...
Unit 4 Assignment 1 Comparative Study Of Programming...
 
Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008
 
Introduction to Go
Introduction to GoIntroduction to Go
Introduction to Go
 
Glenn Vanderburg — Learning to love JavaScript
Glenn Vanderburg — Learning to love JavaScriptGlenn Vanderburg — Learning to love JavaScript
Glenn Vanderburg — Learning to love JavaScript
 
Nairuby metaprogramming with ruby
Nairuby metaprogramming with rubyNairuby metaprogramming with ruby
Nairuby metaprogramming with ruby
 
Ruby
RubyRuby
Ruby
 
Ruby
RubyRuby
Ruby
 
Groovy
GroovyGroovy
Groovy
 
Java for C++ programers
Java for C++ programersJava for C++ programers
Java for C++ programers
 
Which programming language should you learn next?
Which programming language should you learn next? Which programming language should you learn next?
Which programming language should you learn next?
 
Inheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth VersionInheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth Version
 
What would your own version of Ruby look like? (RubyKaigi)
What would your own version of Ruby look like? (RubyKaigi)What would your own version of Ruby look like? (RubyKaigi)
What would your own version of Ruby look like? (RubyKaigi)
 
Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
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
VictorSzoltysek
 

Recently uploaded (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
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...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS 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 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
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
 

Groovy as a Dynamic Language

  • 1. Groovy as a Dynamic Language Darren Cruse (with just a smidge of metaprogramming) Wednesday, April 22, 15
  • 2. Agenda • Has Groovy arrived? (what do you guys think?) • Groovy as a “Dynamic Language” (groovy as part of the family of languages that includes smalltalk, perl, javascript, ruby, etc) • Some metaprogramming code examples: Adding “Ruby style” new methods to groovy (if time brief look at): Auto-generating UML sequence diagrams from running a groovy script. Wednesday, April 22, 15
  • 3. Survey 1 Which is more important to your company/projects: a. Groovy as a language b. Grails as a web framework c. Both are equally important Wednesday, April 22, 15
  • 4. Survey 2 At my company we are using groovy: a. Heavily as the primary language for a project. b. Moderately as a complement to java. c. Lightly for unit tests, throw away scripts, etc. d. We are Evaluating groovy for possible future use. e. We are Not Using Groovy. Wednesday, April 22, 15
  • 8. Ease of Adoption (versus) Benefits of Adoption “Ease of Adoption” “Benefits of Adoption” “Sweet spot of innovation” Adapted From: The Myths of Innovation, by Scott Berkun Wednesday, April 22, 15
  • 9. Dynamic Languages (are on my mind) (Glenn Campbell song, 1973) Wednesday, April 22, 15
  • 10. What is a “Dynamic Language”? (Graeme Rocher and Bob Lee, from 2006) http://graemerocher.blogspot.com/2006/03/groovy-beanshell-dynamic-vs-scripting.html Wednesday, April 22, 15
  • 11. What is a “Dynamic Language”? (Graeme Rocher and Bob Lee, from 2006) http://graemerocher.blogspot.com/2006/03/groovy-beanshell-dynamic-vs-scripting.html Wednesday, April 22, 15
  • 12. “Dynamic Programming Language” (according to Wikipedia) “Dynamic Programming Language describes a class of high level programming languages that execute at runtime many common behaviors that other languages might perform at compilation.” Wednesday, April 22, 15
  • 13. “Dynamic Programming Language” (according to Wikipedia) Most Dynamic Languages: • Are Dynamically Typed • Have functions as “first class citizens” • Allow modifications to types at runtime • Support eval Wednesday, April 22, 15
  • 14. “Dynamic Programming Language” (according to Wikipedia) And “Dynamically Typed” means: “A programming language is dynamically typed when the majority of type checking is performed at run-time as opposed to compile time. In dynamic typing, values have types but variables do not.” Wednesday, April 22, 15
  • 15. Some “Dynamic Languages” (according to Wikipedia) ActionScript BeanShell* Lisp Groovy JavaScript Clojure VBScript Matlab Lua Objective-C Perl PHP Python Ruby Smalltalk Tcl *I guess this means the bunny was right and wrong, at the very same time? Wednesday, April 22, 15
  • 16. Going Back in the Way-Back Machine... Wednesday, April 22, 15
  • 17. Or: What I learned from Perl (that I’ll admit to, and that served me well in learning groovy, and javascript) Back when I did C, I vaguely understood that among other things a compiler lays out things so that generated machine code knows the offset of fields within a structure. This is what makes the generated code fast. But it also makes it inflexible, because offsets are set in stone at compile time. Wednesday, April 22, 15
  • 18. Statically Compiled Languages (compile stuff into offsets that set like concrete) struct student {   int id;   char *name;   float percent; }; offset field +0 id +4 name +8 percent C Code: Offsets used in generated code: compiler Wednesday, April 22, 15
  • 19. But Perl had a very different approach to classes and objects... They were just a Map. This seemed crude, but combined with first class functions, and the speed of edit/test versus edit/compile/test - I grew to like Perl over C for many tasks. Wednesday, April 22, 15
  • 20. $student = {   id => 1,   name => “Jason”, percent => 100.0 }; key value id 1 name Jason percent 100.0 Perl Code: Runtime data structure: interpreter Dynamic Languages (classes in data structures you can manipulate at run time) Wednesday, April 22, 15
  • 21. I grew to feel... Perl was to clay, as C, was to concrete. Wednesday, April 22, 15
  • 22. Modern languages still carry these distinctions (though a little blurrier) Java is still a high performance compiled statically typed language, but with more run-time-stuff than C had (e.g. the Reflection api). And I still relate to the metaprogramming possibilities of Groovy (Javascript, Ruby, Python, etc) by thinking “oh yeah, it’s basically just a Map”. Wednesday, April 22, 15
  • 23. Groovy is Special (because it’s a hybrid) Groovy combines a static language (java) and a Dynamic (smalltalk/ruby/perl like) language into one. Groovy’s classes wrap a Map-like soft and chewy Dynamic Language coating - i.e. “Metaclass” - around a hard candy Static Language core - i.e. a normal java Class. (it’s like an M&M but inside out) Wednesday, April 22, 15
  • 24. Java Class Core (fast but not so flexible) Groovy Metaclass Coating (think map) Wednesday, April 22, 15
  • 25. FINALLY (some code!) (Glenn Campbell song, 1973) Wednesday, April 22, 15
  • 26. Exercise: Add “Factory New” (or “Ruby Style New”) Methods to Groovy i.e. not: def service = new ServiceClass(); but rather: def service = ServiceInterface.new(); Wednesday, April 22, 15
  • 27. Why? •Some argue that the “new” keyword as practiced by C, C++, and Java is flawed, e.g. see: Java's new Considered Harmful By Jonathan Amsterdam, April 01, 2002 http://drdobbs.com/184405016 •It’s an example of something that you can do pretty easily in groovy that would be extremely difficult in java. •Esp. when you consider the result feels pretty much like a native language feature to the programmer using it. Wednesday, April 22, 15
  • 28. From “Java’s new Considered Harmful” In a nutshell, the problem with new is that it fails to encapsulate object creation. def service = new ServiceClass(); no encapsulation! Wednesday, April 22, 15
  • 29. So: let’s see if we can add “factory new” methods to groovy classes. Along with a smidge of what Spring provides (e.g. configurable singletons and prototypes) Without using a container - just groovy, from groovy scripts. Wednesday, April 22, 15
  • 30. Code samples for this talk available at: https://github.com/darrencruse/groovy-metaprogramming-samples Wednesday, April 22, 15

Editor's Notes

  1. Can anybody name this muppet character? Dr. Bunsen Honeydew
  2. Honestly the first thing is really me truly wanting to hear from you guys what you think about groovy adoption. Are your company’s using it? I did some snooping on the net and honestly was a little discouraged. That TIOBE language popularity index had it pretty far down. Then my buddy interviewed at Enterprise Rent-A-Car and he tells me they’re really big on groovy. So I’m confused. Then the second thing relates partly to the fact that I’ve been working on a server-side javascript project recently and though I think it could as easily have used groovy, I’m not unhappy I’ve been thinking a lot about the similarity between these languages and feeling like my knowledge of groovy transfers to javascript and vice versa. Then the last thing is using one of the code examples I’d done for the talk I gave here on metaprogramming a year ago when I’d not thought to bring my DVI to VGA adapter and so hadn’t gotten to show any code.
  3. My real reason for putting this slide in is in the past year I’ve come to appreciate more how much I’m an early adopter. And how I’ve tended to assume other people are like me when really I think this graph gets it right. The innovators and early adopters are a minority. I’d assumed that groovy would have exploded in popularity and have been much closer to java in popularity by now.
  4. This is from a book I thought was really neat I’d recommend it if you feel you’re an early adopter and are interested in why cool stuff often doesn’t catch on faster. The book is mainly talking about the people side of why things don’t catch on. I included this diagram because I feel it’s pointing to something groovy does better than it’s competitors like Ruby. After all groovy’s syntax is so close to java it makes learning it pretty easy. The “Ease of Adoption” seems pretty good to me. Yet it’s not completely backward compatible you can’t *really* copy and paste java as groovy like you can beanshell. And I’m not sure maybe for managers and executives who aren’t programmer types the “benefits of adoption” part still isn’t clear. I loved Jack’s talk where he showed all those examples of how groovy cuts the lines of code compared to java. Yet: At my company we use a lot of contractors so it seems using a well known language where there are lots of inexpensive programmers available seems a high priority. And I wonder if managers would have warmed quicker to a name like “java++” or something than “groovy”. But the curve is one the rise so hope I’m not sounding negative. For me it’s just been a dose of reality and not being *unrealistic* about the rate of adopting new technologies.
  5. I’ve started on a project incorporating server-side javascript (Rhino) on a Spring/Hibernate project with a team of java developers. We’ve been discussing the benefits of doing so (and whether we should!). I’ve noticed the similarities (and some differences) between Rhino and Groovy. And this got me thinking back even to my Perl days and how I never thought much about they’re being a family of “Dynamic Languages”. How do they differ from static languages, what are their strengths (why do I seem to like them?)
  6. So this is the actual text from the comments on Graeme Rocher’s blog post from 2006. In the comments Graeme and Bob Lee started having an argument over Groovy and BeanShell and the term “Dynamic Language”. Graeme is project lead for Grails. And Bob Lee is the creator of the Guice IOC container among other things. Both are absolutely brilliant. And I think there’s really interesting things said in the text. Yet the discussion becomes a “Jane you ignorant slut!” type of argument when I read it I thought it was really funny. It runs on for about 8 minutes I’m not sure we can afford the time if you guys feel it’s taking too long why don’t you give me a signal and if several of you do I’ll stop it early and just make sure to post the link on the google groups site.
  7. These are the dynamic languages the wikipedia page on Dynamic Languages lists. The ones available on the JVM I put in white (unless I’ve missed some?) I noted that BeanShell is listed. So when Bob Lee argued against the term “Dynamic Language” - at least he was wrong according to the guys who made this wikipedia page. Yet Graeme was arguing Groovy and Ruby have features that make them a “Dynamic Language” and BeanShell didn’t. But BeanShell’s listed here! So they were both wrong and right at the same time?!
  8. This thing about edit/test/edit/test just hit refresh kind of thing... Versus edit/compile/test/edit/compile/test. It’s funny even with the advances in these IDEs with auto-compile and (supposed) hot deployment and stuff. I *still* feel slowed down too often having to restart my server or things (we use RAD and WebSphere). Compared to something like groovy or javascript where you just edit and hit refresh on your browser. 100 times a day quick quick quick. To this is the “usability” aspect of these languages that is hard for non-programmers to appreciate.
  9. So I actually resisted Perl in the beginning and then ultimately wound up feeling it was really neat. But then when I joined my current company 8 years ago I was really disappointed when I heard the development head say their goal was to replace all their Perl with java. Not because I’m against java, but rather because I saw these languages as complementary. I’d come to feel Perl brought something to the table. As does groovy. I’m getting here not just at metaprogramming but at the idea that the quick edit/run/edit/run style of development makes for a more agile development style. Where you can dive in and evolve the design as you go. But maybe that’s not fair: I do sometimes love the refactoring tools in java. But I think it’s also the brevity of the code, and the power that first class functions bring. That makes me feel more productive.
  10. Do we have some javascript people here? I didn’t do much javascript for a long time but I got deeply into it a few years ago doing Dojo web 2.0 stuff. And I remember how the Perl experience helped me relate to how prototypes work as runtime data structures. And again - groovy is similar - if not in the details at least in the broad “model” for how to think about things.
  11. I’ve started on a project incorporating server-side javascript (Rhino) on a Spring/Hibernate project with a team of java developers. We’ve been discussing the benefits of doing so (and whether we should!). I’ve noticed the similarities (and some differences) between Rhino and Groovy. And this got me thinking back even to my Perl days and how I never thought much about they’re being a family of “Dynamic Languages”. How do they differ from static languages, what are their strengths (why do I seem to like them?)
  12. How do you mock ServiceClass? The various factory patterns are all about working around this problem. At NFJS I remember the first time I heard (from Dave Thomas I think) this idea that “Design Patterns are workarounds for language limitations”)