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).
0 comentários:
Post a Comment