My view on the understandability of language idioms

August 22, 2012

In my entry on the periodic strangeness of idiomatic Python I noted that I found the C version of the 'repeat N times' loop much more immediately understandable than the Python version. In response, a commentator wrote:

I don't find this: "for (i = 0; i < times; i++) { .... }" idiomatic at all ... unless you are familiar with a language that writes its loops that way. [...]

Looking at my entry again I see that I was unclear about what I meant by immediately understandable. My impression from the comment is that the commentator expects idiomatic code to be understandable even if you don't know the language it's written in. I don't believe in this for idiomatic code any more than I believe in this for ordinary code (which I don't).

When I talked about the C code being more immediately understandable, I meant to someone who knew C in general. If you know C, you know what a for loop is and what the parts of it are; once you know that, it's easy to see what this particular for loop does. In that sense the C idiom for 'repeat N times' is obvious (in a way I feel that the Python idiom is not).

(This is in a sense what 'idiomatic code' conventionally means; it is the natural way to solve the problem for someone who is familiar with the language.)

When an idiom is not obvious or immediately understandable, it's using some less common or relatively obscure feature of the language, or an odd convention that you have to know about, or doing something tricky but clever. There are perfectly good, well regarded idioms that fall into this general category, for example the Schwartzian transform. What these idioms all have in common is that you either have to know them already or you need to carefully think them through before you can understand what they do; someone who merely knows the language without knowing the idioms cannot read code with them in it and immediately understand it. In a sense, such idioms are slang or jargon.

To me this is what makes them strange and worthy of note. If you know the idiom the code's perfectly natural; if you don't know the idiom (but do know the language) it's a 'huh? what?' moment.

Comments on this page:

From at 2012-08-25 19:09:25:

When I talked about the C code being more immediately understandable, I meant to someone who knew C in general.

This addition changes the statement from debatable to circuitous. “Code written using C-derived arbitrary constructs is more obvious to someone who knows C than code written with non-C arbitrary constructs”: err, yes obviously. How does that warrant a writeup?

Aristotle Pagaltzis

By cks at 2012-08-25 21:53:07:

I think I may have been unclear; let me try again. I feel that the C idiom for 'repeat N times' is more clear and less tricky (to someone who knows C) than the Python idiom for the same thing (to someone who knows Python). Any C programmer should get the repeat N times idiom in C, but a Python programmer can reasonably be slowed down and puzzled by the Python version because it involves some tricks and is kind of indirect. I can see a normal Python programmer worrying that there is something special that they don't understand about the use of _ in the 'for _ in xrange(0, max): ...', for example.

Written on 22 August 2012.
« Another problem with how Debian builds from source packages
Illustrating the Ubuntu clown car, AccountsService edition »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Aug 22 01:18:05 2012
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.