Thursday, May 31, 2007

Google Gears

Just today I've seen this link on reddit. and I'm very excited about this. Remember an earlier post about off-line web applications Well, they're closer than ever. Google released Gears, which is a Firefox/IE extension that provides an API for off-line web applications.

On the developer page, google posted 4 demos showing off their API.
  • the first one shows how an offline database can be used to keep track of stuff on the client side, without it going away the second time you visit that webpage
  • the second demo shows how google gears can remember an webpage and display it even when you are offline and the cache is emptied
  • I didn't understand the usefulness of the third demo, but I'm sure there are scenarios which use it
  • the last demo is about asynchronous methods, but I'm not sure this belongs in gears.
I am very excited about Gears, as it seems to work for both Microsoft Internet Explorer and Mozilla Firefox. Google says Safari support is work in progress.

For the moment, I'm quite busy with my thesis, but I'm dying to get my code snippet web application to use Gears.

Also, I can't wait to see a geared up version of Blogger and Gmail. Imagine preparing your posts without the need to access the internet and saving them to the local database. Next time you go online, Blogger will upload whatever you've been working on. Sounds cool, right?

I like Gears because the API seems simple enough and is super useful. However, in the long term, Gears (or whatever other framework for off-line web apps will prevail) will make web applications even more difficult to create. Also, I suppose a whole new range of security issues will be uncovered by this new type of applications.

Wednesday, May 9, 2007

To the Java refactoring maniacs

Every once in a while I have a virtual fight with some Java programmer over refactoring tools. They are not available for C++, Python, whatever. How can you even use an IDE without refactoring tools? Do you know how much productivity you gain? The Java language is the best language in the world; it was designed to be "refactorable". Bullshit!

Let's see. Java is known to be a poor language from expressiveness' point of view. Tools save it somehow, and whenever I try to tell someone there are better languages out there, IDEs are thrown into my face and the refactoring tools they provide are the ultimate arguments for Java.

So I set out to prove that the most simple refactoring tool one can think of is not totally automatable, even in a language as simple as Java. Let's wait no more. I clicked Eclipse in my Applications/Programming menu, went and grabbed a cup of tea while it was loading, came back just to see it's asking about the workspace (first time use), grabbed another cup of tea while waiting, and set out to test my theory.

I entered this code:


import java.lang.Class;
import java.lang.reflect.InvocationTargetException;

public class MainClass {
public static void poorName() {
System.out.println("This is a stupid method.");
}

public static void main(String[] argv) {
try {
MainClass.class.getMethod("poorName", new Class [] {}).invoke(null, new Object[]{});
}
catch (NoSuchMethodException e1) {
System.out.println("there is no method with name poorName.");
}
catch (IllegalAccessException e2) {
System.out.println("there is an illegal access.");
}
catch (InvocationTargetException e3) {
System.out.println("invocation target exception.");
}
}
}


I was initially aiming at fewer lines of code, but then I decided to handle all the exceptions. Simply put, this program looks for a method called poorName in MainClass, using introspection. It then tries to call that method. If the method is not found or whatever, an error message is printed.

As I run this program, I get the nice message:


This is a stupid method.


Then, because I don't like the name of the method, I set out to rename it. Of course, what better way of renaming a method than right-clicking on it's name, and selecting "Rename" from the "Refactor" menu?

Tadam, no error message, method was renamed. Resulting code?


import java.lang.Class;
import java.lang.reflect.InvocationTargetException;

public class MainClass {
public static void goodName() {
System.out.println("This is a stupid method.");
}

public static void main(String[] argv) {
try {
MainClass.class.getMethod("poorName", new Class [] {}).invoke(null, new Object[]{});
}
catch (NoSuchMethodException e1) {
System.out.println("there is no method with name poorName.");
}
catch (IllegalAccessException e2) {
System.out.println("there is an illegal access.");
}
catch (InvocationTargetException e3) {
System.out.println("invocation target exception.");
}
}
}


When I run this code, guess what? It changed behavior. It now prints:


there is no method with name poorName.


which is true, but sad. There you go: method rename is not computable (anyone care for the mathematical proof?), even in a language as simple as Java (well, the language is not as simple as it used to be, but still).

To all Java refactoring maniacs out there: the next time you want to tell me you're not looking at another language because it doesn't provide automated refactoring tools, please think hard about why that happens in the first place.