Is Ruby very syntax rich? Do you like it? I don’t.

By Angsuman Chakraborty, Gaea News Network
Tuesday, August 19, 2008

I tried delving into Ruby couple of times in the past. Every time I had this weird feeling of what am I doing here when I am already very well conversant with a simpler language - Java, which gives me everything I need. Ruby just felt very syntax rich, may ways and interesting tricks to do stuff and so on.While these may be appealing to a script-kiddie, nice syntactical sugars do not help much in the long run or help you create robust code (unless you love flying whales and a service which fails every other day).

I am not afraid of developing CRUD interfaces nor do I find it time consuming. What value can I get from RoR? Every time I delve into Ruby & RoR, I come back feeling unsatisfied. What am I missing here?

I have been playing with Java since ‘96 and whenever I get a chance to code, it is a pleasure. I just coded a simple publish-subscribe broker over XMPP (Jabber wire protocol) using an object database (db4o). Believe it or not it took just 84 lines of code with comments to create a pubsub broker with data persistence. I went on to add live debugging facility, more functionality and reliable messaging. With all these extra features the code still stands below 130 lines but more importantly it performs like a champ.

What more can you ask from a programming language? It is rich in functionality, concise and clear. You can give the code to any newbie and he can understand it. Compare that with small but horribly complicate code in many languages and you will realize what I mean. In Cedric’s challenge (linked above) check out the performance of Ruby (Hint: More than 100 times slower).

BTW: I am already using this publish-subscribe messaging system to communicate Subversion updates. We will soon use this infrastucture to deliver server status and more.

Discussion

Caligula
September 20, 2008: 12:58 pm

Not sure why you didn’t respond to my first reply.

>> Hey, some people really like to type and don’t mind that their application space is surrounded by syntax–nothing wrong with that. It’s just not *my* preferred paradigm. You like it, that’s duckie.

It’s not the question of typing few extra braces, it is the readability that matters for long term maintainability and re-usability. A cryptic Python or Ruby or Perl code, despite its perceived conciseness doesn’t help in that matter.
If few extra braces is all that is bothering you, then in all likely hood you haven’t developed many enterprise products.

I haven’t made assumptions about your development background (except to claim that you didn’t really program in Lisp if you think Java can represent a problem domain as well as Lisp can) so let’s hold off on making any about mine. I’ve been an J(2)EE developer for almost a decade and have been programming in Java since JDK 1.0. I’m a member of the Struts PMC. Java is, unfortunately, where I spend a lot of my time. Coming from a Lisp and Smalltalk background I can assure you it was a pretty traumatic experience back in the mid-90’s–we laughed a lot at Java, even as we watched money being thrown at it left and right.

I am at a loss to explain why you continue to equate “cryptic” with “concise” or “expressive”. Stop talking to me like you’re talking to somebody that is excited when I can write my code with one less character than the next guy and we’ll get along much better.

quality programming imho is determined by simplicity, readability and consequently long term maintainability & re-usability.

And part of readability is a reduction in the code’s cognitive noise. To paraphrase: Java is big on ceremony, which masks essence. Java makes it unnecessarily difficult to focus on essence. Java makes me repeat myself in many different ways. I don’t like repeating myself, and I shouldn’t *have* to.

>> There is no domain in which Java will be more concise or a better reflection of the problem space than something like Smalltalk, Lisp, Ruby, Python, Groovy, Scala, etc. That just isn’t what Java is good at.

I think you are mistaken. Enterprise software is very well served by Java than some of the esoteric languages you mention simply because of free enterprise class libraries and frameworks that are available.

And I *know* you’re mistaken: you’ve confused “Java” with “Java ecosystem”, and seemingly completely ignored that all those same class libraries and frameworks can be used by *any* JVM language–if you’d want to. Naïve re-use of Java libraries can make even an elegant language look ridiculous, but as a stop-gap measure, it works fine.

