Programming idiom: Difference between revisions
add WP:NPOV, some of the stackexchange.com readers clearly disagree with definition given at Wikipedia |
Undid revision 722354788 by Ushkin N (talk): not properly formatted; stackoverflow is not a reliable source |
||
Line 2: | Line 2: | ||
Knowing the idioms associated with a programming language and how to use them is an important part of gaining [[fluency]] in that language. |
Knowing the idioms associated with a programming language and how to use them is an important part of gaining [[fluency]] in that language. |
||
== Distinction from design patterns == |
|||
* "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)'''"<ref>http://programmers.stackexchange.com/questions/106815/difference-between-idiom-and-design-pattern/106819#106819</ref> |
|||
==Examples of simple idioms== |
==Examples of simple idioms== |
||
Line 23: | Line 19: | ||
In [[object-oriented programming]] the implementation details of an [[API]]-level class can be hidden in an own implementation class. An [[opaque pointer]] (or reference) to that class is stored in the API-level class. |
In [[object-oriented programming]] the implementation details of an [[API]]-level class can be hidden in an own implementation class. An [[opaque pointer]] (or reference) to that class is stored in the API-level class. |
||
== References == |
|||
{{reflist}} |
|||
== External links == |
== External links == |
Revision as of 18:01, 30 May 2016
A programming idiom[clarification needed][citation needed] is a means of expressing a recurring[clarification needed] construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task, algorithm, or data structure that is not a built-in feature in the programming language being used, or, conversely, the use of an unusual or notable feature that is built into a programming language. The term can be used more broadly, however, to refer to complex algorithms or programming design patterns.
Knowing the idioms associated with a programming language and how to use them is an important part of gaining fluency in that language.
Examples of simple idioms
Set as associative array
In languages that do not support the set data type, but do support associative arrays, a set can be implemented as an associative array where the value is either irrelevant or is the unit type (or a sentinel value like 1
).
This is useful for example if one needs to perform an operation in which we often need to determine whether some arbitrary value is in a given list or array or not. Set membership in a list or (unsorted) array is an O(n) operation, as one needs to scan the list until the element is found or the end is reached. By contrast, lookup or membership in an associative array is frequently O(1), for example if it is implemented as a hash table.
The following idiom is commonly used to express this in Perl, which has fast associative arrays (implemented as hash tables), but no sets:
my %elements = map { $_ => 1 } @elements;
Pimpl idiom
In object-oriented programming the implementation details of an API-level class can be hidden in an own implementation class. An opaque pointer (or reference) to that class is stored in the API-level class.
External links
- programming-idioms.org shows short idioms implementations in most mainstream languages.
- C++ programming idioms from Wikibooks.