41

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

5
  • In practical terms the distinction may be difficult to determine (and there may be some continuum between an idiom and a design pattern). But it probably derives from the term "idiom" in natural language: en.wikipedia.org/wiki/Idiom (which doesn't really fit the usage). Sep 8, 2011 at 6:31
  • 2
    This is probably better suited for Programmers SE. Sep 8, 2011 at 8:00
  • 3
    @Nawaz: a "Design Pattern" is a relatively "high-level" construct around a language defect. An "idiom" is a relatively "low-level" construct around a language defect ; ) Sep 8, 2011 at 11:03
  • @Nawaz- +1 for humor.
    – Jennifer S
    Sep 8, 2011 at 13:29
  • See also Idiom vs. pattern. May 2, 2017 at 9:50

10 Answers 10

32

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.

3
  • 2
    Another interesting example: Fluent syntax APIs. They make up for the fact that you don't have direct DSL support in your language, and it steps across language boundaries. I am not sure it has graduated to a "Design Pattern" status yet, and it rings like a syntactic idiom... Sep 8, 2011 at 8:21
  • The fact that C# directly supports the Observer pattern would seem to indicate that it does have a huge need for the pattern, so much so that it shifted the implementation away from developers and into the language itself.
    – jaco0646
    Mar 23, 2018 at 16:25
  • @jaco0646 I reworded that line, might be more clear now Mar 24, 2018 at 2:52
37

Design patterns are not usually language specific. Language idioms tend to depend on particular feature of a language (or class of languages) or work around a specific deficiency in said language(s).

4
  • +1 for drawing a distinction with few words and few inaccuracies. Sep 8, 2011 at 6:49
  • 1
    @Merlyn Morgan-Graham: You've commented on almost all answers, including the question. So why don't you post a detail answer with no inaccuracies at all? I would like to know your thoughts.
    – Nawaz
    Sep 8, 2011 at 6:57
  • 1
    @Nawaz: The comment was about the other answers. This one is already nearly perfect. I'd ideally like samples in an answer, but it seems no one is getting them right. I probably couldn't either, otherwise I'd happily oblige :) Sep 8, 2011 at 7:01
  • @Nawaz: Okay I gave it a shot anyway :) Sep 8, 2011 at 7:39
12

I wouldn't put too much currency in the Wikipedia definition.

At the very least an idiom is language-specific whereas a design pattern strives, or should strive, to be language-agnostic. Going further, idioms are usually conventions for enhancing readability, or are the superior alternative (on some technical merit) when there is more than one way to do something. All of those things are related on how ideas are expressed (clarity, verbosity, conciseness), but not to the ideas themselves.

On the other hand, design patterns are the essence of a recurring idea, an idea that a priori can be expressed in any languages that lends itself to it. The Visitor is an implementation of double-dispatch relying on single-dispatch and overloading and this can be used in any language that has single-dispatch and overloading. Knowing about the pattern doesn't help write more expressive or more readable code, it helps solving the related problem. There is nothing idiomatic about it because for instance there is no canonical form of the Visitor in e.g. C++.

6
  • 1
    +1; Good answer. Tho I'm inclined to believe everything here except the last bit. There are languages that have direct support for multiple-dispatch, so the Visitor pattern wouldn't need to exist there. From their perspective, the "pattern" might be more of an idiom. From the ultimate high-level language, all patterns might become idioms... Sep 8, 2011 at 7:08
  • @Merlyn What's idiomatic about reimplementing a first-class language feature? Who does that?
    – Luc Danton
    Sep 8, 2011 at 7:10
  • That's the point. Users of that language would consider the "design pattern" an idiom, because their language is cooler :) Sep 8, 2011 at 7:52
  • @Merlyn That doesn't fit my uses of 'idiom'. An idiom of a language is something you expect a random, proficient user of the language to recognize; reimplementing a first-class feature will look alien and out-of-place. There's no C++ idiom for single dispatch, one just uses virtual in some places whereas hand-written pointer to members table trickery will look just silly.
    – Luc Danton
    Sep 8, 2011 at 8:01
  • 1
    I like where you're going with the definition of idiom being different than a "poor man's design pattern", which is sort of how my model treats it (see my answer). It is less "here is how to implement this" and more "here is the right way to implement this". For example, I can't imagine "Big Three" evolving to a design pattern. And there is the syntactic component, too, e.g. do_something() or die "..."; (stolen from another comment on here). It is based on specific language features, but it is a common way to use those features. It is not cross-language, and probably won't be. Sep 8, 2011 at 8:10
6

The normal English language definition of Idiom. Is a phrase whose accepted meaning is not contained in the words used. Examples would be "Raining cats and Dogs" or "Where's the Beef?"

In programming languages it usually refers to a syntactical shortcut that does something not immediately obvious from the code itself but which is used often enough that other programmers recognize the meaning instantly.

Perl is perhaps the most idiom rich language. With constructs like:

