Friday, December 08, 2006

some rule of programming language design..

.. says that the more commonly something is done, the less typing should be required to do it. The most frequently-used constructs of a language should be able to be written so compactly that no one even notices them.

This is why in (modern) languages, string concatenation requires less typing than many other operations, because string concatenation is darn frequent.

The original design of Java deliberately violates this "rule" in many aspects of the language (although curiously not with string concatenation). Old school Java in fact favors explicitness and exacting clarity over compactness in nearly every other feature except for string concatenation. Newer features like the for-each loop, and proposals like closures, seem to be swinging it back toward the other side.

Of course as a language evolves, what is "frequently used" can change. At the time C was invented, one can guess that incrementing variables was used all the damn time, so often that nobody could be troubled to write i=i+1, no the language had to have i++. Meanwhile strings were out in edge-case-land, so relatively arcane and insecure means of dealing with them were considered good enough.

Perl probably takes the "common therefore compact" philosophy to the extreme. I don't think I need to show any examples. You either know enough about Perl to already understand that, or you won't get the examples anyway.

File under: random musings


Ironically, the one place where Java is presumably trying to help people by making string concatenation easier, turns out to be one of the most famous temptations you should resist in Java (for creating large strings from lots of little pieces in a loop at least)
But it's supposedly "much better now".