What is a programming idiom?

后端 未结 10 2037
一向
一向 2020-12-04 06:42

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:

<
相关标签:
10条回答
  • 2020-12-04 07:26

    An "idiom" in (non-programming) language is a saying or expression which is unique to a particular language. Generally something which doesn't follow the "rules" of the langauge, and just exist because native speakers "just know" what it means. (for instance, in English we say "in line" but "out of line" -- that would be idiomatic)

    Moving this to the programming arena, we get things like:

     if(c=GetValue())
     {...}
    

    which actaually means:

     c = GetValue();
     if (c != 0)
     {....}
    

    which every C/C++ programmer understand, but would totally baffle someone coming from a different programming language.

    0 讨论(0)
  • 2020-12-04 07:28

    It comes from idiomatic the meaning of the word idiom in programming can be summed up as phrase that carries meaning and implications that is more than the sum of the words. In programming most code snippets are actually idiomatic. 'Pertaining or conforming to the natural mode of expression of a language'

    A Programming idiom can be considered descriptive of a class of solutions that is transferable to different cases. Consider while { ... } vs do {} while these are idiomatic, they contain the same words but the ordering carries an important distinction. The exact phrasing will differ by language, but the fundamental meaning and implications will differ; for example do {} while will always be executed once, no matter what language or statements are used to implement it. As an idiom it is transferable shape of an idea. It could be used in many circumstances, and expressed with different words (statements/commands) but the fundamental result will always be the same.

    0 讨论(0)
  • 2020-12-04 07:30

    An idiom is a 'pattern' that can be identified in several places.

    I wouldn't say it has anything to do with a particular programming language.

    Iterator foo;
    foo.reset();
    while (foo.next())
    {
        print(foo.value());
    }
    

    That is a snippet of what i would call the 'for each' idiom which is expressed slightly different in a number of languages.

    Another excellent example of an idiom is Socket. All platforms that claim to have sockets, all work in conceptually the same way, that is, they all have roughly the same interface.

    0 讨论(0)
  • 2020-12-04 07:34

    See http://en.wikipedia.org/wiki/Programming_idiom

    A programming idiom is a pattern, algorithm or way of structuring code. To talk about programming idioms is to talk about those patterns that recur frequently in code or to propose new ones.

    The benefits of being familiar with idioms, particularly the larger ones, is that when looking at code you can see several lines of code but because it is familiar as a particular idiom you can mentally represent and think about the code as that single idiom instead of having to necessarily read and comprehend each line individually.

    To say that code isn't idiomatic is to say that it doesn't structure itself in ways that allow human readers to think about the code effectively.

    0 讨论(0)
提交回复
热议问题