Secret Santa

  • Posted by Mike Naberezny in PHP

    I’ve been following Ruby on Rails for a short time and even started playing with Ruby a bit myself. So far, I think Ruby is neat but I find some of the syntax arcane or esoteric compared to Python. PHP’s popularity speaks for itself and I think it is the most prevalent web scripting language for good reason. Although, I have to say that it really surprises me that Python has never really captured that close-second spotlight that Ruby now enjoys thanks to Rails. Maybe projects like Django will change that.

    Most of the Rails soapboxing claims that Ruby is more productive because the language allows you to write code they describe with adjectives like “terse” and “beautiful”. They show slides of “code beauty” and more often than not, they specifically target the PHP language as the opposite of this, almost always without comparisons.

    Today, I ran across this post. I couldn’t help but think to myself, “That’s sort of nice looking but wow, for such a little thing, how much overkill is that?” Here’s the same thing in PHP, minus the database fetch (add it in two lines).

    $from = $to = array('Paul', 'Matthew', 'Daniel', 'Andi', 'Chuck');
    for (;;) {
        foreach ($from as $i => $f)
            if ($to[$i] == $f) continue 2;
    print_r(array_combine($from, $to));

    That’s less than half the code. I think this is a fair comparison because it directly compares PHP to Ruby, without any “frameworks” in between. Which one do you think is extremely simple?

    I think that Rails is a phenomenal tool, but it’s tooling — not a language. I am sure that great tools will come for PHP as well, and that PHP itself will always be a language that is both highly productive and high performance. Do you Yahoo!?

    Update: I wrote this post back in 2005. Not long after writing it, I decided to give Ruby a more serious look and attended the first Silicon Valley Ruby Conference. In the years since, I’ve done a great deal of work with both Ruby and Rails, and my feelings on this have changed quite a bit. That experience, and the urging of some PHP friends, led me to coauthor a book called Rails for PHP Developers.


  • comment by metapundit 16 Dec 05

    FWIW I’m a php coder so I understand the php example better than the ruby version. Still, looking at the two samples I can’t help but feel the ruby one is a little more expressive and a little less hacky. This could be partially fixed by replacing “for(;;)” by “while(true)” in the php version but still the “continue 2″ and break calls always seem only a step or two above gotos to me.

    Partly aesthetics I guess, which makes it a bit subjective. I’ve not done anything in ruby, but I do envy the more expressive nature both ruby and python code seem to have over php… Oh yeah and I’m interested in turbo gears over django as far as the web frameworks for python goes…

  • comment by lord sarthin 16 Dec 05

    i’m cheering for django as well. Its a great package, cool community and they’re constantly improving it. Plus it powers some fantastic sites.

  • comment by ulysses 16 Dec 05

    to = [“Paul”, “Matthew”, …]
    from = to.dup
    from.sort_by { rand }
    from.each_with_index do |i, index|
    puts “#{i} => #{to[index]}”

    That’s 75% of the code… and it’s almost as ugly as the php version.

  • comment by Mike 16 Dec 05

    It might be 75% of the code, but it doesn’t solve the problem at all. You can’t give a gift to yourself. The equivalent PHP code for your program is below.

    $from = $to = array(‘Paul’, ‘Matthew’, ‘Daniel’, ‘Andi’, ‘Chuck’);
    print_r(array_combine($from, $to));

  • comment by ulysses 17 Dec 05

    Ah, quite right. Let me try this at 10AM instead of 2 AM:

    to = %w(Paul Matthew Daniel)
    loop do
    from = to.sort_by { rand }
    pairs =
    next if pairs.any? { |t, f| t == f }
    pp pairs

    That’s pretty much line for line the same. But I should really *comment* instead of just writing lame code.

    “I think this is a fair comparison because it directly compares PHP to Ruby, without any “frameworks” or other middleware in between.”

    As we see above, you can get the same amount of code in Ruby. So your comparison is shot. I could go on to say “The comparison is really between doing a quick hack of something that is ugly but functional, and doing a cleaner version that has some possiblity for maintainence.” But I won’t.

  • comment by Mike 17 Dec 05

    That’s a nice example. I think that is a much better, apples-to-apples comparison. I could send the bloated PHP 5 version to match the Ruby version from earlier if you’d think that would be more manageable than eight simple lines of code, although that’s probably not necessary. This is a much more appropriate amount of code for doing such an everyday silly task, and our two examples show a real comparison nicely. For this example, it looks about even.

    It is refreshing to finally see a direct comparison of some PHP and Ruby code, and more of this is sorely needed in place of all this hype comparing only a language (PHP) to an entire framework (Rails). More of this is needed in the future.

    I would perhaps be more interested in seeing examples comparing Ruby to Python as well; I only chose PHP as that seems to be the favorite target of your camp. That’s understandable, it is the elephant.

    Thanks for your comments.

  • comment by Scott 17 Dec 05

    These comparisons are fruitless. It’s not about doing it in x% of the code. For that matter:

    x = %w(Paul Matthew Daniel).sort_by { rand }
    pp = x.dup).unshift(z.pop))

    Whoopdy doo.

  • comment by Thomas David Baker 24 Feb 06

    Isn’t it?

    What about …

    It’s an interesting argument.

Post a comment

Thanks for commenting. Please remember to be nice to others and keep your comment relevant to the discussion. I reserve the right to remove comments that don't meet these simple guidelines.