Soylent Code

Soylent Code is People! It's peeeeople!

Wherefore Art Var?

To expand on my little rant about Java 7 let me rant about the one feature that almost all other modern languages have that Java lacks and really bugs me.


Yes, var, it seems like such a little thing, such a minor feature, but it makes refactoring so much easier. Take this statement:

  var foo = someObj.GetFoo();

Note how nowhere in this statement does it explicitly say what foo is. It’s still statically typed because the compiler can infer type from GetFoo’s return. Some people might think that’s a problem but we have modern IDE’s so it’s really no big deal.

The power comes when I want to refactor GetFoo, now as long as whatever it returns has the same signature as the original everything is OK and I never have to touch this file. I might be introducing a interface, or a abstract class or even completely replacing it with some other implementation. It matters not, all that matters is that my change had the smallest impact possible.

In Java 7 they are introducing some generics stuff where you don’t have to state the type twice. So instead of

   Map<String,String> foo = new Map<string,string>();

you can do

  Map<String,String> foo = new Map<>();

This completely misses the point of type inference. All it does is save me some keystokes but it does little to assist future refactorings. The fact that Sun/Oracle spent time on this rather than proper inference features is mind boggling and almost insulting.

P.S. someone has made a library to attempt this. I can’t speak for how well it works or it’s impact as I have not yet used it. I suspect that for type inference to really work well it needs to be baked into the compiler.