C# 什么是惯用代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/84102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:23:29  来源:igfitidea点击:

What is idiomatic code?

提问by MrBoJangles

I'd be interested in some before-and-after c# examples, some non-idiomatic vs idiomatic examples. Non-c# examples would be fine as well if they get the idea across. Thanks.

我会对一些前后 c# 示例、一些非惯用与惯用示例感兴趣。如果非 c# 示例能理解这个想法,它们也会很好。谢谢。

采纳答案by Corey Goldberg

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:

使用带有 append 的循环的非惯用 python:

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

idiomatic python using a list comprehension:

使用列表理解的惯用 python:

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

回答by Bill the Lizard

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.

惯用代码是为您的语言以常见方式执行常见任务的代码。它类似于设计模式,但规模要小得多。习语因语言而异。C# 中的一个习惯用法可能是使用迭代器来遍历集合而不是循环遍历它。其他没有迭代器的语言可能依赖于循环习语。

回答by JacquesB

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);

回答by RickL

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++)

回答by jmoz

In PHP I sometimes encounter code like:

在 PHP 中,我有时会遇到如下代码:

foreach ($array as $value) {
    $trimmed[] = trim($value);
}
return $trimmed;

Which idiomatically can be implemented with:

可以通过以下方式实现:

return array_map('trim', $array);