Wandering Thoughts archives


My view on the understandability of language idioms

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.

programming/IdiomUnderstandability written at 01:18:05; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.