What is a programming idiom?

I see the phrase "programming idiom" thrown around as if it is commonly understood. Yet, in search results and stackoverflow I see everything...

From micro:

  • Incrementing a variable
  • Representing an infinite loop
  • Swapping variable values

To medium:

  • PIMPL
  • RAII
  • Format, comments, style...

To macro:

  • Programming paradigm or common library features as idiom
  • Process model as idiom
  • A collection of idioms equals a new paradigm

Is there a single, common definition for "programming idiom"? Since "programming idiom" is used in many scopes:

  • Micro: syntactic nuance or common syntax
  • Medium: common style and patterns
  • Macro: programming paradigms as idiom

Is it valid to use the phrase in any of these scopes? The answers so far focus on syntactic idioms. Are the others valid as well?


Solution 1:

A programming idiom is the usual way to code a task in a specific language. For example a loop is often written like this in C:

for (i=0; i<10; i++)

PHP will understand a similar construct:

for ($i = 1; $i <= 10; $i++)

But it is discouraged in PHP for looping over an array. In this case you would use:

foreach ($arr as $value)

Whereas in Ruby, you would use:

(1..10).each

for the loop, or:

array.each

There are many many possibilities to write a loop in those languages. Using the idiom makes it immediately identifiable by experienced readers. They can then spend their time on more important problems.

Solution 2:

An "idiom" in (non-programming) language is a saying or expression which is unique to a particular language. Generally something which doesn't follow the "rules" of the langauge, and just exist because native speakers "just know" what it means. (for instance, in English we say "in line" but "out of line" -- that would be idiomatic)

Moving this to the programming arena, we get things like:

 if(c=GetValue())
 {...}

which actaually means:

 c = GetValue();
 if (c != 0)
 {....}

which every C/C++ programmer understand, but would totally baffle someone coming from a different programming language.

Solution 3:

See http://en.wikipedia.org/wiki/Programming_idiom

A programming idiom is a pattern, algorithm or way of structuring code. To talk about programming idioms is to talk about those patterns that recur frequently in code or to propose new ones.

The benefits of being familiar with idioms, particularly the larger ones, is that when looking at code you can see several lines of code but because it is familiar as a particular idiom you can mentally represent and think about the code as that single idiom instead of having to necessarily read and comprehend each line individually.

To say that code isn't idiomatic is to say that it doesn't structure itself in ways that allow human readers to think about the code effectively.

Solution 4:

Idiom is a term from linguistics. It is a group of words that do not literally mean what the say. For example saying someone is "under the weather" when they are not feeling well. That particular phrase came from sailors talking about passengers, seasick passengers would go below the "weather" decks where the ships motion was less. But most of us are not sailors and don't know the literal meaning of the phrase.

In programming many, even most of the instructions are not understood by the general public even though they are English words. for example "for loop". While they make sense to programmers, they don't to most other people.