12
Groovy WHAT IS IT? HOW DOES IT WORK? IS IT USEFUL?

Groovy WHAT IS IT? HOW DOES IT WORK? IS IT USEFUL?

Embed Size (px)

Citation preview

GroovyWHAT IS IT? HOW DOES IT WORK? IS IT USEFUL?

What Groovy Is… Basic’s

◦ Described as being a dynamically typed language for the Java Virtual Machine (JVM).◦ Built on the strengths of Java with additional features inspired by Python, Ruby, and Smalltalk.◦ Allows for static type check and statically compile your code for robustness and *performance.◦ Reduces programming time by reducing scaffolding code when developing web, GUI, and database

applications◦ Scaffolding– Is a framework that allows for CRUD(create, read, update, delete) operations against your database with as little to no

code as possible.

◦ Compiles with Java byte code, thus making it possible to port code wherever Java code can run.◦ Easy to learn for current Java developers.

Three main use cases•Tooling / Testing / Shell scripting

◦ Shell or build scripting, data manipulation, unit testing, code generation, driving native applications

•Building standalone applications• Perfect for small to mid-sized applications with prototyping capabilities• Quick scripting with full use of Java libraries

•Writing dynamic websites (Grails)

Example of Groovyclass Speaker {

String name def age String toString() { "My name is $name and I'm $age" }

}

def speaker = [ new Speaker(name:"George Karaszi", age:24), new Speaker(name:"John Doe", age:44)

]

def upper = (it.toString().toUpperCase() )

speaker.findAll(name -> name =- /.*orge.*/ ) .collect(upper).each(println it)

MY NAME IS GEORGE KARASZI AND I’M 24

Output of code:

Syntax much like java

Example of Groovyclass Speaker {

String name def age String toString() { "My name is $name and I'm $age" }

}

def speaker = [ new Speaker(name:"George Karaszi", age:24), new Speaker(name:"John Doe", age:44)

]

def upper = (it.toString().toUpperCase() )

speaker.findAll(name -> name =- /.*orge.*/ ) .collect(upper).each(println it)

MY NAME IS GEORGE KARASZI AND I’M 24

Output of code:

Easily define lists

Example of Groovyclass Speaker {

String name def age String toString() { "My name is $name and I'm $age" }

}

def speaker = [ new Speaker(name:"George Karaszi", age:24), new Speaker(name:"John Doe", age:44)

]

def upper = (it.toString().toUpperCase() )

speaker.findAll(name -> name =- /.*orge.*/ ) .collect(upper).each(println it)

MY NAME IS GEORGE KARASZI AND I’M 24

Output of code:Easily search and interact with class outputs

Simplify Simplify Simplify…. By far one of the most powerful advantages that Groovy has, is its simplified syntax and programming structure.

Groovy gets rid of the use of semicolons and other end of statement precedents. And give developers a magnitude of different ways of declaring and changing values on the fly.

◦ Using the keyword def (AKA Object). Allows for multiple data types to be assigned to a variable.

Groovy utilizes many types of syntactic sugar to become a super set of Java.

Java original looping Groovy’s equivalent code for looping

for(int i = 0; i < 3; i++) {System.out.println("United United..." );}

3.times {println "United United..."}

Notable: XML and JSON Processing

Groovy employs a Builder pattern for XML handling. Making production of data structure less verbose

Original XML document Groovy’s way of doing XML<languages> <language year="1995"> <name>java</name> <paradigm>Object oriented</paradigm> <typing>Static</typing> </language> <language year="1995"> <name>ruby</name> <paradigm>Object oriented, Functional</paradigm> <typing>Dynamic, duck typing</typing> </language> <language year="2003"> <name>groovy</name> <paradigm>Object oriented, Functional</paradigm> <typing>Dynamic, Static, Duck typing</typing> </language></languages>

def writer = new StringWriter()def builder = new groovy.xml.MarkupBuilder(writer)builder.languages { language(year: 1995) { name "java" paradigm "object oriented" typing "static" } language (year: 1995) { name "ruby" paradigm "object oriented, functional" typing "dynamic, duck typing" } language (year: 2003) { name "groovy" paradigm "object oriented, functional" typing "dynamic, static, duck typing" }}

Type checking

•Because of the dynamic nature of Goovy, compile time is nearly non-existent. The parser in the compiler never has to check the types that are being assigned, thus allowing for faster compile time of larger projects.

•Since Goovy 2.0+, developers have implemented a type of static type checking during compile time with the @TypeChecked annotation

@groovy.transform.TypeCheckedClass tester{…}

This annotation will type check everything in the tester class statically.*However this does not make runtime any faster, as groovy is still dynamic.

Static Groovy The developers of Groovy set out to make sure that the primary functionality of Groovy was as dynamic as possible.

However the dynamic nature raised many concerns with many Groovy developers who where accustom to static type checking.

Goovy 2.0+ introduces @CompileStatic annotation to address the concerns that people have when it comes to compiling statically Vs. Dynamically.

@CompileStatic- This annotation is said to speed the performance of Groovy programs. However according to Goovy’s documentation, it drastically changes the semantics of your compiled code.

Downsides of Groovy…•Due to Groovy’s dynamic nature, many developers find the readability fairly hard to understand.

•The performance of Groovy is incredibly slow.• C/C++ -> Java ->Java VM->Groovy ->Other extensions

•Lack of good tool support. Nothing compared to its parent Java.

•Not ideal for large projects.

•Bugs are more prevalent to applications.

•The creator Groovy said himself he’d never developed Groovy if someone showed him Scala first.

In The End…

Groovy is a good language if you need something done fast without the unnecessary overhead java requires.

Groovy is also becoming more integrated with other technologies such as Spring. Which is an attempt to reduce even more code scaffolding for enterprise application development.

Groovy is a scripting language and not an all in one solution for complex problems