Monday, February 12, 2018

Groovy: Conclusions

Groovy is an interesting hybrid language that bridges the words of strictly-typed, object-oriented languages and dynamically-typed scripting languages.  Because it is built directly onto the JVM, it must adhere to the constructs of an object-oriented language.  However, despite this requirement, the language itself has built into it a number of abstractions that allow for the developer to utilize it as if it were a dynamically typed language.
These abstractions offer a level of power to the language that may not exist in Java itself.  These abstractions and extension include the ability to create domain specific languages and writing more compact and concise code.  It also makes learning the language easier for native Java developers than moving to Ruby or Python.
However, since these elements are grafted onto an underlying language, that can cause problems.   Following the Law of Leaky Abstractions, as coined by Joel Spolsky, there are places where a Groovy developer can encounter errors that may not make sense as they are covered by these abstractions.
class LeakyAbstraction
{
    int makeMeFail()
    {
         Random r = new Random()
         if(r.nextBoolean())
              return -1
 
         // Here’s a “hidden” type conversion error
        
    }
}
In the above example, the method makeMeFail is typed to return a primitive int.  In the case where nextBoolean returns true, this function will complete successfully as the return is a primitive integer.  However, if nextBoolean returns false, the return attempted is a NULL as Groovy will attempt to return the value of the last executed operation.  Because NULL cannot be converted to a primitive this causes a conversion exception.  This error may not make immediate sense to the developer as other scripting languages handle this more gracefully.
With that exception, the Groovy language does provide a welcome addition to the Java eco-system by providing an alternative to the strict, structured world of Java but still offering access to the full breadth of the Java ecosystem.

No comments: