You get a bonus - 1 coin for daily activity. Now you have 1 coin

Software idiom

Lecture



A software idiom is a way of expressing some non-elementary construct typical of one or more programming languages. Generally speaking, an idiom is a typical way to express a task, an algorithm or a data structure that is not embedded in the language at the primitive level, or vice versa, a non-trivial way to use the built-in language elements.

The term is often used in a broad sense, including as a synonym for the term design pattern, and knowledge of idioms in this case often serves as an indicator of fluency in this language.

In a narrower sense, an idiom can be a reproduction in the language of elements of a semantically different language, which in this language can also be applicable, but not provoked by the language itself (that is, not included in its idioms). From this position, many patterns in object-oriented design are considered as ideomatic reproduction of elements of functional languages ​​[.

Simple idioms examples

Increment

In languages ​​like BASIC, the idiom for a single increment of a variable's value is:

  i = i + 1

A shortened version in C-like languages:

  i + = 1;  / * i = i + 1;  * /
  ++ i;  / * same result * /
  i ++;  / * same result * /

Pascal contains a procedure for a similar purpose:

  i: = i + 1;
  Inc (i);  (* same result *)

Swap

Idiom copy-and-swap

In most languages, the exchange of values ​​between two variables is as follows:

  temp = a;
  a = b;
  b = temp;

In Perl, it looks more elegant:

  ($ a, $ b) = ($ b, $ a);

Endless cycle

Pascal:

  while true do begin
    do_something ();
  end;

There are many ways to organize infinite loops in C-like languages, but the following example most clearly shows this:

  for (;;) {
    do_something ();
  }

Perl supports both C-syntax and other options:

  do_something () while (1);  # Short Endless Loop
  similarly
  while (1) {do_something ()};

  # Using a "naked block" and the redo operator
  {
    do_something ();
    redo;
  }

Ada:

  loop
    do_something;
  end loop;

Python:

    while true:
        do_something ()

Sampling from an associative array

In many languages ​​there is an implementation of an associative array, the so-called. hash table.

Implementing an associative array in Perl:

  my% elements = map {$ _ => 1} @elements;

Criticism

Zed Shaw, the author of the book Learn Ruby The Hard Way, notes that attitudes towards idioms in the developer community suggests that the use of idioms should be categorized as morality, because when writing in natural language that requires clarity of presentation, idioms should be avoided [3 ]. He gives the following example from Ruby Styleguide [4]:

 arr = [1, 2, 3]
 
 # bad
 for elem in arr do
   puts elem
 end
 
 # good
 arr.each {| elem |  puts elem}

Using the for loop according to the official Ruby style is not welcome, and the .each construct should be used instead. Shaw's position is that learners of programming languages ​​should study not only the idioms of a particular programming language, but also alternative coding methods - this avoids indoctrination and facilitates the transition to other languages.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Software and information systems development

Terms: Software and information systems development