Wednesday, January 21, 2004

Irritating Java coding recommendation:

"When dealing with a known value and a variable, reverse the logic: Instead of variable.equals("known string");, write "known string".equals(variable). This makes it easy to avoid a nullpointer if variable is null."

This makes sense from a compiler point of view, if you have a language that separates out nulls from boolean testing, but goes against all of the english language. How often do people say "Is '0555 666 7123 the number of Mr Smith?" - never (ok, hardly ever), because we tend to hash things in a name-value look-up way due to the fact that a name is easier to remember than a generic number. Same thing goes for domain names.

Contrast this against the approach that, say, BASIC takes, or that Perl keeps in mind. A computer language is just that - a language, and so it makes sense to model it on grammatical constructs that we are familiar with. Thus, the evolution of the use of keywords such as "foreach", "while", and even Java's naming conventions, e.g. "isBoolean()". It makes sense that code makes sense when being read.

Hmm, that seems to irritate me more than it should. Not sure why. OK, it's out of my system now...

No comments: