matt ryall’s weblog

Having an axe to grind since 2002.

Site

Portrait of Matt Ryall

 

About me

Feed icon Articles feed

Feed icon Comments feed

Archive

Photography

Europe trip 2004

More photos

Software

NoteWiki

Other Pages

About Me

Uni timetable

SysProg Journal

The List

Coding idioms

1 July 2008

Just like human languages, programming languages have idioms — commonly used structures that don’t really fit with the typical use of the language. The number of idioms in the language is closely related to how flexible it is. Languages like Perl seem to have dozens of idioms — Perl is often called an idiomatic language — whereas Java doesn’t seem to have so many.

Idioms seem to arise where it’s difficult to construct something in a particular language. For instance, in Java there’s no simple list or map initialisation syntax. So one idiom I’ve seen in Java is this way of initialising a list:

List list = new ArrayList() {{
    add("one");
    add("two");
    add("three");
}};

The double-braces aren’t a special syntax, they’re just the combination of an anonymous inner class and an instance initialiser block to add items to the list at construction time. Instance initialiser blocks are quite rare, so the above code looks like magic to many Java developers.

Of course, I’d never use a structure like this in production Java code — the idiom is too unusual and hard to understand. I don’t want other developers wondering what the code does when they need to fix bugs in it.

One idiom in Perl is making the equivalent of a switch statement. Perl doesn’t have a built-in syntax for switch like C and Java, so Perl programmers often have a coding idiom like this:

for ($action) {
    /list/ && do { print list($cgi, $articles, $template); last; };
    /edit/ && do { print edit($cgi, $articles, $template); last; };
    /view/ && do { print view($cgi, $articles, $template); last; };
    /add/ && do { print add($cgi, $articles, $template); last; };
    /delete/ && do { print del($cgi, $articles, $template); last; };
    die "Action not supported: $action";
}

This idiom uses many quirks in Perl to work its magic. First, there’s the for loop, which sets the ‘topic’ of the block to $action. Normally, it would iterate through all the values provided, but by just providing one value, you’re using the topic-setting functionality of the loop but not the looping behaviour.

The regex matches at the start of each switching line, like /list/, are matching against the topic, $action. Any successful regex match runs the code block following. The last command in each block is the equivalent of the break statement in other languages.

A simpler example of this idiom is applying the multiple regexes to the same variable:

for ($date) {
    s/^\[//; # strip leading bracket
    s/\[$//; # strip trailing bracket
}

In JavaScript, a language I’ve become familiar with only recently, many of the idioms seem to be around anonymous functions. Here’s a very common idiom for limiting the scope of variable declarations, sometimes (but not very accurately) called ‘namespacing’:

var obj = (function ($) {
    var foo = "bar";
    return {
        prop: function () {
            alert(foo); // "bar" -- foo is visible here
        }
    };
})();
alert(foo); // "undefined" -- foo is not visible here

This can be extended by using the call and apply methods on JavaScript’s Function object to provide a scope or argument for the execution of the anonymous function:

(function ($) {
    // ...
    // within this block, 'this' is obj and '$' is jQuery
    // ...
}).call(obj, jQuery);

There are probably many more idioms like this in programming languages. What are the ones you’ve come across?

 
Posted by Don Brown at 2008-07-01 10:21:37
I use the Java one all the time, though mostly for maps as there is already the convenient Arrays.asList(Object…) method. The Javascript trick is critical for ensuring you don’t pollute the global namespace, though usually it is enough to do:
<pre>
var GlobalVar;
(function() {
… some code
GlobalVar = someVar;
})();
</pre>
 
Posted by Matt Ryall at 2008-07-06 10:25:51
Don, you’re right about it being great for maps. Arrays.asList() in Java 5 and higher is usually a faster way to create a simple list.

I’m not sure I understand your point about JS namespacing though. Are you saying you should have global variables declared in the top scope but defined within a function? What would be the purpose of that?

(Sorry about the formatting issues. The comments system really sucks on my blog. The only formatting provided is replacing newlines in the comment with <br> tags and automatic hyperlinking of URLs.)
 
Posted by James Roper at 2008-07-18 10:25:05
Shell scripting is the land of idioms, as the languages themselves don’t provide much at all. A common idiom I’ve seen in Bourne shell scripts is:

if [ “FOO${VARIABLE}” == “FOO” ]
then

The above is a guaranteed cross platform test for checking if VARIABLE is blank.

Functions can’t return values in Bourne shell scripts, so here’s an idiom to address it:

sayHello()
{
echo “Hello $1”
}
MESSAGE=`sayHello James`
echo $MESSAGE

So the value is returned by echoing it, and the function is invoked using command substitution.
 

Comments on this article have been closed.