Welcome to this review of the Pluralsight course Groovy Fundamentals by Jeremy Jarrell.
Jeremy is a software developer specializing in commercial application development for the enterprise space.
His areas of specialization include agile development methodologies, team leadership, distributed architecture and web application security.
Getting Started with Groovy
Jeremy says Java has become ubiquitous, but the Java language has evolved slowly in comparison to the Java platform.
There has however been a lot of innovation in alternative languages for the JVM, such as Scala, Clojure and Groovy! (Also check out Kotlin)
Groovy is a dynamic language supporting most Java concepts as well as functional programming, meta programming and excellent support for processing JSON and XML files.
Jeremy explains the pros and cons of static and dynamic languages. He also discusses strongly type versus weakly typed languages.
Groovy is considered to be a strongly typed language.
Here we see a pyramid showing static languages at the bottom (representing framework or backend code), dynamic languages in the middle (representing application code),
and DSLs at the top (representing key business rules).
Jeremy says polglot programming is using the right tool for the job.
He explains the purpose of DSLs, giving HTML, Apache Camel and Gradle as examples.
He says you do not need to be a polyglot programming to use Groovy, and many apps are written entirely in Groovy.
Some examples are Grails and Griffon.
Jeremy says Groovy is very accessible to Java developers, allowing all existing Java libraries to be used, and fitting in well with the Object Oriented paradigm.
He says Groovy works well for Web application development, for building DSLs, for writing acceptance tests, or for Scripting in a JVM environment.
We see how to install Groovy on Windows.
If you haven’t already set the Java home environment variable on your machine see http://stackoverflow.com/questions/2619584/how-to-set-java-home-on-windows-7
Jeremy also explains the process for *nix systems. The Groovy enVironment Manager was created to make this easy. See gvmtool.net for details.
The Groovy shell is a REPL. We run it with the command:
In this lesson we learn the basics of using this tool.
Jeremy adds a println statement into a Sample.groovy file, and compiles it from the terminal:
$ groovyc Sample.groovy
Once compiled we can run it like this:
$ groovy Sample
We see that we can also compile and run groovy code directly:
$ groovy Sample.groovy
Jeremy shows us how we can use Groovy inside of the Eclipse IDE with the aid of the Groovy-Eclipse plugin.
We get syntax highlighting and full debugging support. Groovy also enjoys the same level of support in the IntelliJ and NetBeans IDEs.
We learn that classes are not necessary in Groovy, and semicolons are often optional.
can be simplified to just:
println "Hello, world"
We start with a Java-esque for loop to say hello to each of the Beatles.
Jeremy then introduces the “G string”. This allows us to evaluate expression at runtime directly in a string statement.
We define a variable, for example greeting, and then use it in a string with the $ prefix, for example “$greeting”.
We see several more sophisticated examples. It is a very lightweight and terse syntax.
We finish by cleaning up for code, making use of the flexibility that Groovy has over Java, and accomplishing the same functionality with less code.
Another smart Groovy feature is the ability to produce ranges with minimal code. This creates a range of all letters ‘a’ through to ‘g’:
def range = 'a'..'g'
We see how we can take a days of the week enumeration and only display the days of the week. Jeremy also explains extents.
We see how to create an isEven function in Groovy.
Jeremy says closures are easy to understand. They are just a block of code. They have deferred execution: they are not executed until they are called.
i -> println "In a closure: $i"
We also see how to use the findAll function to print only even numbers.
We see that although variables are dynamically defined using the def keyword, they receive specific types at runtime.
We do not need to use the def keyword. We can specify the type that we want if we are concerned about type safety, although Jeremy says using the def keyword is idiomatic.
The Groovy compiler doesn’t enforce type checking at compile time, leaving it as a task for the Groovy runtime.
Jeremy explains Groovy uses duck typing:
“If it walks like a duck and it talks like a duck it must be a duck”.
Python and Ruby also use duck typing. He says unit testing becomes very important when working with a dynamic language.
The full course on Pluralsight also contains modules on the following topics:
Digging Deeper with Groovy Syntax
Working with Databases Using Groovy