什么是编程习语? [英] What is a programming idiom?

查看:102
本文介绍了什么是编程习语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到编程习惯用法"一词泛滥,就好像人们普遍理解的那样.但是,在搜索结果和stackoverflow中,我看到了所有内容...

I see the phrase "programming idiom" thrown around as if it is commonly understood. Yet, in search results and stackoverflow I see everything...

从微型:

  • 增加变量
  • 代表无限循环
  • 交换变量值

到中等:

  • PIMPL
  • RAII
  • Format, comments, style...

到宏:

  • Programming paradigm or common library features as idiom
  • Process model as idiom
  • A collection of idioms equals a new paradigm

编程习惯用语"是否有一个通用定义?由于编程习惯用法"已在许多范围中使用:

Is there a single, common definition for "programming idiom"? Since "programming idiom" is used in many scopes:

  • 微观:语法上的细微差别或通用语法
  • 中等:常见的样式和样式
  • 宏:将范式编程为习惯用法

在任何这些范围中使用该短语是否有效?到目前为止,答案集中在句法成语上.其他人也有效吗?

Is it valid to use the phrase in any of these scopes? The answers so far focus on syntactic idioms. Are the others valid as well?

推荐答案

编程习惯用法是用特定语言编写任务的常用方法.例如,经常在C中这样写一个循环:

A programming idiom is the usual way to code a task in a specific language. For example a loop is often written like this in C:

for (i=0; i<10; i++)

PHP将了解类似的构造:

PHP will understand a similar construct:

for ($i = 1; $i <= 10; $i++)

但是在PHP中不建议在数组上循环.在这种情况下,您将使用:

But it is discouraged in PHP for looping over an array. In this case you would use:

foreach ($arr as $value)

在Ruby中,您将使用:

Whereas in Ruby, you would use:

(1..10).each

循环,或:

array.each

用这些语言编写循环有很多可能性.使用该习语可以使经验丰富的读者立即识别出该习语.然后,他们可以将时间花在更重要的问题上.

There are many many possibilities to write a loop in those languages. Using the idiom makes it immediately identifiable by experienced readers. They can then spend their time on more important problems.

这篇关于什么是编程习语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