HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, a programming idiom or code idiom is a group of code fragments sharing an equivalent
semantic role In certain theories of linguistics, thematic relations, also known as semantic roles, are the various roles that a noun phrase may play with respect to the action or state described by a governing verb, commonly the sentence's main verb. For ex ...
, which recurs frequently across
software Software is a set of computer programs and associated software documentation, documentation and data (computing), data. This is in contrast to Computer hardware, hardware, from which the system is built and which actually performs the work. ...
projects often expressing a special feature of a recurring construct in one or more
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming l ...
s or
libraries A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
. Developers recognize programming idioms by associating and giving meaning (semantic role) to one or more syntactical expressions within
code snippet Snippet is a programming term for a small region of re-usable source code, machine code, or text. Ordinarily, these are formally defined operative units to incorporate into larger Module (programming), programming modules. Snippet management is a ...
s (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 In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
, or
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the rel ...
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 Fluency (also called volubility and eloquency) is the property of a person or of a system that delivers information quickly and with expertise. Language use Language fluency is one of a variety of terms used to characterize or measure a perso ...
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
adverb An adverb is a word or an expression that generally modifies a verb, adjective, another adverb, determiner, clause, preposition, or sentence. Adverbs typically express manner, place, time, frequency, degree, level of certainty, etc., answering q ...
ial or adjectival use of the term as ''using a programming language in a typical way'', which really refers to idiosyncratic. 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
umber_of_elements Umber is a natural brown earth pigment that contains iron oxide and manganese oxide. In its natural form, it is called raw umber. When calcined, the color becomes warmer and it becomes known as burnt umber. Its name derives from ''terra d'ombra ...
' 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. It has several implementations, among them the code fragments for C++: std::cout << "Hello World\n"; For
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
: 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. Code fragments: For Python: s.insert(i, x) For
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
: s.splice(i, 0, x); For
Perl Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
: splice(@s, $i, 0, $x)


See also

* Algorithmic skeleton * Embedded SQL (kind of "standard idiom" to use in any language) *
Idiom An idiom is a phrase or expression that typically presents a figurative, non-literal meaning attached to the phrase; but some phrases become figurative idioms while retaining the literal meaning of the phrase. Categorized as formulaic language, ...


References


External links


programming-idioms.org
shows short idioms implementations in most mainstream languages.
C++ programming idioms
from Wikibooks. {{DEFAULTSORT:Programming Idiom