Now try addressing *Java* as a *language*.

are you suggesting that Ruby is destined the fate of Lisp, Smalltalk or Scala which almost nobody uses these days?

It’s curious you left out Python and Groovy, particularly now that Sun also has some Jython development in-house and Groovy is making use of the work done in JRuby (although Groovy is a bit of a mess as a language). It’s obvious that alternative JVM languages are on the rise–specifically because Java is an irritating language to work with. As it is now Java is an evolutionary dead-end, and even the most conservative attempts to move it forward are met with resistance on several fronts.

By the time it gets anywhere it won’t matter any more and people developing for the JVM will be using something else. Even C# has already adopted many useful patterns and language constructs necessary to write better, clearer, more concise code–while Java remains mired in the past.

>> I run in to Java’s limitations every single day, and it’s irritating. It makes it very difficult to produce well-abstracted frameworks.

Tell me more. I would very much love to hear about some real-life problems you are facing with Java. I have spent several years in Java and never felt stumped by its limitations. In fact I find it very enabling even to this day.

“Stumped”? I’ve never been “stumped” by its limitations either–anything can be worked around with sufficient effort. But I don’t *want* to work around my development language: I want my language to *help* me express myself.

I don’t know if you’re being intellectually dishonest or just don’t know any better: reasonable, non-fanboy discussions about the relative merits of Java have existed since it first came out and it doesn’t take any particular effort to find better summaries on the web than I can provide here.

Java had specific design goals–clarity and concision weren’t among them. Your example of writing a pub/sub broker in < 130 lines doesn’t have anything to do with Java; it has to do with the libraries you used. It’s not a useful example of how Java is concise or clear or encapsulates abstractions–of course it will seem just fine. Good grief. Even saying that I’d bet that it would be cleaner and easier to understand written in Jython or JRuby.

Like I said–you like Java, that’s awesome. More power to you. But don’t deceive yourself into believing that it’s a particularly capable language just because it has a bunch of libraries. Take the time to actually learn Lisp. Or Smalltalk. Or Jython. Or JRuby. Use what those languages give you–*then* get back to me. Heck, you can even program Perl like it’s a real language (read _Higher Order Perl_).

You’ll have a tough time convincing developers with a solid foundation in any of the “fanboy” languages that also know Java that Java-the-language comes close to measuring up.

August 26, 2008: 4:44 am

> Hey, some people really like to type and don’t mind that their application space is surrounded by syntax–nothing wrong with that. It’s just not *my* preferred paradigm. You like it, that’s duckie.

It’s not the question of typing few extra braces, it is the readability that matters for long term maintainability and re-usability. A cryptic Python or Ruby or Perl code, despite its perceived conciseness doesn’t help in that matter. If few extra braces is all that is bothering you, then in all likely hood you haven’t developed many enterprise products.

Programming is not “intellectual masturbation” as my friend would like to say, it is not the question of who writes the smallest lines of code (sounds very juvenile to me like who has the smallest d*), quality programming imho is determined by simplicity, readability and consequently long term maintainability & re-usability.

> There is no domain in which Java will be more concise or a better reflection of the problem space than something like Smalltalk, Lisp, Ruby, Python, Groovy, Scala, etc. That just isn’t what Java is good at.

I think you are mistaken. Enterprise software is very well served by Java than some of the esoteric languages you mention simply because of free enterprise class libraries and frameworks that are available. However your categorization may have hidden meaning, are you suggesting that Ruby is destined the fate of Lisp, Smalltalk or Scala which almost nobody uses these days?

> I run in to Java’s limitations every single day, and it’s irritating. It makes it very difficult to produce well-abstracted frameworks.

Tell me more. I would very much love to hear about some real-life problems you are facing with Java. I have spent several years in Java and never felt stumped by its limitations. In fact I find it very enabling even to this day.


Caligula
August 25, 2008: 4:33 am

