Design Patterns – Difference Between Idiom and Design Pattern

design-patternsidiomsterminology

What is the difference between idiom and design-pattern? It seems that these terminologies overlap somewhere; where exactly, I don't know. Are they interchangeable? When should I use what?

Here is a list of C++ Idioms. Can I call them design patterns?

Wikipedia defines,

Programming Idiom as a low-level Design Pattern

What does it mean? What does "low-level" mean here?

This question is inspired from another question : https://stackoverflow.com/questions/7343531/are-the-some-design-patterns-language-dependent

Best Answer

An idiom is an idea to work around the quirks of a language. Some examples that come to mind are any of the C++ idioms you linked in the original question. They solve a common problem in that language in a canned way.

A design pattern is similar, in that it solves a common problem. But the ideal design pattern is based on common language features, and thus is language agnostic.

There is a continuum between idioms and design patterns, though, just as there is from low-level to high-level languages.

The Visitor pattern is a good example; if there were only one language that only supported single dynamic-dispatch, then we might consider the Visitor pattern an idiom of that language. But there are whole hordes of languages that don't directly support multiple-dispatch. Hence, the Visitor pattern was born.

The Observer pattern also comes to mind - C# directly supports it, so it doesn't need the common work-around form of the pattern.

An example going the other direction is OO features (inheritance, polymorphism, etc). C doesn't directly support them. If more languages were like C, then we might develop design patterns to implement v-tables, type-safety, etc. Since plenty of languages support those feature, we'd call any common solution in C an idiom, rather than calling the generalized solution a design pattern.