Your SlideShare is downloading. ×
0
JVM. quack() or Groovy vs Ceremony
…Stas ShevchenkoJava Launch, 23/04/2013, Riga
Java Language comes with• Design Patterns• Boilerplate• Overly Ceremony code
1. Patterns…
Boilerplate
2. Boilerplate-   getter/setters-   Lazy init factories-   toString, hashCode, equals-   Explicit Exception declaration/ha...
Ceremony: Code’s Worst Enemy
Code Today (Death Star)               ESSENCE      CEREMONY
Future: Paradigm, Languages,         Frameworks CEREMONY            ESSENCE
3. Ceremony to Essence code. Step 0public ActionForward edit(ActionMapping mapping,                ActionForm form,       ...
Step 1. Duck Typingedit(mapping, form, request, response)    throws Exception {  personForm = form;  if (personForm.getId(...
Step 2. duck typing, without local                variableedit(mapping, form, request, response)    throws Exception {  if...
Step 3. Implicit return, exceptionsedit(mapping, form, request, response) {  if (form.getId() != null) {    mgr = getBean(...
Step 4. Dont add a manager layer to       MVC (yet). KISS + YAGNI.edit(mapping, form, request, response) {   if (form.getI...
Step 5. Conditionals make code            expensive to testedit(mapping, form, request, response) {   person = Person.find...
Step 6. All action methods have the        same four argumentsedit() {  person = Person.find(form.getId());  form = conver...
Step 7. Delegate object showing to                 formedit() {  person = Person.find(form.getId());  mapping.findForward(...
Step 8. Standard routingedit() {  person = Person.find(form.getId());}Or Rubydef edit  @person = Person.find(params[:id]) ...
Java WordIoCAspectsLombok or Eclipse XtendCode generators -> Spring Roo
JVM Languages    Clojure     Scala    Groovy     JRuby    Jython  Java Script
Clojure- Lisp – WTF?- By default Functional Programming only
Scala- Syntax WTF- Acclimatization period 6 to 12 months
JRuby- Ruby guys- gemns hell
Jython- Python syntax ((
Java Script (Rhino)- Is Java Script
Starting Groovy1. Download the .zip file from   http://groovy.codehaus.org and unzip it to   local drive2. Create the GROO...
Groovy Console
Primitives3.times { println it }assert (10 instanceof Integer)println 4.4.classString s = 10 as String;println s;
Groovy Beansclass Customer {   Integer id   def name   Date dob}def customer = new Customer(id:1, name:"Gromit",dob:new Da...
Collections – Lists, Rangesdef list = [5, 6, 7, 8]assert list[2] == 7assert list instanceof java.util.Listdef range = 5..8...
Collections - Mapdef map = [name:"Gromit", likes:"cheese",id:1234]assert map["name"] == "Gromit"assert map[id] == 1234asse...
Collections featuresassert [1, 3, 5] == [a, few, words]*.size()def words = [ant, buffalo, cat, dinosaur]assert words.findA...
Duck typing// Hey duckDuck myDuck = new Duck()
myDuck.quack()// Hey quackerdef duck = new Duck()
myDuck.quack()
Duck typing 2class Duck {   def quack() {println("quack")}}def action = "quack"def duck = new Duck()duck."${action}"()
Operatorsa == b     a.equals(b)a+b        a.plus(b)a-b        a.minus(b)a++        a.next()a << b     a.leftShift(b)def gr...
Closuresdef squareClosure = { value ->  value * value;}assert (4 == squareClosure(2))
IOdef file = new File(sms.txt).eachLine{ println it }file.write(”rewrite file”)file.append(“add to file”)
file << ”sexy st...
XMLdef xml = new groovy.xml.MarkupBuilder()xml.goods(type:”current”){  good(“Beer”)  good (“Rock&Roll”)  good (“Girls”)}
XMLdef goods = new XmlSlurper().parseText(…)def allGoods = records.nameassert 3 == allRecords.size()def allNodes = goods.d...
DSLThis is a really cool topic, where the stars arebegan…• A DSL allows expressions in a domain specific  manner• Method p...
Something to readGroovy++Grails -> GORMGradle
At the end – NPE fighter in my teaminfringementAction.setCustomsOffice( versionedReplyInf != null ? (versionedReplyInf.get...
At the end on GroovySafe navigation operator “?.”infringementAction.setCustomsOffice(versionedReplyInf?.getReplyInf()?.get...
Upcoming SlideShare
Loading in...5
×

Groovy vs Boilerplate and Ceremony Code

855

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
855
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Groovy vs Boilerplate and Ceremony Code"

  1. 1. JVM. quack() or Groovy vs Ceremony
  2. 2. …Stas ShevchenkoJava Launch, 23/04/2013, Riga
  3. 3. Java Language comes with• Design Patterns• Boilerplate• Overly Ceremony code
  4. 4. 1. Patterns…
  5. 5. Boilerplate
  6. 6. 2. Boilerplate- getter/setters- Lazy init factories- toString, hashCode, equals- Explicit Exception declaration/handling- Close for resources- synchronization
  7. 7. Ceremony: Code’s Worst Enemy
  8. 8. Code Today (Death Star) ESSENCE CEREMONY
  9. 9. Future: Paradigm, Languages, Frameworks CEREMONY ESSENCE
  10. 10. 3. Ceremony to Essence code. Step 0public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { PersonForm personForm = (PersonForm) form; if (personForm.getId() != null) { PersonManager mgr = (PersonManager) getBean("personManager"); Person person = mgr.getPerson(personForm.getId()); personForm = (PersonForm) convert(person); updateFormBean(mapping, request, personForm); } return mapping.findForward("edit"); }
  11. 11. Step 1. Duck Typingedit(mapping, form, request, response) throws Exception { personForm = form; if (personForm.getId() != null) { mgr = getBean("personManager"); person = mgr.getPerson(personForm.getId()); personForm = convert(person); updateFormBean(mapping, request, personForm); } return mapping.findForward("edit");}
  12. 12. Step 2. duck typing, without local variableedit(mapping, form, request, response) throws Exception { if (form.getId() != null) { mgr = getBean("personManager"); person = mgr.getPerson(form.getId()); form = convert(person); updateFormBean(mapping, request, form); } return mapping.findForward("edit");}
  13. 13. Step 3. Implicit return, exceptionsedit(mapping, form, request, response) { if (form.getId() != null) { mgr = getBean("personManager"); person = mgr.getPerson(form.getId()); form = convert(person); updateFormBean(mapping, request, form); } mapping.findForward("edit");}
  14. 14. Step 4. Dont add a manager layer to MVC (yet). KISS + YAGNI.edit(mapping, form, request, response) { if (form.getId() != null) { person = Person.find(form.getId()); form = convert(person); updateFormBean(mapping, request, form); } mapping.findForward("edit"); }
  15. 15. Step 5. Conditionals make code expensive to testedit(mapping, form, request, response) { person = Person.find(form.getId()); form = convert(person); updateFormBean(mapping, request, form); mapping.findForward("edit"); }
  16. 16. Step 6. All action methods have the same four argumentsedit() { person = Person.find(form.getId()); form = convert(person); updateFormBean(mapping, request, form); mapping.findForward("edit");}
  17. 17. Step 7. Delegate object showing to formedit() { person = Person.find(form.getId()); mapping.findForward("edit");}
  18. 18. Step 8. Standard routingedit() { person = Person.find(form.getId());}Or Rubydef edit @person = Person.find(params[:id]) end
  19. 19. Java WordIoCAspectsLombok or Eclipse XtendCode generators -> Spring Roo
  20. 20. JVM Languages Clojure Scala Groovy JRuby Jython Java Script
  21. 21. Clojure- Lisp – WTF?- By default Functional Programming only
  22. 22. Scala- Syntax WTF- Acclimatization period 6 to 12 months
  23. 23. JRuby- Ruby guys- gemns hell
  24. 24. Jython- Python syntax ((
  25. 25. Java Script (Rhino)- Is Java Script
  26. 26. Starting Groovy1. Download the .zip file from http://groovy.codehaus.org and unzip it to local drive2. Create the GROOVY_HOME environment variable and add $GROOVY_HOME/bin to you path3. type groovy -version
  27. 27. Groovy Console
  28. 28. Primitives3.times { println it }assert (10 instanceof Integer)println 4.4.classString s = 10 as String;println s;
  29. 29. Groovy Beansclass Customer { Integer id def name Date dob}def customer = new Customer(id:1, name:"Gromit",dob:new Date())
  30. 30. Collections – Lists, Rangesdef list = [5, 6, 7, 8]assert list[2] == 7assert list instanceof java.util.Listdef range = 5..8assert range.size() == 4assert range[2] == 7assert range instanceof java.util.List
  31. 31. Collections - Mapdef map = [name:"Gromit", likes:"cheese",id:1234]assert map["name"] == "Gromit"assert map[id] == 1234assert map instanceof java.util.Map
  32. 32. Collections featuresassert [1, 3, 5] == [a, few, words]*.size()def words = [ant, buffalo, cat, dinosaur]assert words.findAll{ w -> w.size() > 4 } == [buffalo,dinosaur]def words = [ant, buffalo, cat, dinosaur]assert words.collect{ it[0] } == [a, b, c, d]def sub = list[1, 3, 20..25, 33]assert sub == [101, 103, 120, 121, 122, 123, 124, 125, 133]
  33. 33. Duck typing// Hey duckDuck myDuck = new Duck()
myDuck.quack()// Hey quackerdef duck = new Duck()
myDuck.quack()
  34. 34. Duck typing 2class Duck { def quack() {println("quack")}}def action = "quack"def duck = new Duck()duck."${action}"()
  35. 35. Operatorsa == b a.equals(b)a+b a.plus(b)a-b a.minus(b)a++ a.next()a << b a.leftShift(b)def groovy = *”beer", ”rock&roll"]groovy << ”girls"
  36. 36. Closuresdef squareClosure = { value -> value * value;}assert (4 == squareClosure(2))
  37. 37. IOdef file = new File(sms.txt).eachLine{ println it }file.write(”rewrite file”)file.append(“add to file”)
file << ”sexy style"
  38. 38. XMLdef xml = new groovy.xml.MarkupBuilder()xml.goods(type:”current”){ good(“Beer”) good (“Rock&Roll”) good (“Girls”)}
  39. 39. XMLdef goods = new XmlSlurper().parseText(…)def allGoods = records.nameassert 3 == allRecords.size()def allNodes = goods.depthFirst().collect{ it }def firstGood = goods.name[0]assert ’sms’ == firstGood .name()assert ‘Text’ == firstGood.@make.text()
  40. 40. DSLThis is a really cool topic, where the stars arebegan…• A DSL allows expressions in a domain specific manner• Method pointers make this easy:
def list = []
def insert = list.&add
insert ”beer"
insert ”rock&roll"
  41. 41. Something to readGroovy++Grails -> GORMGradle
  42. 42. At the end – NPE fighter in my teaminfringementAction.setCustomsOffice( versionedReplyInf != null ? (versionedReplyInf.getReplyInf() != null ? (versionedReplyInf.getReplyInf().getInf() != null ? (versionedReplyInf.getReplyInf().getInf().getInf() != null ? (versionedReplyInf.getReplyInf().getInf().getInf(). getCustomsOffice() != null ? versionedReplyInf.getReplyInf().getInf().getInf(). getCustomsOffice() : null) : null) : null) : null) : null);
  43. 43. At the end on GroovySafe navigation operator “?.”infringementAction.setCustomsOffice(versionedReplyInf?.getReplyInf()?.getInf()?.getInf()?.getCustomsOffice())
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×