Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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"?

share|improve this question
    
This belongs as much on english.stackexchange.com as it does here. –  S.Lott Jul 20 '11 at 19:43
    
@perl.j: Insightful. You've really clarified why the definition of "Idiomatic" doesn't belong in english.stackexchange.com. Very helpful. –  S.Lott Nov 22 '11 at 19:11

7 Answers 7

up vote 13 down vote accepted

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.

share|improve this answer
    
Excellent description. So in C#, if I create an extension method called TryAdd() on Generic Dictionary class to first check !Contains() and then call Add(), this would be idiomatic? I'm using a feature specific to C# - extension methods. –  Robert Dailey Jul 20 '11 at 16:51
1  
I'm not sure exactly as most experienced C++ coder would tell you to write free functions to extend classes instead of adding members, where possible. It's not the same as C# extension methods but it's pretty close and available to any language that allow free functions. What I'm sure is that writting "set/getters" in C# by using properties IS idiomatic. –  Klaim Jul 20 '11 at 17:11
    
But this isn't C++, so using the "C++ way" might not be "idiomatic" enough. Using an extension method here is reasonable and very C#-like. In any case it was an example but extension methods are very idiomatic to C#. –  Robert Dailey Jul 20 '11 at 18:06
    
Yes that's why I said "I'm not sure", it feels idiomatic but it's so close to other basic features of other languages that I'm not sure (because I'm not a C# dev). I have another example : looping is done by recursion in all functional languages. You can do it in other languages, but clearly it's idiomatic to functional languages. Not sure if it helps. –  Klaim Jul 20 '11 at 18:14
1  
Of course python goes one step further by having an idiomatic way of referring to idiomatic code, i.e. pythonic. *8') –  Mark Booth Jul 22 '11 at 16:00

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

share|improve this answer
    
If I thought the "google" definitions for this term was helpful I wouldn't have posted here. I wanted a better description (tutorial-like) with examples. –  Robert Dailey Jul 20 '11 at 16:49
2  
@Robert - I guess I don't understand why you don't understand. –  ChaosPandion Jul 20 '11 at 16:53
1  
I read your answer first, it wasn't quite clear. After reading the response that is now marked as my answer, it became very clear what "idiomatic" meant and I now understand it. Now that I read your answer it makes sense. However, you didn't walk me through examples and different scenarios, which is why I think your answer wasn't as helpful. You gave a "webster" response which is technically correct but not helpful as far as helping me understand. I appreciate your help though. –  Robert Dailey Jul 20 '11 at 16:57

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 :/)

share|improve this answer

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.

share|improve this answer
    
How is collection.each do |current| different from foreach(current in collection) ? Both are loop constructs that set current to each element of collection. –  MSalters Jul 21 '11 at 9:42

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.

share|improve this answer
    
Wikipedia used incrementing as an example, so that's why I mentioned it in my question. Why are idioms required to be complex? This doesn't seem like part of the definition based on the other answers. –  Robert Dailey Jul 20 '11 at 16:53
4  
@Robert Dailey: How could incrementing possibly be an idiom? It's part of the language. Idioms are made by a language's users, they change over time. Incrementing is not an idiom, it's a basic functionality of the language. Taking that logic, I could say that any other basic language function is an idiom, and therefore every program is idiomatic, which it is not. –  DeadMG Jul 20 '11 at 17:26

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.

share|improve this answer
1  
The idiomatic approach would surely be to declare the pointer in the if condition, so that it can never be accessed if it's not NULL. if(Foo* p = SomeThingThatReturnsAFooPointer(arg, param, x, y)) –  DeadMG Jul 20 '11 at 21:24
    
mm, probably. Or a thousand lines earlier because it's used throughout this block. Or in a header file because it's a member variable. I only declared it so readers would know it was a Foo*, you're right that in real life it wouldn't be declared there. –  Kate Gregory Jul 20 '11 at 22:06

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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