I've been thinking about this concept for a long time. Especially after reading some of Larry Wall's writings on Perl and natural languages.

I've been trying to advocate the idea of various programming language idioms being useful in what they convey to the maintenance programmer about intent. Like natural language idioms, programming idioms say more than the sum of their syntax.

I think if we began to consciously explore the use of the different idioms, we could more accurately understand the intent of the original programmer. One other important point is that like natural language idioms, programming idioms are cultural. If you are new to the culture, the idioms are strange and require effort to understand. Once you are comfortable with them, more information is conveyed than appears in just the syntax.

For example, I like to use the argument of conditional logic in Perl to try and illustrate these principles:

# form 1 if(condition) { action; } # form 2 action if condition; # form 3 condition and action;

(Update: thanks Zaxo for correcting my and/or confusion.)>/p>

All of these structures cause the computer to execute very similar code. (Let's ignore the actual opcodes and return values for the moment. Let's also ignore the other similar constructs for now.) I maintain that these three different forms place different levels of importance on the action and condition. This is in spite of the fact that condition is always executed first and if it is true the action is executed.

To me, the first form says that the condition and action are both very important to the function of this code. The second form is kind of a guard case. The condition enables the action, but it is less important. The third form seems to place more emphasis on the condition.

It seems to me that most of the code I have read uses these idioms in the way I've described. I don't know if this is written down somewhere and I've just missed it, if it's an unconscious meme that many Perl programmers apply, or maybe I'm just dense and this is obvious to everyone else.

It seems that some level of textual analysis could discover useful idioms of this type.

G. Wade

In reply to Textual Analysis and Idiomatic Perl by gwadej
in thread Textual Analysis and Perl by cyocum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.