Tuesday, April 17, 2007

Make New Friends, But Keep The Old

I recently coded something very trivial in Perl. What surprised me was how long it took me.

It was a classic Perl situation; rows of text in one format that needed to turn into rows of text in another format. The classic Perl solution here is a foreach, a regex, and a sprintf() call. I've been coding Ruby so much that my first step in coding this was to try to build objects, and then an iterator.

Objects in Perl exist, in a sense, but they're nothing like Ruby objects. The brilliant book Higher-Order Perl actually shows you how to build an iterator in Perl, but that's for unusual circumstances. You don't write Perl that way. It's backwards; it disregards the relevant idioms and adds extra steps. It makes a molehill into a mountain. My solution was all wrong because I was trying to write Ruby in Perl.

It's kind of like the difference between being fluent in a foreign language and simply going native. On the one hand it's great that Ruby's style has become, to some extent, a matter of instinct for me. But I was surprised that it took me as long as it did to mentally switch gears.

When I first started doing Ruby, getting away from Java seemed almost heavenly to me. But now I almost want to code something in Java again to see how it feels different. When you learn new languages, you want to expand your skill set, not replace it.


  1. Giles,

    I think you've hit named the magic moment of competency. I remember I had been living in Holland about 4 months when I went about my errands.

    I bought groceries, went to the butcher, had lunch, went to class, took a nap, and then was ordering a beer when it hit me.

    I've lived all day without thinking about translation. It just was pure expression of thought.

    What you describe in your Ruby / Perl shifting is the moment where expression becomes possible -- versus a series of fast, hard, and frequent slams of head against wall.

  2. I just ran in to this same thing a couple days ago, only almost even worse. Converting an old Java servlet into some Ruby code, just looking at the old Java code gave me a headache.

    It made me want to drop Ruby for a while and go back to Java, just so I don't lose all the skillset I built up before I came here. On the other hand, it also made me realize again the difference between a language that fits me and twisting myself to fit into a language.

    In all the time I wrote Java, it never felt like something natural to me... it was always thinking a concept, and translating to Java... whereas, with Ruby, I can usually almost directly map the concept into code that I am completely comfortable with.

  3. @steven - I guess you're right. usually when I switch languages I mainly just think about syntax. this time I had to think differently about the nature of the solution. (of course on the other hand, with Java and Python you kind of have to think differently.)

    @techno - there's a good book on Java called "Thinking In Java" but it doesn't really work, at least as far as I can tell, because I've had that too, where thinking in Ruby is something you can do pretty naturally, while thinking in Java never really seemed to happen for me. the exception would be when looking at everything in terms of "how can I build an object somewhere else for this code to call?" but other than that thinking in Java is kind of weird.

  4. I think that, after having learned Lisp and Smalltalk, my thoughts will change again. I'm pretty sure that the reason it's easier for us to think in Ruby than in Java is that we tend to think in higher levels of abstraction than are available in Java... although it is a "high level" language, it's not near as high level as regular human thought.

    Ruby/Smalltalk are definitely much closer, and Lisp forces you to think of abstractions in other terms than what you are used to... you abstract at a different angle, almost, than you would normally do. I've read tons and tons of people who say that once they start programming Lisp, they forever after have problems returning to other languages because they are abstracting along different lines than what you are capable of in other languages.


Note: Only a member of this blog may post a comment.