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.
|