![]() |
|
Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: Pre vs Post Incrementing variablesby BrowserUk (Patriarch) |
on Sep 12, 2010 at 09:42 UTC ( [id://859826]=note: print w/replies, xml ) | Need Help?? |
Because long, long ago, in a land far away, the designer's of a language wrote in their specification:
(*) Who knew they had sentient software way back then :) Essentially, the designers of the C language traded source code ambiguity, programmer intuition, and program correctness, for performance. Instead of allowing the programmer to specify exactly the order in which sub-expressions would be executed--through the intuitive use of parentheses and precedence, rather than clumsy hack of creating unnecessary temporary variables--they opted to allow compiler writers to re-order those sub-expressions "for efficiency". Perhaps the most short-sighted and pervasive premature optimisation ever. In the days when cpu clock speeds were measured in low megahertz and single opcodes could take dozens of those clock cycles to perform; in a language that was explicitly designed to compiled directly to machine code; such optimisations could have a significant impact on program performance. So programmers would tolerate such inconveniences, for the gains that could be achieved. Now, it is arguable that the trade-off no longer makes sense: when you have processors with speeds measured in Gigahertz executing multiple instruction per clock cycle; in a language were even the simplest of sub-expressions takes dozens if not hundreds of op-codes. The scope for performance optimisation through the reordering of sub-expressions is essentially non-existent. But, for better or worse, Perl apes many of the rules laid down by the C language. In part I believe, because it made things easier for C programmers moving to Perl. So, we have the situation whereby the order of sub-expression evaluation is "unspecified", meaning that you cannot predict the result of using two or more side-effectful operations on a single variable within the same expression, on the basis of the language description alone. It also means that when ambiguities arise in the implementation, such as the one you describe, you cannot point a finger and called it a bug, because "the behaviour is unspecified". You'll simply be told: "Don't do that!". The fact that there is no logical reason, performance or otherwise, for having pre- and post-increment operate differently in this regard:
is simply dismissed as user error. "Because you shouldn't be doing it anyway!". One day, someone will see through these hoary ol' chestnuts of programming lore, and define a language that allows the programmer to write source code that specifies exactly what he wants to happen, and know it will happen. Without having to recourse to inventing unnameable intermediary variables for no good reason. Till then, you'll just have to get used to not using two or more side-effectful operators, on the same variable, within the same statement. {shrug} Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
In Section
Seekers of Perl Wisdom
|
|