OK, so after auto null checks and code blocks what else would make Java that little bit clearer to read and write? What about casts?
Once again, here is an example use case:
public int getAge(Object obj) {
if (obj instanceof Person) {
return ((Person) obj).getAge();
}
return -1;
}
Note, I've deliberately kept the use case 'stupid'. There are many good OO solutions to this kind of problem, but I believe that the vast majority of Java developers still end up writing nasty casts all the time.
So, what do I propose could be changed in Java 1.6?
public int getAge(Object obj) {
if (obj instanceof Person) {
return obj.getAge();
}
return -1;
}
The compiler can easily figure out that obj must be an instance of Person in the if statement. So why do we need the cast? The code without the cast is more readable and just as safe.
I still believe that casts have a useful role to play in Java, when you want to emphasise a point in conversion, or to cast without an instanceof. But when we've already checked with the instanceof, why are we have to repeat ourselves? Thats just boilerplate code.
Opinions welcome, as always.