>> Some languages make it significantly easier to create abstractions; Java is not one of those languages.

> Can you give some concrete examples?

Really? then you didn’t do very *much* in Lisp. Even Ruby is better at expressing a domain, although it’s far uglier than Smalltalk.

Hey, some people really like to type and don’t mind that their application space is surrounded by syntax–nothing wrong with that. It’s just not *my* preferred paradigm. You like it, that’s duckie.

>> The same application in about 1/2-3/4 the code, and the code reflects domain much more accurately.

> It really depends on your domain. Unless you are using it for simple CRUD applications, you may not derive much benefits from the framework imho.

Who’s talking about framework? I’m talking about language. There is no domain in which Java will be more concise or a better reflection of the problem space than something like Smalltalk, Lisp, Ruby, Python, Groovy, Scala, etc. That just isn’t what Java is good at.

I run in to Java’s limitations every single day, and it’s irritating. It makes it very difficult to produce well-abstracted frameworks.

August 24, 2008: 12:22 am

@Foe

> Because it’s entirely possible to create Java programs that are sprawling messes of mind-boggling garbage, and it’s entirely possible to create Ruby programs that are models of clarity and concision.

Yes and also vice versa. You can create messy programs in Ruby and for that matter in any language. So doesn’t that actually disprove your assertion? Conciseness should be more dependent on the programmer / developer than on the language going by your statement alone. Am I missing something here?

> That is a precisely scoped problem domain/design pattern - exactly the sort of thing Java does very well.

Why is that?

> I’ve finished mine in RoR, so let me know when you’re ready.

I wouldn’t exactly go into photo sharing because I think it is a crowded and somewhat saturated space (and I like Flickr). However I too am building a Web 2.0 application. Let’s compare notes after I am done :)

> But they’re are those of us who manage to find a place for both while avoiding the title of “script-kiddie”.

You are the second one making the mistake. I am not saying Ruby developers are script-kiddies. The cartoon says it and I haven’t drawn the cartoon. It s from StuffThatHappens. I thought people will notice the small attribution at the bottom. I do however think that solving problems in one-line to prove superiority of your language (in whatever language that may be) is script-kiddie mentality.

In the end what matter is ease of development, extendability and maintainability. Let’s see how Ruby scores on all three with emphasis on extendability and maintainability few years down the line. It is too early to pass a verdict either way.

What do you think?

BTW: Why are you my foe? I know I am not yours :)


Foe
August 22, 2008: 1:07 pm

There’s nothing really being argued here. Your claim for Java’s “conciseness” is ridiculous. Concision in memory managed languages is more a function of design than implementation (but design has a lot to do with it in languages like C and C++, too). About the only thing I agree with is your assertion that the programmer matters more than the language. This is true.

Because it’s entirely possible to create Java programs that are sprawling messes of mind-boggling garbage, and it’s entirely possible to create Ruby programs that are models of clarity and concision. All you’re really saying here is, “Boy, I like Java!”

Your example of a pub-sub broker is not relevant, either. That is a precisely scoped problem domain/design pattern - exactly the sort of thing Java does very well. That’s really great about your line counts, but please, build, say, a fully featured photo-sharing application with paid subscriptions, search and tagging, image hosting, sharing, resizing and cropping, along with social sharing services, and then let’s compare, by all means, line counts. Now THAT would be a comparison of CRUD applications. I’ve finished mine in RoR, so let me know when you’re ready.

Finally, you say: “I don’t think Ruby is fundamentally a better paradigm.”

Okay, well, yeah. It’s not a fundamentally better paradigm because it’s not, from Java, a fundamentally DIFFERENT paradigm. It’s just a different implementation of a very similar paradigm. They’re both object-oriented, class-based langauges, with different purposes. Comparing them is little like comparing tomatoes with strawberries. They’re both berries, but they’re suitable for different courses of the meal.

