2 Rookie Java Constants and Enums Pitfalls

In various code reviews I perform on Java and Groovy code I often see magic numbers and other random Strings littered all over the code base.

A magic number, for example, is the 4.2 in below (Groovy) code snippet:

if (swashbuckle >= 4.2) {

What does 4.2 mean?

My advise is to extract such a number to a constant and give it a meaningful intention-revealing name, so that we can all keep understanding our code.


After refactoring into

if (swashbuckle >= MAX_ALLOWED_CAPACITY) {

even beginning developers understand the value of The Constant and start extracting values all over the place into constants. When I talk about enums it’s even more possible to write readable code, but quickly inexperienced developers quickly fall into the following traps. Continue reading “2 Rookie Java Constants and Enums Pitfalls”

Quicktip! Grails scaffolding: enums and i18n

Grails scaffolding feature is a great asset for quickly generating create/read/update/delete (CRUD) screens for your domain classes. Create an entity, create a controller and put static scaffold on top. For various types of properties within your domain class a suitable (HTML) input is used, to take the user’s input: booleans become checkboxes, associations with other domain classes become (depending on ownership and constraints) e.g. dropdowns or multi-selects, etc.

Even an enum works out of the box as one could imagine 🙂 This post serves as a small quicktip – primarily for me and other developers looking for these kind of things – of internationalizing scaffolded screens in general and enums in particular using  toString() and Spring’s MessageSourceResolvable.

Take the following Product with a Status:

class Product {

	enum Status {

	String name
	Status status

	static constraints = { name blank: false, unique: true }

class ProductController {

	def scaffold = true

Opening up our /product/create url will show what you would expect.

Continue reading “Quicktip! Grails scaffolding: enums and i18n”