Thursday, September 24, 2009

Je vais m'amuser

If you're a native French speaker, you may be giggling to yourself at the title of this post. If, like me, you had two semesters of French in college, you may see nothing wrong — until your Parisian instructor starts giggling. “I will amuse myself.” The difference is idiom: in America, amusing oneself is harmlessly waiting for another; in France … well, it's still harmless, but that's not the point.

Programming idiomatically means understanding a language at a level deeper than its syntax. In Python, it means using list comprehensions rather than explicit iteration, or a generator rather than a physical list. In Java, it means using enum rather than constants. In any object-oriented language, it means “tell, don't ask.”

A hallmark of programmers who are new to a language is their lack of idiom. They write the new language as if it were the old, often bemoaning the loss of their familiar constructs (I'm no different: as I was learning Java I wished for C-style function pointers). There's an old saying that “You can write Fortran in any language.”

A danger of idioms is that they can turn into slang, used to add color rather than value, a way to signify membership in a clique. Duff's device is an extremely clever use of C's switch statement, and arguably a brilliant optimization (although I have trouble believing that the cycles saved from unrolling loops weren't immediately lost waiting for the IO register). But when used for copying memory, it loses all cleverness: replacing a CISC hardware instruction with discrete moves, or preventing a RISC compiler from applying its own optimizations.

More dangerous are the programmers who never spend enough time with a language to learn its idioms. They are the ones who code randomly: an ArrayList here, a LinkedList there, reading all rows from a database so that they can do a sum on the client. Exposure to different languages can be a great learning experience, as long as you learn enough to know when someone wants a little time alone.

3 comments:

dangph said...

> in France … well, it's still harmless, but that's not the point

That was very, uh, elliptical. I don't get it. So what does "je vais m'amuser" mean?

kdgregory said...

see: http://en.wikipedia.org/wiki/Onanism

... at least according to my Parisian professor

dangph said...

Thanks for the explanation. I don't agree with you though. I'm not a native speaker, but I've looked up a lot of dictionaries and done a lot of searching on th Web, and as far as I can tell, "s'amuser" doesn't mean what you say it means. It *can* have a sexual connotation, but that is a secondary meaning and one that would be context dependent. I would say that "Je vais m'amuser" would translate to "I'm going to have fun", plain and simple, and no one would giggle unless there were a sexual context.

Sorry for being so off-topic. Your point is a good one, and it still stands. For effective communication in programming languages (as in natural languages) it is important to get the idioms right.