PHP vs Ruby/Python Syntax

There are a plethora of articles out there comparing the popular scripting languages of the day. Some of them compare performance, others available frameworks, and even others report to determine what language is going to make you happiest or more productive. I write this article to compare a single point that I read (and sometimes hear) a lot: the idea that Ruby and Python are more "beautiful", "expressive", "readable", or "fun for the programmer" than PHP. Admittedly, my background is mostly in C/C++ and PHP, but I have dabbled in several other languages, and researched even more. I'm not a fan-boy of any particular language or technology. There are plenty of things that piss me off about PHP. But the "elegance" issue is not really tied to any particular language, more to families of languages with differing syntax. We'll call them "curly-brace" (or C-style) and "non-curly-brace" languages, but the distinction goes far beyond braces.

“The first problem I see is the confusion between 'looks nice' or 'fewer characters' and programming efficiency and true readability.”

The first problem I see is the confusion between "looks nice" or "fewer characters" and programming efficiency and true readability. For some reason Ruby/Python users think that the fact that they don't have so many braces or that it "looks more like English" makes their language more attractive or "readable". I think that idea is load of crap.

We don't program in English, and for good reason. The grammar is far to complex - too complex to parse, too ambiguous, too many ways to say the same thing. Programming languages ought to indicate logic structure as much as possible. Anyone who has tried knows the complexity in extracting the logical structure from a linear language, such as English.

When it comes to code, I want the intention of the author to be as clear and explicit as possible. I have no interest in saving myself typing a few delimiters in the name of "efficiency" or removing some braces or semi-colons in the name of "beauty". People have this idea that if you can just type what you're thinking (in English, presumably) and it works, that makes for a better language. I personally think it is a building block for crap code.

“We don't program in English, and for good reason. The grammar is far to complex - too complex to parse, too ambiguous, too many ways to say the same thing.”

You'll see these examples out there where someone writes a block of code in Ruby, then the same thing in PHP. In Ruby, they have two lines - one of which has four methods strung together. In the other, they have four lines of PHP that does the same thing. (I'm not comparing built-in functions here, that's a different discussion.) Then they say, "Look at how much better life is in Ruby! What took you four lines I can do in two!" They think that lines of code are some measure of efficiency or something. The truth is, in these examples, the logic in each step is usually the same. They just crammed it onto one line.

We used to have a developer who would write lines of jQuery with a million methods chained together, and it would do something like run a whole slideshow, practically. And it was like deciphering ancient hieroglyphics to understand.

I don't think it serves anybody to cram four (or even more) operations on a single line. Sure, when you thought of it, it was one coherent idea. But when someone else (or even yourself) comes back to read that code later, it gets a little confusing when you're doing four different things with one instruction. The human brain works best through algorithms in a succinct step-by-step format, where each step is a simple progression of where you were before. In truly readable code, you want short, understandable lines that do one thing (much as in truly readable code, you want short methods that do one thing).

“The human brain works best through algorithms in a succinct step-by-step format, where each step is a simple progression of where you were before. In truly readable code, you want short, understandable lines that do one thing.”

This pattern is seen over and over. "Oh, well I just throw an 'if x' at the end of my statement, and I did what you did in three lines in one! It's so much more 'expressive'!" But you have to read the whole damn line to realize that statement might not ever get executed. "Why would you need those pesky parenthesis? Just throw in a space and a couple commas and you've got a method call!" Except that those parenthesis serve the reader to show that it IS a method call. "But I can assign values to multiple variable on a single line!" What makes you think that a good idea?

The philosophies of the people who are active in the non-curly-brace language communities are all-too-often in search of the wrong goals. Everyone can make up their own mind about their preferences, but I happen to like my parenthesis and brackets; and I find I can write beautiful, expressive, readable code "in spite" of them....

Grid Image