v
Groovy in production
Đào Văn Hiệp - 2018/08/10
Các vấn đề hiện tại?
Agenda:
- What is Groovy?
- How Groovy work?
- Why Groovy?
- Groovy vs Java
- Best practice with Groovy in production
Let's Go!
What is groovy?
Groovy is
A feature-rich
Java-friendly
Dynamic language *
For the Java platform
=> A super version of Java
What is a dynamic language?
A language that allows the types of variables to be changed at
runtime...
...among other things!
Difference between dynamic vs static language?
Groovy History
• 2003: Started by James Strachan and Bob McWhirter.
• 2004: Commissioned into JSR 241 but was almost abandoned.
• 2005: Brought back to life by Guillaume Laforge and Jeremy
• Rayner.
• 2007: Groovy 1.0.
• 2012: Groovy 2.0.
• 2014: Groovy 2.3 (official support for JDK 8)
• Now : Groovy 2.5 (Stable version)
Who use Groovy?
http://groovy-lang.org/
How Groovy work?
- Groovy runs in the JVM.
- GDK extend JDK
=> Groovy is syntactically aligned with Java.
- Compile to Bytecode like Java
Groovy Pos (vs. Java)
• Easy to learn
• Productivity (More concise and
meaningful code - Funny typing)
• Can call on Java classes. (Coding
link Java)
• Faster in develop, testing
• Addtional operators
• Mix Functional with Object Orient
Programming (Closure)
Groovy Cons (vs. Java)
• Nhiều lỗi tiềm ẩn với dynamic
coding: sai kiểu dữ liệu,....
• IDE không nhắc handle
exception
• Slower than Java
Demo
Groovy Features
• Closures
• Collection literals
• Properties (GroovyBeans or POGO)
• Multi-line strings
• String interpolation
• Default arguments
• Everything’s an Object (even numbers)
• Operator overloading
• GPath expressions
• Additional operators
• And much much more...
Closures are anonymous functions that:
o may accept parameters or return a value,
o can be assigned to variables,
o can be passed as arguments,
Demo
Groovy Closure
A both can take params, return value & do something, but...
Lambda expressions:
Language facility for implementing functional interfaces
• Interfaces with a single abstract method.
• Function, Predicate, Supplier, Consumer.
Groovy closures:
• Can be assign as variable
• Can be reference to default param by "it"
• Use like Functional Programming
Groovy Closure <> Java 8 Lambda
Groovy best practices
"def" keyword
Người đọc không hiểu dữ liệu trả về.
(Đặc biệt với đoạn dài)
=> Gây khó khi đọc hiểu & maintain
Don't use "if" with Object
Groovy truth and equals
• Null value is false.
• Empty collection or map is false.
• Empty string is false.
• Zero value is false.
Best pratices:
- Chỉ nên sử dụng với Boolean
- Các trường hợp dùng với Type Number
rất dễ nhầm
- Các trường hợp check List/String nên
check cả null
(StringUtils.notBlank()/CollectionUtils.notEmpty())
Don't use "if" with Object
• GString. (Double quote: "This is GString: ${1 + 1}")
Can be calculate, evaluate method....
• String.
• Single quote: 'This is String'
• Double quote: "This is String" - Like Java String
=> Don't use GString when unnecessary (String constant)
String vs GString
Use multiple line
String vs GString
"?:" operator
=> Hữu dụng khi dùng để set default
value
Vấn đề:
- Các trường hợp String blank hoặc Number
value 0
- KQ: 10 & 10​
Null Safe operator
With Null safe
Without Null safe
with() method
Check condition
Set multiple value
Use like builder
Collection In Groovy
• Find/Filter
• Map
• List to Map
• Grep (exclude null, empty elements)
Format Map, Json
Before
After
• Use operator : '==' (compare String), '=~' (Check regular/Contains)
• Operator: '+', '-' (add, remove with List)
• Parse Date: Date.parse(...), Date.format(...)
Groovy best practices (Continues)
• Annotations:
• @InheritConstructors
• @TupleConstructor
• @Slf4j
• @CompileStatic
• @Singleton
Groovy best practices (Continues)
• Groovy 2 Cookbook
• Programming Groovy 2
• Blog: http://mrhaki.blogspot.com
• Groovy Web Site: http://groovy.codehaus.org
How to start with Groovy?
Groovy best pratices at EWAY

