SyntaxHighlighter

Friday, July 8, 2011

Simpler if condition and Truthy/Falsy

Coming from Java land, I'm enjoying the ability to write shorter code in groovy than in Java. The condition inside an if statement is one of those. Given cool is a boolean, in Java I'd do this.
if (cool == true) {
// do something cool
} else {
// do something hot
}
In groovy, I can do this.
if (cool) {
// do something cool
} else {
// do something hot
}
But it's good to remember what actually gets counted as true or false. Although I don't like using null as a value, I had to deal with one the other day. I had a return value returned from a service call, the service call itself returned either a String or null.
def something = oldService.tellMeSomething()

if (something == null) {
// I don't like null, but I have to live with it for now.
} else {
// work on something
}
I got excited and changed the code to the following.
def something = oldService.tellMeSomething() // same service call
if (!something) {
// I don't like null, but I have to live with it for now.
} else {
// work on something
}
Then, I suddenly had a hunch that this might be wrong. The thing was that an empty String is a legitimate value returned, and I wasn't sure if an empty String would be evaluated as true or false. So I ran the following in a groovyConsole.
def e = ""
def n = null

if (e) {
println "empty string is truthy"
} else {
println "empty string is falsy"
}

// check for the opposite case too for a good measure.
if (!e) {
println '(!empty string) is truthy'
} else {
println "(!empty string) is falsy"
}

if (n) {
println "null is truthy"
} else {
println "null is falsy"
}


// check for the opposite case too for a good measure.
if (!n) {
println "(!null) is truthy"
} else {
println "(!null) is falsy"
}
If you ran that, or you know groovy better than I do, you might be relieved to know that I reverted my change back to the original (and I sighed a little too for the use of null).

Thursday, July 7, 2011

Groovy Date clearTime() method

Groovy has a nice Date class defined.
One I learned to use recently is clearTime() method.
Date today = new Date().clearTime()
This should result to today's date with no time component attached to it.

But, my code kept on failing.
So I dug around a bit, and found this: http://jira.codehaus.org/browse/GROOVY-4788

I was on groovy 1.7.3, because my project's on grails 1.3.7. As you can see on the jira page, at this version of groovy, clearTime() does not have a return value.

Watch out for it if you are stuck!

After thought: groovy 1.8 seem to have very nice improvements over previous versions of groovy. I hope grails 1.4 will get released soon!

Friday, June 10, 2011

semi-colon: ';'

I'm not going to start with the usual "Hello, world!" example.
My beginning is more trivial than that: just a semi-colon.

;

Every Java statement has to end with ;.
In Groovy, I don't have to. I could, but I don't.
So, this works:

System.out.println("Look, no semicolon!")


It's a small change, but if you get used to it, it can also deter you from trying to put multiple statements in one line as you *might* have been doing in Java.
Don't try to be too clever and try to have multiple statements in one line. It doesn't always help.

Thursday, June 9, 2011

Groovy?

Ok, first things first.

This isn't about some groovy music band, dance moves, or anything like that.

I'm a software developer.

On this blog, I intend to log (much in the sense of a logbook) my journey in learning the programming language called, Groovy.

Comments are welcome, but this is my journey into learning groovy. You shouldn't use this as a manual to the groovy language.

Ok, I'm done for now.