Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is idiomatic code?

Tags:

c#

People also ask

What does idiomatic python mean?

Idiomatic Python is what you write when the only thing you're struggling with is the right way to solve your problem, and you're not struggling with the programming language or some weird library error or a nasty data retrieval issue or something else extraneous to your real problem.

What is idiomatic style writing?

An idiomatic expression is a turn of speech that makes sense in one language, but if translated literally into another no longer makes sense. Some examples: put out the cat, put out the light, take a walk, take a bath, take a moment. The use of English prepositions is largely idiomatic.

What is idiomatic and non idiomatic?

Idiomatic and non-idiomatic meaningsThe same expression or phrase can be used in more than two contexts. The word "break" has many meanings. The literal meaning is called non-idiomatic meaning and the other formed phrases (noun + preposition / verb + preposition) have idiomatic meanings.

What is idioms in Java?

Java 5 Idioms The word 'Idiom', in this context, should be taken to mean something like 'Common Practice'. This page isn't an appropriate place to put patterns, it's a place to put example of common solutions to java coding problems. If the Idiom linked to off this page has no code, then its a pattern.


Idiomatic means following the conventions of the language. You want to find the easiest and most common ways of accomplishing a task rather than porting your knowledge from a different language.

non-idiomatic python using a loop with append:

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

idiomatic python using a list comprehension:

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

Some examples:

Resource management, non idiomatic:

string content;
StreamReader sr = null;
try {
    File.OpenText(path);
    content = sr.ReadToEnd();
}
finally {
    if (sr != null) {
        sr.Close();
    }
}

Idiomatic:

string content;
using (StreamReader sr = File.OpenText(path)) {
    content = sr.ReadToEnd();
}

Iteration, non idiomatic:

for (int i=0;i<list.Count; i++) {
   DoSomething(list[i]);
}

Also non-idiomatic:

IEnumerator e = list.GetEnumerator();
do {
   DoSomenthing(e.Current);
} while (e.MoveNext());

Idiomatic:

foreach (Item item in list) {
   DoSomething(item);
}

Filtering, non-idiomatic:

List<int> list2 = new List<int>();
for (int num in list1) {
  if (num>100) list2.Add(num);
}

idiomatic:

var list2 = list1.Where(num=>num>100);

Idiomatic code is code that does a common task in the common way for your language. It's similar to a design pattern, but at a much smaller scale. Idioms differ widely by language. One idiom in C# might be to use an iterator to iterate through a collection rather than looping through it. Other languages without iterators might rely on the loop idiom.


Practically speaking, it means writing code in a consistent way, i.e. all developers who work on your code base should follow the same conventions when writing similar code constructs.

So the idiomatic way is the way that matches the style of the other code, non-idiomatic way means you are writing the kind of function but in a different way.

e.g. if you are looping a certain number of items, you could write the loop in several ways:

for (int i = 0; i < itemCount; i++)

for (int i = 1; i <= itemCount; i++)

for (int i = 0; i < itemCount; ++i)

etc

What is most important is that the chosen style is used consistently. That way people become very familiar and confident with how to use it, and when you spy a usage which looks different it can be a sign of a mistake being introduced, perhaps an off by one error, e.g.

for (int i = 1; i < itemCount; i++)