[go: nahoru, domu]

Jump to content

Programming idiom

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Luminaxster (talk | contribs) at 15:57, 19 October 2017 (Added more concrete defintion and benefits. Differentiating the concept(idiom) from the implementation (code fragment).). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A programming idiom (AKA Code Idiom) is expressing a special feature of a recurring construct in one or more programming languages. Developers recognize programming idioms by associating and giving meaning to one or more code fragments. The idiom can be seen as a concept underlying a pattern in code, which is represented in implementation by contiguous or scattered code fragments. These fragments are available in several programming languages, frameworks or even libraries. 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, and transferring knowledge in the form of analogies from one language or framework to another.

Examples of simple idioms

Printing Hello World

One of the most common starting points to learn a program or notice the syntax differences between a known language and a new one. It has several implementations, among them the code fragments for C++:

#include <iostream> 
std::cout << "Hello World\n";" and for Java is "System.out.println("Hello World");"

For Java:

System.out.println("Hello World");

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.

See also