Groovy as your primary language

Groovy is fantastic. Remind we why I still program in Java?

I’ve been watching some Groovy videos from the recent SpringOne2GX conference. I’ve always really liked Groovy, and use it for all my personal projects. It’s so much more succinct, and the tools around it are very powerful.

After watching these videos, and seeing even more power available, as well as the fantastic enhancements around Type Checking and static compiling, I’m left asking myself why I code in Java at all.

Almost all valid Java code is valid Groovy code, so it’s easy to transition at your own pace. Groovy then allows you to remove so much ceremony, plus has so many powerful additions. AST transforms for creating an Immutable object. Defining a POJO while only having to define the properties. Powerful DSL’s that if written correctly, are Type checked at compile time, rather than at runtime.

Spock testing framework combines all the power of Junit, Mockito and JUnitParams in an even easier to read and use DSL for testing.

Even if you use Spring Boot, there is very good support for using Groovy everywhere. The main language, as the templating language (the new Groovy MarkupTemplate DSL is fantastic. Type checked at compile time rather than runtime, and optimizations performed by the compiler to improve performance at runtime).

Replacing Spring Data with GORM (from Grails). Spring Data allows for reflection based methods, BUT these aren’t checked until run time, and are limited in their functionality.

Using the latest GORM DSL, the queries are checked at compile time, and are much more flexible and readable. Instead of relying on a very long method name, a closure is used to define the request.

This will ensure the People object has a name property and an adult property at compile time.

DSL’s written correctly, are type checked at compile time, and your IDE can provide code assistance. Very powerful tool.

Watch the videos below and learn more about Groovy. I intend to incorporate it into my next project, and don’t want to code Java again.

Groovy after all these years at Mutual Life
Advanced Groovy Features
Modern approach to writing DSL

Java: Converting Time from one format to another

I’ve started using Checkstyle on my code base. One of the checks I’ve enabled is magicnumbers, which as its name implies, ensures your code isn’t using magic numbers.

As a result, I found some code that was doing the following:

How many times have you done that? Or wanted to convert milliseconds to minutes or various others? Created a constant with 60*60*24 for conversions?

Doing a bit of Googling, I learned that in Java 6, as part of the Concurrency updates, the JDK now has a TimeUnit class. This then allows one to do the following instead:

That’s much cleaner, and less prone to both misunderstanding and errors.

  • Checkstyle:[elink link=””]
  • TimeUnit: [elink link=””]

Java 7 Nio Can replace

Working on a project that was using FileUtils.copy to copy a file from an InputStream to a File.

I decided to investigate the Nio package in Java 7 (and if you’re still using Java 6, please stop). In the docs for Files.copy ([elink link=””]) was an example of downloading an html page to a file.

It’s very clean and also means I no longer need to depend on an external jar to perform many file operations.

Good bye commons-io. You’ve served me well, but I have a new ‘friend’.

Detailed information on Java 8

TechEmpower has posted a summary of expected changes in Java 8. So many of these features are long overdue, but I still be very thankful for them. Here’s a very short list of the highlights. If you develop in Java, check out the list, and let’s hope Java 8 actually ships in March 2014, and with all these features.

  • Interfaces can define static methods, and default methods.
  • Lambdas.
  • Better type inferencing.
  • Proper time library
  • Some functional additions to Collections
  • Concurrency additions
  • Annotations allowed in more places

[elink link=””]

Thucydides – What is its focus?

Thucydides isn’t about Browser based functional testing, but I’m not sure that everyone’s aware of that. Hence this entry.

Thucydides is a report generation structure around JUnit tests written
using JBehave, EasyB or Java. It provides easy integration of WebDriver
to do browser based testing, but that isn’t its primary goal. Its
primary goal is to foster communication between developers and their
clients. To provide living documentation of their code, and a high-level
view into their progress.

This can be applied using browser
based functional testing, but another great use would be during
development of a service. If one is creating a RESTful API, for example,
one would be (I hope) creating integration/functional tests for same.
By wrapping these tests in Thucydides, the Thucydides reports could be
published as both documentation and examples.

Just something to think about…