Groovy best pratices at EWAY

  • 1.
    v Groovy in production ĐàoVăn Hiệp - 2018/08/10
  • 2.
    Các vấn đềhiện tại?
  • 3.
    Agenda: - What isGroovy? - How Groovy work? - Why Groovy? - Groovy vs Java - Best practice with Groovy in production
  • 4.
  • 5.
    What is groovy? Groovyis A feature-rich Java-friendly Dynamic language * For the Java platform => A super version of Java
  • 6.
    What is adynamic language? A language that allows the types of variables to be changed at runtime... ...among other things! Difference between dynamic vs static language?
  • 7.
    Groovy History • 2003:Started by James Strachan and Bob McWhirter. • 2004: Commissioned into JSR 241 but was almost abandoned. • 2005: Brought back to life by Guillaume Laforge and Jeremy • Rayner. • 2007: Groovy 1.0. • 2012: Groovy 2.0. • 2014: Groovy 2.3 (official support for JDK 8) • Now : Groovy 2.5 (Stable version)
  • 8.
  • 9.
    How Groovy work? -Groovy runs in the JVM. - GDK extend JDK => Groovy is syntactically aligned with Java. - Compile to Bytecode like Java
  • 10.
    Groovy Pos (vs.Java) • Easy to learn • Productivity (More concise and meaningful code - Funny typing) • Can call on Java classes. (Coding link Java) • Faster in develop, testing • Addtional operators • Mix Functional with Object Orient Programming (Closure)
  • 11.
    Groovy Cons (vs.Java) • Nhiều lỗi tiềm ẩn với dynamic coding: sai kiểu dữ liệu,.... • IDE không nhắc handle exception • Slower than Java
  • 12.
  • 13.
    Groovy Features • Closures •Collection literals • Properties (GroovyBeans or POGO) • Multi-line strings • String interpolation • Default arguments • Everything’s an Object (even numbers) • Operator overloading • GPath expressions • Additional operators • And much much more...
  • 14.
    Closures are anonymousfunctions that: o may accept parameters or return a value, o can be assigned to variables, o can be passed as arguments, Demo Groovy Closure
  • 15.
    A both cantake params, return value & do something, but... Lambda expressions: Language facility for implementing functional interfaces • Interfaces with a single abstract method. • Function, Predicate, Supplier, Consumer. Groovy closures: • Can be assign as variable • Can be reference to default param by "it" • Use like Functional Programming Groovy Closure <> Java 8 Lambda
  • 16.
  • 17.
    "def" keyword Người đọckhông hiểu dữ liệu trả về. (Đặc biệt với đoạn dài) => Gây khó khi đọc hiểu & maintain
  • 18.
    Don't use "if"with Object Groovy truth and equals • Null value is false. • Empty collection or map is false. • Empty string is false. • Zero value is false. Best pratices: - Chỉ nên sử dụng với Boolean - Các trường hợp dùng với Type Number rất dễ nhầm - Các trường hợp check List/String nên check cả null (StringUtils.notBlank()/CollectionUtils.notEmpty())
  • 19.
    Don't use "if"with Object
  • 20.
    • GString. (Doublequote: "This is GString: ${1 + 1}") Can be calculate, evaluate method.... • String. • Single quote: 'This is String' • Double quote: "This is String" - Like Java String => Don't use GString when unnecessary (String constant) String vs GString
  • 21.
  • 22.
    "?:" operator => Hữudụng khi dùng để set default value Vấn đề: - Các trường hợp String blank hoặc Number value 0 - KQ: 10 & 10​
  • 23.
    Null Safe operator WithNull safe Without Null safe
  • 24.
    with() method Check condition Setmultiple value Use like builder
  • 25.
    Collection In Groovy •Find/Filter • Map • List to Map • Grep (exclude null, empty elements)
  • 26.
  • 27.
    • Use operator: '==' (compare String), '=~' (Check regular/Contains) • Operator: '+', '-' (add, remove with List) • Parse Date: Date.parse(...), Date.format(...) Groovy best practices (Continues)
  • 28.
    • Annotations: • @InheritConstructors •@TupleConstructor • @Slf4j • @CompileStatic • @Singleton Groovy best practices (Continues)
  • 29.
    • Groovy 2Cookbook • Programming Groovy 2 • Blog: http://mrhaki.blogspot.com • Groovy Web Site: http://groovy.codehaus.org How to start with Groovy?