Saturday, May 30, 2009

diff -q -r

I love to program in Groovy/Grails platform.
I have developed a framework for executing applications over Grid infrastructures such as PRAGMA. This project is in early stages therefore new features and modifications are actively added.
However, sometimes there are changes to cause that some components interfere negatively with other components and to track errors are challenge task, specially in this kind of projects who exhibit loosely coupled architectures. In fact, this is a service-oriented framework if you wish. As consequence, in rare situations a set of events fall in place and produce unexpected errors. That situation happened to me.
Thanks God, I decided to use git for managing versions in my software and I could identify the last date when the software worked well. However, I was pretty sure that many modifications were not the cause of the fault. Then, I started to check differences between the last stable version and the last version. So, I employed the 'diff' tool.
If you use 'diff -q -r <firs-dir> <second-dir>', it searches for differences between these two directories and print out the files to actually present differences.

OK, you got the files to present differences, now what? OK, use 'vimdiff'. If you invokve 'vimdiff file-one file-two' it would present a screen divided in half, left side shows 'file-one' and right side 'file-two'. In addition, the program highlights the text segments different between the files. Then, you could decide what changes make and what ignore.

In conclusion, git is a great tool for managing files versioning, diff is an elder tool used for building the 'patches' of early versions of Linux kernel but it stills do a great job. Finally, vimdiff is a great complement to diff. :-D.

Good hack,


Monday, May 25, 2009

Running groovy scripts from command line

Groovy is another language for the Java Virtual Machine(JVM).
Further of the ubiquitous Java language programming, there are several alternative languages for JVM, such as Clojure, JRuby, Jython, Rhino and Scala, among others.
Groovy is a relative new language who incorporates features from widely adopted and accepted languages such as: Python, Smalltalk, Ruby, and of course Java, among others.

One catching feature is that Groovy allows rapid prototyping. As mentioned above, Groovy incorporates a lot of fancy features such as closures, implicit casting, powerful operators over data collections, etc.
In particular, I love the possibility to write a snippet of code and quickly check if it fulfills with my expectations.

Lets look a trivial example. You are a newbie Java programmer and you wish to know if you understand how a loop works. How do you that in Java:
  • Write a Java class
class Looping {
  public static void main(String args[]) {
    for (int i = 0; i < 10; i++) {
       System.out.println("i: " + i);
  • Save this file  as '', compile it 'javac' and run it 'java Looping'
Now, lets see how this can be done in Groovy (I assume that you usually work in a real operating system like Linux):
  • Write a text file
#!/usr/bin/env groovy
for (int i = 0; i < 10; i++) {
  println("i: ${i}")
  • Save this file as 'example.groovy', change its permissions 'chmod +x example.groovy', run it './example.groovy'. That's it.
No 'main' methods were required, no compilation of source code in JVM machine code; just write your code and run it.
Groovy offers a lot of interesting features. I mentioned just one that I considered useful for rapid prototyping, but there are more serious, well-elaborated, and amusing features that this modern language can offer.
However, the Groovy  Achille's heel is its poor performance. However, when current releases are compared with earlier releases we can say that great achievements has been made. (However, more work is required)

Anyway, if you are a Java developer and are starting to test new Java features and don't want lose a lot of time writing testing classes but scripts to test snippet of code, Groovy is an option. (...and slowly you'll start loving it and adopt it for production releases not just for testing purposes, I'll assure you :-D.)

Good hack!