Stefan Wagner

«Truth can only be found in one place: the code.»  Robert C. Martin alias Uncle Bob


Blog | Archive | Resume | Projects | References

Groovy is awesome!

22 Jan 2016 | groovy, grails, gradle

In my last project, we used Grails 3 that uses groovy as the main technology. With groovy you can really focus on your business problems. You have very little boilerplate code.

Here it is some cool stuff I really like in groovy.

Working with strings:

def text = 'Groovy is awesome!'
assert text[10..16] == 'awesome'
assert text - ' is awesome!' == 'Groovy'
assert text[0].isLetter()
assert text[0].isUpperCase()
assert text[6].isWhitespace()

Working with lists and maps:

List emptyList = []
def countries = ["England", "Switzerland", "France"]
assert countries[1] == "Switzerland"
countries << "Italy"
assert countries.contains("Italy")
Map emptyMap = [:]
def country = [name: "Switzerland", capital: "Bern", population: 8211700]
assert country.name == "Switzerland"

The elvis operator is very useful:

def text
assert 'not null' == text ?: 'not null'
text = 'Hi!'
assert 'Hi!' == text ?: 'not null'

Avoid null pointer exception:

class Country {
    Capital capital
}
class Capital {
    String name
}
def switzerland = new Country()
println switzerland?.capital?.name
comments powered by Disqus

Older · View Archive (11)

Jobseeking with OWL

In the current CTI science project, we’re testing OWL and the possibility to classify objects as well. The idea is to find the right candidat, a job seeker, for a certain vacancy.

Newer

Modularize a Grails application

I’ve just worked in a project where we upgraded a web application from grails 2.x to 3.x. One part of the application was divided into a plugin and three slim grails applications, which kinda inherited from this plugin. One of the grails applications was the official end user application and the other two where some kind of preview applications. We had some issues with this structure because plugins have the following disadvantage while development: