[go: nahoru, domu]

Jump to content

Programming idiom: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Ripounet (talk | contribs)
Link to programming-idioms.org
normalize indentation to 2 spaces and replace code blocks with <source> tag
Line 9: Line 9:
In virtually all languages that support [[Assignment (computer programming)|assignment]], a counter can be incremented using code such as this (for example in [[BASIC]]):
In virtually all languages that support [[Assignment (computer programming)|assignment]], a counter can be incremented using code such as this (for example in [[BASIC]]):


<pre>
<source lang=freebasic>
i = i + 1
i = i + 1
</source>
</pre>


As this is an extremely common operation, many languages provide specialized [[Operator (computer programming)|operators]] for this, notably the [[C (programming language)|C language]], and many others derived from it:
As this is an extremely common operation, many languages provide specialized [[Operator (computer programming)|operators]] for this, notably the [[C (programming language)|C language]], and many others derived from it:


<source lang=C>
<source lang=C>
i += 1; /* i = i + 1; */
i += 1; /* i = i + 1; */
++i; /* same result */
++i; /* same result */
i++; /* same result */
i++; /* same result */
</source>
</source>


Line 26: Line 26:


<source lang=Pascal>
<source lang=Pascal>
i := i + 1;
i := i + 1;
Inc(i); (* same *)
Inc(i); (* same *)
</source>
</source>


Line 36: Line 36:
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:


<code>
<pre>
temp = a;
temp = a;
a = b;
a = b;
b = temp;</code>
b = temp;
</pre>


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


<code>
<pre>
($a, $b) = ($b, $a);</code>
($a, $b) = ($b, $a);
</pre>


Or even more concise in [[Python (programming language)|Python]],
Or even more concise in [[Python (programming language)|Python]],


<code>
<pre>
a, b = b, a</code>
a, b = b, a
</pre>


===Infinite loop===
===Infinite loop===
Line 55: Line 58:
The code used to write an [[infinite loop|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:
The code used to write an [[infinite loop|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:


<source lang=Pascal>
<code>
while true do begin
while true do begin
do_something();
do_something();
end;</code>
end;
</source>


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:
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:


<source lang=C>
<code>
for (;;) {
for (;;) {
do_something();
do_something();
}</code>
}
</source>


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


<source lang=Perl>
<code>
do_something() while (1); # Succinct one-line infinite loop
do_something() while (1); # Succinct one-line infinite loop
# same as
# same as
while (1) { do_something() };</code>
while (1) { do_something() };
</source>


<source lang=Perl>
<code>
# Using a "naked block" and the redo operator
# Using a "naked block" and the redo operator
{
{
do_something();
do_something();
redo;
redo;
}</code>
}
</source>


Ada loops forever this readable way:
Ada loops forever this readable way:


<source lang=Ada>
<code>
loop
loop
do_something;
do_something;
end loop;</code>
end loop;
</source>


Also readable python syntax:
Also readable python syntax:


<source lang=Python>
<code>
while True:
while True:
do_something()</code>
do_something()
</source>


===Set as associative array===
===Set as associative array===
Line 102: Line 111:


<source lang=Perl>
<source lang=Perl>
my %elements = map { $_ => 1 } @elements;
my %elements = map { $_ => 1 } @elements;
</source>
</source>



Revision as of 07:47, 13 September 2015

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, 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

Incrementing a counter

In virtually all languages that support assignment, a counter can be incremented using code such as this (for example in BASIC):

  i = i + 1

As this is an extremely common operation, many languages provide specialized operators for this, notably the C language, and many others derived from it:

  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 instead provides 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);

Or even more concise in Python,

  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()

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 OOP the implementation details of an API-level class can be hidden in an own implementation class. A pointer (or reference) to that class is stored in the API-level class.

Read more in the article Opaque pointer.