What is "idiom expression" in programming?

13

I have already come across the term "idiomatic expression" or "more idiomatic form". Just search the site that will find several references: link

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

    
asked by anonymous 15.02.2017 / 12:54

1 answer

11

It is related to elegant programming, yes. It is not elegant to produce non-idiomatic codes. But elegance is not everything in programming.

The expression or idiomatic form is the most elegant way to do a common task in that language.

Let's take English.

  

I separated from my girlfriend

Could be translated as:

  

I splited with my girlfriend

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

  

I broke up with my girlfriend

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

A language can approach a design pattern, but it can not be because it is something more specific. Some people may mistake 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 confused with a cake recipe, but language is more about the form of writing than the task itself.

Language here means a specific way to use a language. It is the peculiar way of expressing something in a particular language.

Non-idiomatic codes are usually produced when a solution is obtained from one language and port on the other. Or if the programmer experienced in another language is not accustomed to the standard of that language. Or even if it's producing codes, but it's not really programmer. Even similar languages like Java and C # have very different languages.

Some languages benefit greatly from idiomatic codes, others are more about aesthetics, and conformation with that specific community.

C #

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

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

You may prefer to scan collections with for instead of foreach or even LINQ, but it is not idiomatic.

You can use the Observer project pattern instead of a event which is idiomatic.

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

You can create your own naming pattern , 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 will in many cases replace 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, this way pythonic :

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

Universal languages

It has languages that are not so inherent in language. An example is the fluent interface .

Conclusion

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

A language dictionary of a language could group the most diverse forms of writing the code of certain tasks that are common for a programmer. I miss this on the internet.

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

You have a Wikipedia article on the subject.

    
15.02.2017 / 13:38