2010-10-15

Everything can be evaluated as a boolean value

I was rereading jwz's seminal essay, "java sucks.", and it made me think of something that sucks about Java, the syntax for testing references for whether they're null:

Foo foo = bar.getFoo();
if (foo != null) {
  //
}

This is something that EnScript gets right: pretty much every EnScript expression can be evaluated as a boolean value, without having to use boolean operators.

References
Foo foo = bar.getFoo();
if (foo) { // true if foo is non-null
  //
}

Integers
int i = 0;
if (i) { // true if i is non-zero
  //
}

Strings
String s = "";
if (s) { // true if s is not empty
  //
}

Once you get used to this [COMPLETELY OBVIOUS] shorthand, it's hard going back to a language that thinks there's something magical about boolean expressions.

No comments:

Post a Comment