Subtle language differences that gets you - No Fluff Just Stuff

Subtle language differences that gets you

Posted by: Venkat Subramaniam on June 19, 2006

It?s fun and you learn a great deal when you try something new. When you do that in front of an
audience, you first make a fool out of yourself and then learn.

That?s what I did at my Get Groovier with Grails talk in Salt Lake City on Saturday. I was nearing
completion of my mostly live coding talk when I ran into a problem. No matter what I did, the page would
not update after I threw in some AJAX together. When you mess up in front of good audience,
everyone eagerly tries to help. Well, after quite some logical thinking, I got a suspicion and nailed down
the problem. Here is an over simplification of what I ran into:

Consider the following Ruby code (I got to Groovy after learning Ruby, so my mind thinks about Ruby quite a bit
while writing Groovy):
def foo(val)
    if val > 4
        "haha"
    else
        "hoho"
    end
end

puts foo(5)

The output is ?haha.? In Ruby the last expression evaluated is returned by convention from  the method.
Same is in Groovy. But, here is similar Groovy code:
def foo(val)
{
    if (val > 4)
    {
        "haha"
    }
    else
    {
        "hoho"
    }
}

println foo(5)

The output is null. I guess it assumes the last statement is the empty statement before the last }. I need
to look at the language specification to see what it says. After much ado, I put a return before ?haha? and
?hoho? and got it working (In the actual example I was playing with, had to put return before the
result hash table). Of course, I will not forget this anymore.
Venkat Subramaniam

About Venkat Subramaniam

Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., creator of agilelearner.com, and an instructional professor at the University of Houston.

He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with sustainable agile practices on their software projects.

Venkat is a (co)author of multiple technical books, including the 2007 Jolt Productivity award winning book Practices of an Agile Developer. You can find a list of his books at agiledeveloper.com. You can reach him by email at venkats@agiledeveloper.com or on twitter at @venkat_s.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »