![]() |
Quote:
|
Quote:
Let me repeat that. I don't see any loops. I am always cognizant of what's "really happening" under the hood. I know that in executing the program there's a lot happening down under. Symbols are being looked up in dictionaries. Memory is being allocated to hold values. Method dispatch tables are being consulted to locate the correct code to execute next. And yes, there are some loops down there, too. But I don't need to look at any of that to understand what the program does. The code I look at and have to understand doesn't have any loops in it. ax^3 + bx^2 + cx^1 + dx^0 takes 4 exponentiations, 4 multiplications, and 3 additions. (((ax + b)x + c)x + d) takes 3 multiplications and 3 additions. But we're drifting away from the point I wanted to make. The PHP version could easily be rewritten to evaluate the polynomial using multiply-add. Any competent programmer would do that as a matter of course. Let's consider it done and move on. My main point was this: if you're comparing two languages by looking at sample code to perform a given task, write each sample the way a reasonably competent user of that language would have written it. It's pointless to write the program in yet a third language (C in this case) and then translate that clumsily into the languages being compared. That doesn't give you a useful comparison. Besides the obvious perils of non-idiomatic translation, it rules out beforehand any features of the target languages that aren't present in the source language. In this case, Ruby has a slew of operators that operate on entire sequences (of which arrays are only one flavor) as fundamental objects. While you can loop across the elements of a sequence, it is much more common in Ruby to operate on entire sequences with a single operation. To someone not used to this, operations like inject, collect, reverse, sort, detect, any?, all?, and grep may seem exotic, but they're the bread and butter of Ruby programming. A Ruby programmer would no sooner replace any of these with an explicit loop than would a C programmer replace for(...) with the equivalent if() and goto statements. You could do it, but why? It takes more code to write, and it's harder to read, as well as being more error-prone. If you try to explain that "for" is easier to understand than "if/goto", and they reply "But 'for' just uses 'if/goto' under the hood, so they're still there," you know they're just not getting it. This fancy-schmancy "for" construct isn't just some exotic hard-to-understand thing you're doing to show off. It really does make the program easier to read and easier to get right. All it takes is the initial effort to learn what "for" does. Likewise when you explain that we have all these operators that work on entire sequences so you don't have to write out the explicit loops, and they say "But 'inject', 'collect', 'grep' et al. really do still run loops underneath," you know they're still not getting it. These aren't fancy-schmancy constructs that get thrown in just to show off. They really do make the program easier to read and easier to get right. All it takes is the initial effort to acquaint yourself with them. I feel that I'm being sucked into acting like a Ruby fanboy here. I do kinda like some features of Ruby, but it's not my favorite language. (Trying to debug Rails code cured me of that.) I just thought this particular example was maligning it unfairly. |
who's maligning anything? everybody chill a bit. all code gets executed in machine language - everything higher level is just a metaphor. what metaphor you use depends on what you know, what you're trying to do, and how your brain works. beyond that... whatever.
|
| All times are GMT -5. The time now is 05:34 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.