Jump to content

Programming idiom

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Ted BJ (talk | contribs) at 22:04, 23 June 2022 (Fixing typo in disputed tag). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A programming idiom or code idiom is a group of code fragments sharing an equivalent semantic role,[1] which recurs frequently across software projects often expressing a special feature of a recurring construct in one or more programming languages or libraries. Developers recognize programming idioms by associating and giving meaning (semantic role) to one or more syntactical expressions within code snippets (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's semantic role is a natural language 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.

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.

A common misconception is to use the adverbial or adjectival use of the term as using a programming language in a typical way, which really refers to idiosyncratic.[disputeddiscuss] For example, an idiosyncratic way to manage dynamic memory in C would be to use the C standard library functions malloc and free, whereas idiomatic refers to dynamic memory allocation as recurring semantic role that can be achieved with code fragments malloc in C, or pointer = new type [number_of_elements] in C++. Common to both is that the code fragments are intelligible to somebody unfamiliar with C or C++, unless the code rationale is exposed to the developer.

Examples of simple idioms

Printing Hello World

One of the most common starting points to learn to program or notice the syntax differences between a known language and a new one.[2]

It has several implementations, among them the code fragments for C++:

std::cout << "Hello World\n";

For Java:

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

Inserting an element in an array

This idiom helps developers understand how to manipulate collections in a given language, particularly inserting an element x at a position i in a list s and moving the elements to its right.[3]

Code fragments:

For Python:

s.insert(i, x)

For JavaScript:

s.splice(i, 0, x);

For Perl:

splice(@s, $i, 0, $x)

See also

References

  1. ^ "Mining Idioms from Source Code. Miltiadis Allamanis, Charles A Sutton. FSE 2014: Proceedings of the 22nd ACM SIGSOFT International Symposium on Foundations of Software Engineering November 2014 Pages 472–483". arxiv.org.
  2. ^ "Print Hello World". www.programming-idioms.org.
  3. ^ "Insert element in list". www.programming-idioms.org.