So, for your tomato/strawberry comparison, I say, “kudos!” Clearly, you’re a tomato-man. But they’re are those of us who manage to find a place for both while avoiding the title of “script-kiddie”.

August 22, 2008: 11:24 am

> 50% of the so called Ruby programmers are Web developers. They only know 2 languages, Ruby and maybe Visual Basic. They like to build small Web applications fast and design nice user interfaces with Photoshop.
> 20% are ex-PHP developers.
> 20% are ex-Java developers.
> The other 10%, are Python haters.

That may explain why they are so fanatic about the language :)


test
August 22, 2008: 8:13 am

Ruby has gotten lots of kids hooked via Rails, a web development framework.

50% of the so called Ruby programmers are Web developers. They only know 2 languages, Ruby and maybe Visual Basic. They like to build small Web applications fast and design nice user interfaces with Photoshop.

20% are ex-PHP developers.

20% are ex-Java developers.

The other 10%, are Python haters.


Matt
August 22, 2008: 8:12 am

Ruby has gotten lots of kids hooked via Rails, a web development framework.

50% of the so called Ruby programmers are Web developers. They only know 2 languages, Ruby and maybe Visual Basic. They like to build small Web applications fast and design nice user interfaces with Photoshop.

20% are ex-PHP developers.

20% are ex-Java developers.

The other 10%, are Python haters.


Sidewinder128
August 22, 2008: 7:14 am

I don’t like Ruby, It seems as a bad version of smalltalk.

It is slow as hell even with Yarv.

I prefer much Python.

August 22, 2008: 6:08 am

> Some languages make it significantly easier to create abstractions; Java is not one of those languages.

Can you give some concrete examples?

> The same application in about 1/2-3/4 the code, and the code reflects domain much more accurately.

It really depends on your domain. Unless you are using it for simple CRUD applications, you may not derive much benefits from the framework imho.


Caligula
August 22, 2008: 5:43 am

It’s the programmer that matters and not language.

That is true only to a certain extent. Some languages make it significantly easier to create abstractions; Java is not one of those languages.

What value can I get from RoR?

The same application in about 1/2-3/4 the code, and the code reflects domain much more accurately.

(Comment form does not look/work well in FF3.)

August 22, 2008: 4:07 am

@James
I agree with your analysis. Few syntactical sugars doesn’t make a mature language as Rubyers seem to forget these days. Java is backed by years of programming and massive community support.

August 22, 2008: 4:03 am

@rgc
> Out of curiosity, is Java your only language?

Good question. These days I regularly program in Java, PHP & Javascript. Previously I used to do hardcore C programming, before that I dabbled in 80×86 Assembly language (masm compiler in case you are interested). I did some work in ASP, J#. I even dabbled in Ruby much before it became popular, and was just out of Japan.
I programmed in Pascal, in fact loved the language very much (and damn the compiler fitted in a single floppy!). I also programmed in Fortran 77, DBase 4, Basic and even Visual Basic, Lisp, Prolog, Perl and few others you probably haven’t heard about.

Hope that answers your question.


rgc
August 21, 2008: 10:11 pm

I have noticed that the majority of people who don’t want to learn Ruby only know one language.

And that most rubyists actually know and use multiple languages.

Out of curiosity, is Java your only language?


James Hofmann
August 21, 2008: 9:57 pm

I don’t use Ruby or Java, particularly, though I have done a test or two in them.. I’ve mostly used Python.

But one thing that hasn’t come up in this discussion is that it’s not necessarily about syntax - syntax is a non-absolute and the optimal syntax for one situation is never the optimal syntax for every situation. (see regex’s, XML, etc.) Type systems, reflectivity, and programming styles are more interesting and conceptually deeper points.

