什么是惯用代码?

发布于 2024-07-04 08:16:30 字数 68 浏览 7 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彼岸花ソ最美的依靠 2024-07-11 08:16:30

惯用意味着遵循语言约定。 您希望找到完成任务的最简单、最常见的方法,而不是从不同的语言移植您的知识。

使用带附加循环的非惯用Python:

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

使用列表理解的惯用Python:

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

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] 
灼疼热情 2024-07-11 08:16:30

一些示例:

资源管理,非惯用:

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

惯用:

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

迭代,非惯用:

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

也非惯用:

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

惯用:

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

过滤,非惯用:

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

惯用语:

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

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);
独行侠 2024-07-11 08:16:30

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

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.

萌梦深 2024-07-11 08:16:30

实际上,这意味着以一致的方式编写代码,即所有在您的代码库上工作的开发人员在编写类似的代码结构时都应遵循相同的约定。

因此,惯用方式是与其他代码的风格相匹配的方式,非惯用方式意味着您正在编写某种函数,但以不同的方式。

例如,如果要循环一定数量的项目,则可以用多种方式编写循环:

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

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

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

等等

最重要的是一致地使用所选样式。 这样人们就会对如何使用它变得非常熟悉和自信,当你发现一个看起来不同的用法时,它可能是引入错误的迹象,也许是一个错误,例如

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

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++)
自由范儿 2024-07-11 08:16:30

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

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

可以用以下方式实现:

return array_map('trim', $array);

In PHP I sometimes encounter code like:

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

Which idiomatically can be implemented with:

return array_map('trim', $array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文