44

I understand an "idiom" to be a common operation or pattern that in a particular language is not simplified by core language syntax, such as integer increment:

i = i + 1;

In C++, this idiom is simplified by an operator:

++i;

However, when someone uses the term "idiomatic", I am not sure how to understand it. What makes a piece of code "idiomatic"?

void.pointer
  • 5,113

7 Answers7

51

An idiomatic way of writing some code is when you write it in a very specific way because of language-specific idioms that other languages don't have.

For example, in C++, exploiting the RAII idiom leads to ways of writing C++ code managing resources that are idiomatic.

Another example would be using list comprehensions in Python to generate lists. It's idiomatic because you would have used list comprehension in idiomatic Python but you could have done the same using a generator function in any other language or even in Python.

Often, someone trying a new language without using the idioms specific to this language will not write idiomatic code.

Robert Harvey
  • 200,592
Klaim
  • 14,902
  • 4
  • 51
  • 62
23

Normal Definition

Using, containing, or denoting expressions that are natural to a native speaker

Programming Definition

Using, containing, or denoting expressions that are natural to a [Insert Language] programmer

ChaosPandion
  • 6,313
15

Idiomatic in the context of programming can usually be defined as "the most natural way to express something in a language"

I see the word idiomatic come up particularly a lot in Ruby. In Ruby, there are massive meta-programming capabilities, and to write idiomatic Ruby code, you must usually use these.

Note that just because a piece of code is idiomatic, does not mean that it is clean or even concise. Many times you must make compromises.

So basically, idiomatic most commonly refers to the most common way to write something in a language, often times including "slang" (idioms). If a piece of code is not idiomatic, it may be perfectly readable, concise, clean, and correct, but it may feel/look awkward in the language used. It is a preference of taste as to whether rewriting such a piece of code idiomatically would actually be a good thing and must be judged on a case by case basis

Idiomatic English for example can depend on the region. Using the word bum is probably required for idiomatic UK English, while butt is more appropriate for US English. (I don't know much UK English sadly :/)

Earlz
  • 23,048
3

The best example I can devise, off the top of my head, for something that's idiomatic, comes from Ruby.

In a wealth of languages, you can iterate with:

for( start; end; increment){
    code;
}

Or something very similar. Many languages also have a foreach(x in SetOfXes) construct. But iteration that's idiomatic to Ruby includes things like:

collection.each do |current|
  current.do_stuff
end

and even

10.times do |x|
  puts x
end

I see these as idiomatic because they represent a way of doing something that is less common, and that represents something at the core of the philosophy of its domain. Ruby has a philosophy about interaction with objects that, while not unique, is not ubiquitous.

On a semantic note, whenever I hear the word idiomatic, my brain automatically completes the thought as "idiomatic to ..." - I just reflexively parse it as relating to a specific subject.

asfallows
  • 2,341
  • 19
  • 18
2

Idioms are usually the best possible way of expressing a common, relatively complex situation in a language. Incrementing is not an idiom or anything of the sort. Using prefix increment instead of postfix, you could argue to be a C++ idiom.

Idioms are the best way to use a language, as determined by expert users. A real C++ idiom would be function objects. Another idiom would be RAII. There's nothing in the language telling you that you must free resources in the destructor. But it's idiomatic to do so. Another example is templates- it's idiomatic to use templates for, well, everything you can, but there's nothing stopping you over-using inheritance.

DeadMG
  • 36,914
2

Your definition doesn't strike me as correct. An idiom is a way of writng something that may or may not be possible in other languages, but that is commonplace in this language. Usually, it's shorter than the alternative, but that's not really a requirement.

It might be easier to explain it by talking about what is non-idiomatic. In C++, it's pretty idiomatic to write:

Foo* p = SomeThingThatReturnsAFooPointer(arg, param, x, y);
if(p)
{
 // whatever
}

It's even more idiomatic to write:

Foo* p; 
if(p = SomeThingThatReturnsAFooPointer(arg, param, x, y))
{
 // whatever
}

This code does exactly the same thing - some folks who are new to C++ might read it as testing to see if p is equal to what the function returns, but that's not what it does.

Compare to what someone might write, very non-idiomatically, who has come from another language:

Foo* p = SomeThingThatReturnsAFooPointer(arg, param, x, y);
if(p !=NULL)
{
 // whatever
}

You'll also see this stuff knocked as non-idiomatic:

if (x>0)
  return true;
else
 return false;

Because the idiomatic approach is

return (x>0);

The non-idiomatic ways aren't wrong, but they usually take longer to type and they always take longer to read, for those who know the idioms. If I call you "the boy who cried wolf" and you know the story, it's quicker than if I explain about how false alarms cause people to ignore you. The problem, of course, is if you don't know the story and don't know what wolves have to do with what we're talking about. Similarly, it can be a problem if you've never seen return x<y; before and really don't know what it does.

Kate Gregory
  • 17,495
1

When there are multiple ways of expressing something, the most common or commonly accepted way. For instance, using a structured statement in C instead of the exact equivalent using goto statements.

hotpaw2
  • 7,988