Saturday, March 2, 2013

Ruby vs Java

There are probably hundreds if not thousands of articles with the title "Ruby vs Java" or "Java vs Ruby"; nevertheless I decided to write down my considerations about the way these two languages compare to each other.
So let's start by looking at the advantages that each language has over the other.

Ruby advantages

Everything is an object


In Ruby everything is an object, even primitive types and classes. The fact that a class is an object makes it much easier to write unit tests because it allows you to mock them and to create stubs for class methods as well.
In Java, instead, you cannot mock static methods. This makes it difficult to test units that make use of them. You end up implementing instance methods even for functionalities that do not use any information about the object's state but just to facilitate testing.

Modules


Modules allow for a class to include functionalities without the need to extend any parent class. This makes it easy to reuse cross-cutting concerns like logging, security, transaction management and so on.
To achieve the same in Java you need to use delegation which can require a lot of repetitive code.
Module are sometimes abused, though, like Ryan Bates points out in this gist.

Blocks of code


This is one of the main attractions for newcomers to Ruby. Blocks of code allow for a very concise syntax to implement the Template Method Pattern.
Java requires the definition of an interface and multiple classes implementing it, this again results in more verbosity.

Duck typing and syntactic sugars


Both features make the code extremely compact and readable: no need for type definition or type casting, no semicolons at the end of the line, no parentheses for method invocations, operators overload, getters/setters automatically available for all instance variables and many, many more.
Ruby code is incredibly expressive and easy to read, many say indeed that "Ruby speaks English". To understand the concept it is enough to read some tests written using RSpec, which I consider one of the best library I ever used.
On the other hand, Java code is verbose and it can require multiple lines of code for stuff which you could get done in a single line in Ruby.

No compilation required


The thing that annoys me most while working on a Java web project is that every time I change something , even if it is just a Javascript or CSS file, I need to re-compile and re-deploy. This is not the case with Ruby resulting in a boost in productivity.

Java advantages

Performance


Performance can be the single reason to prefer Java over Ruby.
In web applications, though, 90% of the time is spent in the database, in requests to external systems and not in the language itself. If your web app is slow and you decide to replace Ruby with Java, you will probably not achieve much improvement.
On the other hand, if you are working on an application making intensive calculations, than Java outperforms Ruby. The following benchmarks executed on a x64 Intel quad-core running on Ubuntu gives you an idea of the difference in performance between the two languages: http://benchmarksgame.alioth.debian.org/u64q/which-programs-are-fastest.php.

Multi-threading and concurrency


The results from the previous benchmark derive also from the fact that Ruby, or at least the MRI implementation, does not support threads. JRuby and Rubinius do support threads but I think 95+% of Ruby production environments are based on MRI.
In a web application, it is necessary to launch different processes in order to be able to handle multiple requests which is something that does not scale very well.
Doing parallel computations is very problematic without threads. If using separate processes, the only way to let them share data is through a database, a file or some other external resource whose access speed is obviously way slower than memory.

Interfaces


Interfaces are a very simple and clear way to define the contract between a piece of software and the external world. Unfortunately, many people abuse them and create interfaces for every single class they write.
However, when used properly they do help understanding how a module works and how to use it. Furthermore, interfaces allow to modify a module or to completely replace it in a manner completely transparent to rest of the system.

IDE support


Eclipse, Intellij, Netbeans all make the life of every Java Developer so much easier by refactoring variables, methods, classes. They also let you quickly navigate through the classes and save a lot of keystrokes with auto-completion.
The features I consider the most useful are those for reverse engineering, like showing class hierarchies or methods usage. This can be a life-safer, especially when working on big, old projects, where many contributors were not writing really clean code, just to use an euphemism :)
Because of the extremely dynamic nature of Ruby, IDE cannot help much in this sense and without an extensive test suite it can be really hard to extend or modify an old Ruby project.

Conclusions

I think both Ruby and Java are great. It would be nice if they could somehow blend into one language that combines the best of both worlds. Actually, I think this language already exists!!! I am talking about Scala, of course.
Scala combines the flexibility, expressiveness and conciseness of Ruby with the performance, scalability and tools support of Java. Scala takes this even further by being a truly functional language which makes it suitable for concurrent, scalable and fault-tolerant applications.
Companies like Twitter and Linkedin are successfully using it and I believe there prospects a really bright future for this language.

11 comments:

  1. There is also JRuby, you can write lots of stuff in ruby and integrate already existing codebase in java or just write in java code that needs to be optimized :)

    ReplyDelete
  2. Yes, I indeed mentioned JRuby as well :)

    ReplyDelete
  3. What about java's built in GUI, and the ease of deployment?
    Is there anyway in Ruby to easily deploy an application with a GUI, without a lot of messing around installing say tcl/tk, then the ruby interpreter, then your code?
    I'd say that's a win for java in terms of useable code you might want to sell to someone. Run one jar file, as averse to lots of instructions on downloading this and downloading that.

    ReplyDelete
    Replies
    1. As for the gui part Rubymine is awesome, and i think you can compile ruby code with rubinius

      Delete
  4. Nice post! Have you ever wondered how the transition between on to the other language works? My friend switched from Java to Ruby some time ago (she has described the move at; https://netguru.co/blog/posts/brace-no-more-going-from-java-to-ruby-on-rails) and it looks like it's not an easy ride.

    ReplyDelete
  5. thats why I prefer groovy. its uses the JVM and its a very dynamic language.

    ReplyDelete
  6. I think Groovy is the language you are looking for Scala for me seems to have to many options choice and incompatibilities. Should have followed the KISS principle.

    groovy on the other hand has nearly every feature you could desire is inspired by Ruby, Smalltalk and Python.

    ReplyDelete
  7. This articles tell high traffic websites use java rather than ruby on rails as twitter migrated towards java from ruby on rails.

    http://blog.cygnet-infotech.com/2013/10/is-it-ruby-on-rails-or-java-for-you.html

    http://www.letmeshare.in/2293/getting-started-with-ruby-rails-learn-ruby-rails-from-basics

    ReplyDelete
  8. thanks for the comparison...

    ReplyDelete
  9. This is one awesome post.Thanks Again. Really Cool.

    ReplyDelete
  10. and what would be your opinion on Perl VS Ruby?

    ReplyDelete