while (<IN>) {
    print $_
}

Whose meaning is obvious to an experienced perl programmer but a mystery to anyone else

2
  • I think "where's the beef" is more a meme than an idiom. Might have another continuum there ;) There might be more to an idiom than simply the implementation in the language, since things might be partial or failed implementations of that idiom - C++'s "Big Three", for example. In that case, the idiom is the name and description of "Big Three". Sep 8, 2011 at 6:48
  • 2
    Perl has the best idioms - do_something() or die "arrrrgh!";
    – cxfx
    Sep 8, 2011 at 7:19
2

Idioms are language specific. E.g. while (*dest++=*src++); is a C/C++ idiom. It is completely impossible to write something remotely similar in Pascal or Java. Use the word "idiom" as you use it in english. "How do you do?" as a greeting is an idiom. Some languages like german and frensh have the same idiom. But lots of other languages would not "ask" something like this as a greeting. A (object oriented) pattern on the other hand usually can be adapted into any language that supports inheritance and delegation. A idiom might be as simple as one line of code. A design pattern always involves several classes.

1
  • +1. Good point : A idiom might be as simple as one line of code. A design pattern always involves several classes.
    – Nawaz
    Sep 26, 2011 at 12:39
2

I found this post searching for common C++ idioms, as I have been getting fairly deep into it recently and would like my code to not look as amateurish as I feel it is... :-P

Having spent quite a bit of time with Perl, I've found idioms in that language to be much like those found in natural languages, like English or Spanish (only two I know well enough to know some idioms).

I disagree that an idiom is like a "small design pattern". I still disagree, although less so, that an idiom is a way to work around a deficiency in a language.

Perhaps Luc Danton's answer comes closest, but let me explain. I think that an idiom is, well, idiomatic of those who use the language. Usually, a common expression or sequence of expressions that, while perhaps not obvious, performs an operation or expresses intent in a way that makes sense to those who are fluent enough to have seen it before.

Back to Perl, perhaps the best-known idiom is the "Schwartzian Transform", an expression that performs a sort on data in a compact and efficient manner. It is not the most obvious way to perform such an operation, but it is succinct and those who have seen it before know instantly what it is doing.

Another notable example is "The Orcish Maneuver", which takes advantage of perl's notions of true/false, rich operators, and operator precedence.

One that I personally like quite a bit is somewhat related to the Orcish Maneuver, but I know of no name for it:

push @{ $some_hash{$key} ||= [] }, $some_value;

This is indeed not obfuscation, but rather a clear, compact expression of something that would otherwise take several lines to do. If the key is present in the hash and has a true value, de-reference it as an array and push $some_value onto that array. If the hash element is not present or has a false value, assign it an empty array, then dereference that array and do the push.

It's also worth noting that as of Perl 5.14, part of this idiom is obsolete - push can now operate directly on the array reference, no @{} needed! Also, as of Perl 5.10, one can use //= instead of ||= which checks not for truth but for defined-ness.

1
  • in python: some_dict.setdefault(key, []).append(some_value). Jun 3, 2013 at 5:09
0

the point of an Idiom is to be an idea or concept that spans programming languages, it is a way of doing things and a process that works without much conceptual restructuring when you take it from one and stick it in another, like the lowly bubble sort. Design patterns are specific implementations of an idiom, or extentions of the idea to fit into a language, you therefore have the javascript design pattern of the event-listener idiom etc.

2
  • event listener is a language feature that implements signal/slots (pattern?) or the Observer pattern. There are also things in C++ called idioms that simply don't matter or don't fit in other languages (as far as I know) - e.g. the copy-and-swap idiom. Sep 8, 2011 at 6:33
  • Design patterns are specific implementations of an idiom? How exactly? Did you see the C++ idioms, in the link in my question?
    – Nawaz
    Sep 8, 2011 at 6:40
0

I'm not 100% sure, but idioms are simply terms pertaining to a certain field. When you say "design patterns", you think of "Observer Pattern" "Chain of responsability" "Visitor pattern" "factory". These are common patterns used to solve common problems in programming. Look here for a comprehensive list: http://en.wikibooks.org/wiki/C++_Programming/Code/Design_Patterns

0

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

I take it to mean that it is not a high level, abstract way to model your application or components of your application, but instead a clever or common use of language semantics.

for example, setting a variable if it evaluates to false (commonly used to set nil variables conditionally):

var ||= some_default_value
0

Here's an example of an idiom (in C#) for handling an event. You're not allowed to fire an event if it doesn't haven't any handlers attached, so the idiom is to always check this first.

Therefore the general idiom for handling events becomes:

EventHandler handler = this.MyEvent;
if ( null != handler ) { handler( param1, param2 ); }

This idiom is specific (although not exclusive) to the C# language.

More generally however, the C# eventing mechanism is an example of the observer design pattern which could be implemented in any language.

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.