What is "idiomatic expression" in programming?

Asked

Viewed 750 times

19

I have come across the term "idiomatic expression" or "more idiomatic form". Just search the site you will find several references: /search?q=idiomatica

After all, what is "idiomatic expression"? It is related to elegant programming?

  • 3

    https://pt.wikipedia.org/wiki/Express%C3%A3o_idiom%C3%A1tica

1 answer

17


It has relationship with elegant programming yes. It is not elegant to produce non-idiomatic codes. But elegance is not everything in programming.

Idiom or idiom is the most elegant way of doing a common task in that language.

Let’s get the Englishman.

I split up with my girlfriend

It could be translated as:

I splited with my girlfriend

This can be understood, but it is not idiomatic. It would be better to speak:

I Broke up with my girlfriend

In programming it’s like that too, you can do something that works well, the code runs and produces the expected result always, probably efficiently, but in that particular language has a more elegant way of doing, may even generate more efficiency or avoid some problem that only exists in this language.

A language can approach a project pattern (Pattern design), But it’s not enough because he’s something more specific. Some people might confuse it with good practice, and in a sense it is, at least in the form of writing the code for a very simple task. It could also be mistaken for a cake recipe, but language is more about the writing form than the task itself.

Language here means a specific way of using a language. It is the peculiar way of expressing something in a given language.

Nonidiomatic codes are often produced when a solution is picked up from one language and port from another. Or if the experienced programmer in another language is not used to the standard of that language. Or if he’s producing code, but he’s not really a programmer. Even similar languages, such as Java and C#, have very different languages.

Some languages benefit greatly from idiomatic codes, others occur more by aesthetics, is conformation with that specific community.

C#

For example, you can program in C# 100% imperative, abandon OOP. But it is not idiomatic.

Or you can abuse dynamic or object to make the application more flexible, but it is not idiomatic.

You may prefer to scan collections with for in place of a foreach or even LINQ, but is not idiomatic.

You can use the project pattern Observer and not a event which is the idiomatic.

Note that writing in a bad way goes beyond not being idiomatic. If instead of using using to have resources, use a try-finally is not idiomatic, but if you do not use the try-finally, then it’s a mistake anyway.

You can create a nomenclature standard its, but the language has a pattern that is more idiomatic.

C# has a modern idiomatic form and another legacy form. And this changes in each version. For example, in C# 7 tuples have in many cases replaced at least the use of Tuple<> and out in parameters.

Python

A non idiomatic example in Python:

mylist = [1, 2, 3, 4]
newlist = []
for i in mylist:
    newlist.append(i * 2)

Now idiomatic, said way pythonic:

mylist = [1, 2, 3, 4]
newlist = [(i * 2) for i in mylist]

I put in the Github for future reference.

Universal languages

There are languages that are not so inherent to the language. An example is the interface Fluent.

Completion

People don’t always agree on the idiomatic way. And the same language can have different ways. Often there is the idiomatic way legacy and the modern way with the advent of some new way of expressing that more elegantly. And some domains may require a specific language.

A language dictionary of a language could group together the most diverse ways of writing the code of certain tasks that are common to a programmer. I miss it on the Internet.

Professional programmers seek to make the idiomatic way as far as it makes sense. Amateur programmers make at the base of the Bumba-meu-boi. Professional programmers do not use the languages without questioning them first and know when to avoid the language in favor of a better solution for that case. The professional knows which language to use.

Has a wikipedia article on the subject.

  • I haven’t accepted yet to see if there’s another answer. Great answer.

  • 1

    @Murilo is always good to wait even, don’t be in any hurry. I wish I had other answers myself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.