Java’s disadvantages, in that kind of discussion, are that it was designed along the same lines as C/C++ — with a generally low level of dynamism. It has required-annotation, link-safe-only static typing. That kind of system is downright primitive compared to implementations like that of the ML family, or, for a more recent and familiar-syntax example, haXe. Java doesn’t do metaprogramming, and while it can support functional programming, it’s not a built-in. So it stays very centered in object-orientation — and Java is not prototype-based OO, but class-based OO.

Ruby(or Python for that matter) isn’t perfect on all those points, but the additional concepts are much better supported. So in those situations where the extra stuff becomes a good solution, they become the better languages by dint of having a bigger toolbox.

Java’s strength now comes from its maturity. It’s long since rid itself of low performance as an argument for anything but a bleeding-edge game, an embedded system, or a pure number-crunch application. It has enormous libraries and mountains of frameworks for all the typical industry needs. Documentation abounds, multiple heavyweight IDEs are available, and someone has probably already seen any given problem you run into. This confers a lot of help to anyone who wants to “get stuff done” and doesn’t need to create new algorithms or architect an exotic system.

August 21, 2008: 9:01 pm

> The second trick is that Ruby makes it blindingly easy to call C programs

Java too has Runtime.getRuntime().exec(…) calls you know which aren’t very complicated last I checked.

> And even then, there are different ways to measure best. Is it the most readable code, or most quickly written. Or the quickest to execute? Or that which uses the least power to execute on a portable device?

Java would probably be better again in terms of readability as far as my experience with reading Ruby code goes, but I am not an expert in Ruby. Java executes pretty well in portable devices and is present on billions of mobiles so far, so no dice there too. I agree with your premise that best can be defined for any number of criteria. However one-liners shouldn’t be the criteria for defining best by any sane standards whereas readability or maintainability or speed can very well be, don’t you agree?

> I think this article would be best summarized as, “There is a huge premium to what you already know.”

In part, yes. Knowledge gives you increased power. However a fundamentally different paradigm can increase the power many times over after the obligatory learning curve. So in the long run a better paradigm normally prevails. I don’t think Ruby is fundamentally a better paradigm. However JRuby is actually a more interesting proposition to me. It offers the power and niceties of both the worlds.

August 21, 2008: 8:50 pm

> I believe those names and emails are meant to be replaced when you post….
Yes, they should. Exactly what happened? Can you please provide some details?

August 21, 2008: 8:49 pm

> You might be the first person to call Java code “concise”. Sorry, I disagree.

Me too. Any good programmer can write concise code in Java as well as in most other languages. It’s the programmer that matters and not language.

August 21, 2008: 8:47 pm

> If the challenge were, instead, to involve string manipulating using regular expressions, Java and C would fall flat on their faces; Ruby, Perl, and several other languages would spring into the lead.

I would respectfully disagree. Java supports the regular expressions supported by Perl, PHP etc. Java has added regular expression methods in String class as well as an entire package ( java.util.regex ) for regular expressions. None of the modern languages has any lead over string manipulation imho.

August 21, 2008: 8:44 pm

> “Umm this is my first visit to your blog, yet it seems to have identified me as Jeremy Weiskotten. I am not he.”

I will look into it. Are you saying that it didn’t give you an option to provide your name, email, blog url etc.?
BTW: Who are you?

August 21, 2008: 8:39 pm

@comctrl6
Your Java code doesn’t work. Are you looking for something like this?
for(String prop : (new TreeSet(System.getProperties().stringPropertyNames()))) System.out.println(prop + ” : ” + System.getProperty(prop));

That’s what you want in a single line. Including the fixtures isn’t an issue because that overhead is negligible to none in real programming projects. However if you insist here it goes (hoping WordPress maintains the sanity of the code):
import java.util.*;
public class SortedProps {
public static void main(String ignore[]) {
for(String prop : (new TreeSet(System.getProperties().stringPropertyNames()))) System.out.println(prop + ” : ” + System.getProperty(prop));
}
}

August 21, 2008: 8:13 pm

