Programming idiom: Difference between revisions
Jerryobject (talk | contribs) m Per Wikipedia Style Manual: Decapitalized common nouns in internal links. Removed needless piping in 1 link. |
|||
Line 6: | Line 6: | ||
===Incrementing a counter=== |
===Incrementing a counter=== |
||
In a language like [[ |
In a language like [[BASIC]], the code to increment a counter by one is mundane: |
||
<code> |
<code> |
||
Line 12: | Line 12: | ||
</code> |
</code> |
||
The [[C programming language]] and many others derived from it have language-specific features that make this code shorter: |
The [[C (programming language)|C language]], and many others derived from it, have language-specific features that make this code shorter: |
||
<code language="C"> |
<code language="C"> |
||
Line 32: | Line 32: | ||
===Swapping values between variables=== |
===Swapping values between variables=== |
||
: ''Main article: [[ |
: ''Main article: [[swap (computer science)]]'' |
||
In many languages, code for swapping the values in two variables looks like the following: |
In many languages, code for swapping the values in two variables looks like the following: |
||
Line 98: | Line 98: | ||
===Array lookup hash table=== |
===Array lookup hash table=== |
||
Suppose we have an [[ |
Suppose we have an [[array data structure|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 [[big O notation|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 table]]s, as in [[Perl]]). |
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 table]]s, as in [[Perl]]). |
Revision as of 08:37, 18 October 2010
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 language, and many others derived from it, have language-specific features that make this code shorter:
i += 1; /* i = i + 1; */
++i; /* same result */
i++; /* same result */
There is a 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.
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".
Swapping 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
# same as
while (1) { do_something() };
# Using a "naked block" and the redo operator
{
do_something();
redo;
}
Ada loops forever this readable way:
loop
do_something;
end loop;
Also readable python syntax:
while True:
do_something()
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
- C++ programming idioms from Wikibooks.