Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

language agnostic - What is a programming idiom?

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

From micro:

  • Incrementing a variable
  • Representing an infinite loop
  • Swapping variable values

To medium:

To macro:

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

  • Micro: syntactic nuance or common syntax
  • Medium: common style and patterns
  • Macro: programming paradigms as idiom

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 will understand a similar construct:

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

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

foreach ($arr as $value)

Whereas in Ruby, you would use:

(1..10).each

for the loop, or:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...