I don’t think anyone who does Ruby is a script-kiddie. My concern is about the programmer’s obsession with fewer lines of code. I like my code to be concise, very concise. However I would not sacrifice legibility for making it more concise.

I recently wrote a publish-subscribe broker (with subscription & message persistence) over xmpp protocol in 84 lines of code, fully commented and very legible. I would say the code is very concise by any standards, wouldn’t you agree?
I then added reliable messaging to it, on-line debugging facilities and lot more commenting and it all came to 130 lines in total, still concise considering the task it accomplishes. My point is that we should strive for conciseness but not at the cost of readability and maintainability.


Ryan
August 21, 2008: 4:39 pm

I’ll take a shot at this flame-bait, ignoring the ridiculous implication that everybody who likes Ruby is a script-kiddie.

Doing more with less code is not a bad thing. Some of the “syntactical sugar” of Ruby let’s me cut through a lot of BS and get something done faster than I could in most other languages, and I appreciate it every day. Personally, I’m a lot less concerned with newbies being able to read my code (as opposed to smart people who know what they’re doing) than I am with being able to get stuff done in the simplest way possible.


comctrl6
August 21, 2008: 3:40 pm

Here’s an exercise for you:

Try printing out the system properties in alphabetical order in Java.


import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class Main {

public static void main(String[] args) {

Map sortedProps = new TreeMap(System.getProperties());

for (Iterator itr = sortedProps.keySet().iterator(); itr.hasNext();) {
String property = (String) itr.next();
System.out.println(property + ” = ” + sortedProps.get(property));
}
}
}

Now try the same thing in JRuby:


ENV_JAVA.sort.each { |e| puts e.inspect }


Chris
August 21, 2008: 12:08 pm

I believe those names and emails are meant to be replaced when you post….


Jeremy Weiskotten
August 21, 2008: 11:28 am

Umm this is my first visit to your blog, yet it seems to have identified me as Jeremy Weiskotten. I am not he.

Seems like a bad bug.


Jeremy Weiskotten
August 21, 2008: 11:05 am

I think this article would be best summarized as, “There is a huge premium to what you already know.”

Why am I not bothered that current implementations of Ruby are not that fast? Ruby has a couple of tricks up its sleeve that make its speed not matter, most of the time. It’s such a wizard at letting me think and code at a very high level that most of the time, I can get the icky n squared algorithms out of there and go straight to the yummy algorithms. A hash instead of a list, a cache instead of looking it up every time, etc. And I can do these things at a very low cost, without dirtying up the code or spending much time. The goodness of the algorithm is more important than whether or not your language is interpreted or compiled. The second trick is that Ruby makes it blindingly easy to call C programs, and being a Linux programmer, I float on a sea of open source C programs which are well written, well documented, and very fast. There is almost always one around to do whatever graphic, math or other whiz-bang thing I need done, so I shell out to them when there’s heavy lifting to be done. No need to reinvent the algorithmic wheel.

One might suppose that underlying Cedric’s challenge is the idea that whatever language best solves it is the best language overall, but that would be untrue. It’s just the best language for that challenge. And even then, there are different ways to measure best. Is it the most readable code, or most quickly written. Or the quickest to execute? Or that which uses the least power to execute on a portable device? If the challenge were, instead, to involve string manipulating using regular expressions, Java and C would fall flat on their faces; Ruby, Perl, and several other languages would spring into the lead. It is the benchmark that determined the winner. It is not, by any means, an objective contest.

To turn this remark on its side, the is probably a language out there that is, in some objective way, better for what I am doing than is Ruby. But, because I know Ruby so very well, it has a huge premium over that other language. That premium is not imaginary, it is real. Even though that other language may be objective better, I have to consider the fact that I know Ruby and I have work to do. That tips the scale enormously, for me.


Jeremy Weiskotten
August 21, 2008: 10:16 am

You might be the first person to call Java code “concise”. Sorry, I disagree. Java is by far the noisiest language I have ever seen.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :