Programming idiom

Programming idiom

A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task or algorithm 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 in to 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

Incrementing a counter

In a language like Basic, the code to increment a counter by one is mundane:

i = i + 1

The C programming language and many others derived from it have language-specific features that make this code shorter:

i += 1; /* i = i + 1; */ ++i; /* the same */ i++; /* the same */

(Technically, there is a slight difference between the first two expressions, which yield the new version of i, and the third, which yields the old version of i. When the expressions are used as isolated statements, as in this example, the yielded value is ignored, so the difference is irrelevant.)
Pascal, as a keyword-centric language, contains a built in procedure for the same operation:

i := i + 1; Inc(i); (* same *)

These are the "idiomatic" ways of "adding one to a counter".

wapping values between variables

: "Main article: Swap (computer science)"In many languages, code for swapping the values in two variables looks like the following:

temp = a; a = b; b = temp;

In Perl, the list assignment syntax allows a more succinct expression:

($a, $b) = ($b, $a);

Infinite loop

The code used to write an infinite (nonterminating) loop varies widely between different programming languages, although it often takes the form of a while loop where the test condition is always true. In Pascal, for example:

while true do begin do_something(); end;

There are several ways to write an infinite loop in C, including a loop very similar to the Pascal example, but the following idiom uses the unusual appearance of the empty for loop condition to draw attention visually to the loop:

for (;;) { do_something(); }

Perl allows the C syntax above, but supports some other syntax as well. For example:

do_something() while (1); # Succinct one-line infinite loop # Using a "naked block" and the redo operator { do_something(); redo; }

Array lookup hash table

Suppose we have an array of items, and we need to perform an operation in which we often need to determine whether some arbitrary item is in the array or not. Looking up an element in an array is an O(n) operation: we have to scan the array until we meet the element, or until the end.

Therefore, we create an associative array in which the array elements are keys, and the value is irrelevant. This assumes an implementation of associative arrays in which lookup is fast (e.g. hash tables, as in Perl).

The following idiom is commonly used to express this in Perl:

my %elements = map { $_ => 1 } @elements;

External links

* [http://en.wikibooks.org/wiki/C%2B%2B_Programming/Idioms C++ programming idioms] from Wikibooks.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Reflection (computer programming) — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computi …   Wikipedia

  • programming language — Language Lan guage, n. [OE. langage, F. langage, fr. L. lingua the tongue, hence speech, language; akin to E. tongue. See {Tongue}, cf. {Lingual}.] [1913 Webster] 1. Any means of conveying or communicating ideas; specifically, human speech; the… …   The Collaborative International Dictionary of English

  • Idiom (Softwaretechnik) — In der Softwaretechnik gehören Idiome zu den Mustern (englisch pattern). Buschmann definiert: „Ein Idiom ist ein programmiersprachenspezifisches Muster und damit ein Muster auf einer niedrigen Abstraktionsebene. Ein Idiom beschreibt, wie man… …   Deutsch Wikipedia

  • Criticism of the APL programming language — The APL programming language has been used since the mid 1960s on mainframe computers and has itself evolved in step with computers and the computing market. APL is not widely used, but minimalistic and high level by design, at several points in… …   Wikipedia

  • Oz (programming language) — Oz Paradigm(s) multi paradigm: logic, functional, imperative, object oriented, constraint, distributed, concurrent Appeared in 1991 Designed by Gert Smolka, his students Developer Mozart …   Wikipedia

  • Java (programming language) — infobox programming language name = Java paradigm = Object oriented, structured, imperative year = 1995 designer = Sun Microsystems latest release version = Java Standard Edition 6 (1.6.0) latest release date = latest test version = latest test… …   Wikipedia

  • Cargo cult programming — is a style of computer programming that is characterized by the ritual inclusion of code or program structures that serve no real purpose. Cargo cult programming is typically symptomatic of a programmer not understanding either the bug they were… …   Wikipedia

  • this (computer programming) — In many object oriented programming languages, this (also called self or Me) is a keyword that is used in instance methods to refer to the object on which they are working. C++ and languages which derive in style from it (such as Java, C#, and… …   Wikipedia

  • Fusebox (programming) — Fusebox is a web application framework for ColdFusion and PHP. Originally released in 1997, it is currently in its fifth major incarnation. The current version, Fusebox 5.5, was released to the world at the beginning of December 2007.Fusebox is… …   Wikipedia

  • Opaque pointer — In computer programming, an opaque pointer is a special case of an opaque data type, a datatype that is declared to be a pointer to a record or data structure of some unspecified type. Opaque pointers are present in several programming languages… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”