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 22:30, 12 February 2023 (added evidence of how idioms are used.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer programming, 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. Such idiomatic knowledge is widely used in crowdsourced repositories to help developers overcome programming barriersCite error: A <ref> tag is missing the closing </ref> (see the help page).

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.[2]

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. ^ "Insert element in list". www.programming-idioms.org.