83

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:

To macro:

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?

1
  • A programming idiom is an idiom written in a programming language.
    – jaco0646
    Oct 12, 2018 at 20:33

10 Answers 10

67

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.

29

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.

2
  • 1
    -1 While idioms may be influenced by syntax, I think there is much more to them. Jan 2, 2010 at 6:21
  • 3
    I like this answer. In natural language an idiom is something whose meaning cannot be constructed from the meanings of its constituent terms. In other words it is an indivisible atom semantically even though syntactically it can be divided. So in programming, idioms are things you don't question but just rote-memorize and can be productive with. Aug 31, 2016 at 22:05
16

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.

3

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.

2
  • 4
    -1: A programming idiom is different than the non-programming variant. Jan 2, 2010 at 6:22
  • 1
    ...and yet it isn't.
    – Jason S
    Sep 27, 2017 at 4:38
3

Since large programs grow from small ones, it is crucial that we develop an arsenal of standard program structures of whose correctness we have become sure -- we call them idioms -- and learn to combine them into larger structures using organizational techniques of proven value.

A programmer should acquire good algorithms and idioms.

Alan J. Perlis - SICP Foreword

2

From WikiPedia: A programming idiom is a means of expressing a recurring construct in one or more programming languages.

I'm guessing you've already been down that road though!

1
  • This just delegates the question to "What is a construct?" Is it micro, medium, or macro?
    – Pacerier
    Jun 13, 2014 at 23:53
2

An idiom is a 'pattern' that can be identified in several places.

I wouldn't say it has anything to do with a particular programming language.

Iterator foo;
foo.reset();
while (foo.next())
{
    print(foo.value());
}

That is a snippet of what i would call the 'for each' idiom which is expressed slightly different in a number of languages.

Another excellent example of an idiom is Socket. All platforms that claim to have sockets, all work in conceptually the same way, that is, they all have roughly the same interface.

2
  • 1
    When I hear "Socket", I think "I/O pattern". I don't see it as an idiom - patterns exist across languages, whereas (as described by most other answerers) idioms tend to be language-specific.
    – Tom
    Feb 11, 2011 at 4:51
  • I didnt give my particular answer as an attempt to distinguish between an 'idiom' and a 'patterns', which should be clear if you read the first five words. It was meant as an informative and explanatory answer.
    – Zuu
    Feb 22, 2011 at 18:29
1

An idiom is a way of saying something that is particular to a given language. For example here are a handful of english idioms.

You can extrapolate this to apply the concept to programming.

1

Get into a rut early: Do the same process the same way. Accumulate idioms. Standardize. The only difference(!) between Shakespeare and you was the size of his idiom list – not the size of his vocabulary.

  • ALAN PERLIS, Epigrams in Programming

http://www.cs.yale.edu/quotes.html

0

It comes from idiomatic the meaning of the word idiom in programming can be summed up as phrase that carries meaning and implications that is more than the sum of the words. In programming most code snippets are actually idiomatic. 'Pertaining or conforming to the natural mode of expression of a language'

A Programming idiom can be considered descriptive of a class of solutions that is transferable to different cases. Consider while { ... } vs do {} while these are idiomatic, they contain the same words but the ordering carries an important distinction. The exact phrasing will differ by language, but the fundamental meaning and implications will differ; for example do {} while will always be executed once, no matter what language or statements are used to implement it. As an idiom it is transferable shape of an idea. It could be used in many circumstances, and expressed with different words (statements/commands) but the fundamental result will always be the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.