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.

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, Add Comment.
Search:
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.