Thursday, February 26, 2009

Top 10 Tips for Grails Developers

1) Buy the Grails books and read them:
What can i say about this except Knowledge is Power. Grails is jam packed with so much useful tools and functionality that when you want to do something that might seem off the beaten track. Chances are Grails already has a really elogent solution to it already. Here are some of my favorites that i now take for granted: flash ojbect, pagenation, url mapping, taglibs;

Anyway the grails books i recommend are:

Definitive Guide to Grails 2nd edition: written by the god father of Grails Graeme Rocher
Groovy and Grails Recipes: This contains lots and lots of examples.

and the new commer but looks very good from the early release i got:
Grails in Action


2) Learn Groovy and think in Groovy

Grails is an amazing framework but it really does shine brightly when you combine that with the powerful concepts of groovy. Try your best to find out how to solve problems the groovy way. There is always a groovy way to solve problems. Here is good example:

The spread operator: *

In java if i want to iterate through a list of people and collect the names. I would do something like this:

List names = new ArrayList();
for (Iterator iterator = people.iterator(); iterator.hasNext();) {
Person person = (Person) iterator.next();
names.add(person.getName());
}
System.out.println(names);


In Groovy i would do this:

def names = people*.name
println names


ha ha so easy. I could also do this which is the same thing:

def names = people.collect {it.name}
println names


Another good one to learn is extensions that groovy have made to the JDK classes. These are so cool. Here is a quick example:

Open a url in java and download the response and print it to the standard out:

In Java:

try {
// Create a URL for the desired page
URL url = new URL("http://hostname:80/index.html");

// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
StringBuffer sb = new StringBuffer();
while ((str = in.readLine()) != null) {
sb.append(str);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println(sb.toString());



In Groovy

def text = new URL("http://hostname:80/index.html").text
println text


And no i am not joking that is really it:)

I am not even getting into builders and dsl. Anyway the point is: Learn groovy and learn the GDK. Here are my book recommendations for learning groovy:

Here are some greate resources for learning groovy:
Groovy home
PLEAC

And the Books i recommend:
Groovy Programming
Groovy Recipies
Groovy in action

3: Get to know the Grails Plugins:
Seriously this will save you so much time. There are already so many grails plugins that one of them probably already does what you want to do. It is the classic buy not build saying except you are not actually buying and it is a component level.

The plugin directory can be found here: Grails Plugins

Also the new grails in action book seems to have a good coverage of the grails plugins.


4: Learn Hibernate and Spring
So this is my top ten tips for grails and for me i believe this is very very important. I personally get more out of Grails because i already new the foundational frameworks it uses inside out. I can then appreciate what grails gives me so much more. Don't get me wrong Grails shields you from most of the complexity (or fluff) of hibernate and spring and you could happily use it without knowing detailed knowledge of either. However i have personally found that is is good to know what is going on under the covers. This knowledge will help you in understanding error messags such as the classic lazy loading exceptions.

5: Unit Test, integration test and plain test automated (of course)

Well this is a general tip. Although it should not really have to be a TIP!!! Grails and groovy makes it so easy to test it is just not funny. I won't go into the details but learn the basics Mocking, Stubbing etc. Grails has a fantastic plugin for testing which is now part of the grails 1.1 as standard:

Testing Plugin

There are also some cool new commers for integration testings:

Functional testing plugin


6: Use a good IDE
I personally find that using an IDE saves me even more time when creating grails apps. For me the clear winner at the moment is Intellij. The Grails plugin is awesome. Although there are thing i don't like about Intellij (does not highlight variable occurances easily) but i can live with them. The next best and is free is Netbeans.

7: Learn and use the HTML W3C standards
Basically if you using Grails you are building websites. If i can give one tip about doing that is it follow the standards. Sorting out browser render issues can suck away time. But if you follow the standard then you will remove 90% of your problems. First tip is to reset the browsers style sheets so you get a clean playing field. I personally think that YUI offers the best solution to this. Check this out here:

YUI CSS reset


8: If it looks good then it is good.
There is so much truth in that. People are attracted to things that look good. This is evident in the Ruby on Rail camp. Ruby on Rail emerged from a bunch of designers and it shows. Their websites alway look good with a web2.0 style. Where as grail emerged from a more programmer community. An again this show in the websites. The websites do not often look good. This not all website written in grails but a lot. So my tip is spend some time on making your website look good. Even using something like rounded corners and a nice font can really make a website look much better. Bring a designer in if you need to. This good for everyone in the long run. More people are attracted to the grails framework by seeing remarkable sites and the community grows and grows.

Get designers to compete for you 99designs.com
Quickly generate rounded corners: http://www.roundedcornr.com/

9: Use firebug on firefox
An invaluable tool i use everyday when building sites in grails is not part of grails at all. It is firebug plugin for firefox. If you are not already using firefox then your mad. And as a developer if you are not already using firebug plugin for firefox then you are madder. Get this and use it now it will save you hours and hours of time.


10: Keep upto date with the groovy and grails community
It important to keep upto date with g ang g communtiy. Here are the best links to do that:

Grails home/
Grails and Groovy blog aggregator/

Twitter posts from the main people in Grails and Groovy
Graeme Rocher
Guillaume Laforge
Glen Smith


More from you:

So if anyone out there has anymore tips they would like to share then please do so int the comments section below.

9 comments:

bherrmann7 said...

very good stuff, much thanks.

Maxim Gubin said...

Nice post!
Long live Grails!

Erik Pragt said...

Nice list! Btw, about the highlighting in IntelliJ: since version 8.x, the automatic highlighter is included. It can be enabled in the settings, under Editor -> Highlight on Caret Movement

Hope that helps!

Erik

Peter Delahunty said...

Hi Erik

Many thanks for that tip. That was the missing jewel in intellij for me. Why they don't activate it automatically i don't know :)

Anonymous said...

Eclipse is currently better than Netbeans for Groovy and Grails development.

Check out the discussion we're having in the mailing-list: http://www.nabble.com/Preferred-IDE-td26006607.html#a26006607

Nirav said...
This comment has been removed by the author.
Nirav Assar said...

Very nice article. Spring has put out an awesome version for eclipse with groovy grails recently

web design hampshire said...

Great guidance,
I also would like to share something that if you are a Ruby programmer, probably you would like doing things like 2.days or 3.day.ago. You can do these things, only installing the magic numbers plugin.

essay papers said...

Great, in addition to debugging web sites, Firebug is a useful tool for web security testing and for web site performance analysis