Latest Entries »

Monday morning, while doing pair programming with my boss at work, we encountered an interesting behavior in Groovy. It’s not a bug, but I was surprised enough to share it with you.

Let’s define two arrays:

def a = [1, 2, 3]
def b = [4, 5, 6]

We wanted to add the elements from “b” to “a”. Groovy overloads the “<<” operator on arrays, allowing you to push items into an array. Naturally, our first instinct was to use that. However, it didn’t work.

a << b
println a
[1, 2, 3, [4, 5, 6]]

The reason is that an array in Groovy is a heterogenous list of objects, and objects of differents types can be put in the same array. In our case, Groovy pushed the array “b” (its reference) into “a”.

We quickly realized our mistake. Multiples solutions exist to solve our problem. We took a loop-based approach, like so, iterating through “b” and pushing every element into “a”.

a = [1, 2, 3]
b.each { a << it}
println a
[1, 2, 3, 4, 5, 6]

We could have used the “addAll()” method, inherited from Java, which pushes all the elements from one array into another.

a = [1, 2, 3]
a.addAll(b)
println a
[1, 2, 3, 4, 5, 6]

A more elegant solution, though, would have been to use operator overloading. By using the “+=” operator, the elements of “b” will be appended to “a”.

a = [1, 2, 3]
a += b
println a
[1, 2, 3, 4, 5, 6]

As closing words, if you want to quickly test some Groovy code, head over to the Groovy web console, which runs a Groovy console on Google App Engine. In it, you can write Groovy code and execute it, and the results will be displayed on the Web page.

Groovy and Grails

I first heard of Groovy and Grails in fall 2009, in a course about reengineering. I didn’t think much of it at the time, but the internship I took in summer 2010 really allowed me to appreciate the true power of these two technologies. More importantly, a distributed object middleware course I took in fall 2010, in which we saw the Java EE stack (Tomcat, Spring, Hibernate, etc.) really helped me understand the amount of work that went into Grails, so that people could easily build Java EE-compatible Web applications.

In a nutshell, Groovy is a programming language that is essentially based on Java, while boasting features more commonly found in dynamic languages like Ruby and Python, such as dynamic typing, easy list and map declarations, etc. Groovy code compiles down the Java bytecode, meaning that .class files produced by the Groovy compiler can be executed by a standard JVM. This also means that Groovy is almost fully interoperable with Java: a Groovy class can inherit from a Java class, and a Java class can inherit from a Groovy class.

In theory, any .java file could be renamed to a .groovy file and still work with Groovy, as Java code is perfectly legal Groovy code (but not the other way around!).

Grails (Groovy on Rails) is a Web application framework built around Groovy, the same way Rails is built around Ruby, or Django is built around Python. Like other Web frameworks, it embraces the MVC architectural pattern (Model-View-Controller), as well a promoting the Convention over Configuration principle. To achieve this, Grails stands on the shoulder of giants:

  • Groovy is used for all the programming: domain classes, controllers, services, views, etc.
  • Hibernate is used for the mapping between domain classes and data persistance;
  • Spring is used for dependancy injection and the implementation of the MVC pattern;
  • Views are implemented using GSPs: Groovy Server Pages, which are of course based on JSPs (Java Server Pages);
  • JUnit is integrated into Grails to easily test domain classes, controllers, views, taglibs, and so forth, on both the unit and integration levels;
  • Tomcat is integrated into Grails, meaning that an instance is started everytime starts the Web application. Furtermore, a Grails application can be packaged into a WAR file that is compatible with every Java Web container, like Tomcat, Glassfish, JBoss, etc

Grails implements a robust plugin system. Basically, a Grails plugin is just a special Grails application: it contains domain classes, controllers, views, etc., allowing for modular applications that respect the DRY (Don’t Repeat Yourself) principle. The framework also enables the user to include its own JAR files, or source code files (in Groovy of Java), to allow easy integration of external or legacy systems.

A good introduction to Grails (including live coding of a Grails application) can be found in these two videos:

http://www.infoq.com/presentations/rudolph-grails-intro

http://www.youtube.com/watch?v=8d1hp8n1stA

This was just a very quick introduction. I’ll probably get back to Groovy and/or Grails in future blog posts, because I use it professionally.

In the meantime, stay groovy!

Welcome to my blog!

Hello!

By writing this blog, my goal is to share my thougths about software engineering and technology in general, but also to present some of my code and programming projects. Also, I’ll use this space to share solutions to programming problems I’ve had, so it may (hopefully) be helpful to someone in the future.

I’m a software developer who lives in Montreal, Canada. As I write these lines, I’m currently completing my degree in software engineering at the École de technologie supérieure (ÉTS).

Although I’ve lived all my life in French, I’m writing this blog in English to improve my writing skills, and to reach a broader audience.

My first experiences with programming were in 2002, at the age of 15. It all started by creating static websites on Microsoft Frontpage. However, static Web pages don’t follow the DRY principe, and I soon was tired of manually editing menus (see Larry Wall’s definition of laziness). So, I learned PHP and dynamic pages, and thus was born my passion for programming. A year later, I discovered Linux, and that really confirmed my career path: I wanted to become a programmer.

So here I am, 9 years later, writing a blog on software development!

Over the years, I have developped quite an heterogeneous skillset:

  • Linux system administration;
  • Information security;
  • Anti-spam systems;
  • HTTP protocol;
  • Log parsing;
  • Data migration (import/export/transformation);
  • ERP and CRM management;
  • Web applications, mostly Java and Grails;
  • SMS applications;
  • 3D programming.

Right now, I’m all over the Grails framework, which I think is an excellent tool to quickly and efficently create Web applications. Having almost exclusively worked with Java for the entire duration of my degree, using Grails (and, of course, the Groovy programming language, on which Grails is based) is definitely a breath of fresh air.

I’ll post a more detailled post about Groovy and Grails in the following days.

In the meantime, I hope you enjoyed this brief presentation about me!