Friday, March 09, 2007

Groovy impressions

Recently I've been doing some work in Groovy. First impressions were very good, but I won't start using it for everything. I think we should choose the right tool for the job, and dynamic languages like Groovy are great for some tasks, but are worse for others (and this is not an universal truth, some people function better in one way, others in other ways). I already knew some of the syntax-sugar (map handling, duck typing, etc) that is also common to Ruby or Python, so that was not a surprise. What really impressed me in Groovy was how complex tasks were made simple. For example: file handling. Here's a small example:


File f = new File("a.txt")
f << "text"

This is enough to append the given string to the file. This is different from just "typing less characters". What's great here is that resource handling is completely hidden from the programmer. We just tell it to read or write something to a file and don't have to remember to open or close resources.
Another gem:

URL url = new URL("http://www.google.com")
File f = new File("a.txt")
f << url.text

Yes, it does what you're thinking: automatically fetches the contents of the given URL and stores it in the file a.txt. Now, if you add closures and regular expressions, page scraping just became a lot easier :)

Some people see less typing as the ultimate goal to increase productivity. Although I think much faster than I type, I still spend more time thinking than typing. In the end I don't think a few less characters make any difference. I think is much more a psychological effect than anything else (I constantly see people writing how Ruby makes programming more "fun"). But this kind of simplicity shown above does make a difference because it make file handling easier and less error-prone (no more forgetting to close resources).

Sunday, March 04, 2007

Fixing dead pixels

Fortunately I don't have any dead pixels on my monitor. If I had I could try this, although I'm highly skeptical about it's effectiveness. But, who knows...every now an then I get surprised at something :)

Thursday, March 01, 2007

Java 6 Hotpatching

I already knew of the ability for a java process to attach itself to another process for monitoring purposes. Java 6 comes with a nice graphical console (jconsole) that shows several statistics about the running process, like memory usage, etc.
What I didn't know was that the API for that allowed to do this. According to the article we can substitute a class for a new version of it in runtime!! Although I don't see this sort of thing being used very often, it's still very cool :)