Programming idiom

From Wikipedia, the free encyclopedia
(Redirected from Programming idioms)
Jump to navigation Jump to search

In programming and in code, an idiom describes a commonly-used way to code a relatively small construct in a particular programming context (i.e. programming language). Many such constructs are found in multiple programming contexts yet tend to vary by context.[1] Like a linguistic idiom, a programming idiom is a commonly-used way to express a concept in a language that exists outside the definition of the language yet is constrained by it.

Similar to a software design pattern, an idiom is a template to be followed, not code that can be copy-and-pasted into a codebase. In this sense, an idiom is a pattern, yet software design pattern is a classification reserved for significantly larger-scale functionality; usually involving the interaction of multiple objects.

Using the idioms for a programming context (instead of using idiosyncratic constructs) helps a team work together since they lower the cognitive load of the resulting code. Such idiomatic use is common in crowdsourced repositories to help developers overcome programming barriers.[2]

Examples

[edit | edit source]

Writing to standard output

[edit | edit source]

Writing to standard output is generally something covered early when learning a language; it is often presented through the task of writing a hello world program.[3]

A common idiom in C++ like:

std::println("Hello World");

For Java:

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

For Rust:

println!("Hello World");

Using dynamic memory

[edit | edit source]

In C, use the C dynamic memory allocation functions such as malloc() and free().

In C++, use the new and delete operators. The C dynamic memory allocation functions are usable in C++, but would generally be considered idiosyncratic.

See also

[edit | edit source]

References

[edit | edit source]
  1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
  2. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
  3. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
[edit | edit source]