« links for 2006-05-03 | Main | links for 2006-05-04 »

Java lacks multiline string declarations and therefore sucks

Java is a fine language for functionality. But Java sucks for building browser code.

This is a function that prints a multiline block of HTML, in Java:


String cssButton (String buttonText) {
StringBuffer mine = new StringBuffer();
mine.append("<div class=\"buttonC\">");
mine.append("<div class=\"img_left border-img03L-style01\"></div>");
mine.append("<div class=\"text back01 border-img03-style05\"><input name=\"\" type=\"submit\" value=\"");
mine.append(someOtherFunction("buttonText"));
mine.append("\" class=\"submit-trans style04b\"/></div>");
mine.append("<div class=\"img_right border-img03R-style01\"></div>\n</div>");
return mine.toString();
}

And this is approximately what the same function looks like in Perl (or PHP, or Ruby):


cssButton (buttonText) {
my $mine = <<EOF;
<div class="buttonC">
<div class="img_left border-img03L-style01"></div>
<div class="text back01 border-img03-style05"><input name="" type="submit" value="$buttonText" class="submit-trans style04b"/></div>"
<div class="img_right border-img03R-style01"></div>
</div>
EOF
return $mine;
}

Obviously the second subroutine looks a lot more like HTML and is therefore easier to read. And easier to read means easier to maintain, which is what it's all about.

It's worth noting that it doesn't look like Sun even understands why the inclusion of heredocs might make a difference to client-side developers.