Rails and Django - Programming Language (part 3/15)
Programming Language
It could be argued that the most telling difference between Rails and Django is the language they're implemented in, and the culture that has built up around the languages' communities.
Language Community
Here are some rough metrics on the popularity and communities of the the Ruby and Python languages (and Php for comparison).
| Programming Language | Ruby | Python | Php |
| Year invented | 1993 | 1991 | 1994 |
| TIOBE Index (Feb, 2007) | 10th (2.5%) (up from 20th Feb 2006) | 7th (3.6%) | 4th (8.8%) |
| User Groups, USA | 94 | 32 | |
Chat room members | 373 (#ruby-lang) | 357 (#python) | 330 (#php) |
| Freshmeat projects (www.freshmeat.net) | 327 | 2442 | 3798 |
| Frameworks (sampling) | Rails, Nitro, Merb | Django, TurboGears, Pylon | CakePhp, Symfony |
The TIOBE Programming Community Index (http://www.tiobe.com/tpci.htm), for what its worth, is a rough indicator of the popularity of major programming languages (the top 3 languages being Java, C and C++ respectively, FYI).
Both Php and Python have more history and a much wider breadth of usage than Ruby. Python, for example, is known to be used in mission critical projects at NASA, an indication of its quality and reliability.
The Ruby language, a relative newcomer in the West, was invented in Japan. In the US, most people know of (and use) Ruby in context of the Rails framework. Although many Ruby champions might strongly disagree with this statement.
Language Features
It is not my intent to directly compare the language structure, syntax and semantics. I will only share some impressions.
Ruby and Python are really quite similar. For example,
- Both are strong object-oriented languages. That is, everything is an object.
- Both are dynamic languages, lets you write code that writes code.
- Both are dynamically typed (because it doesn't use explicit data-type declarations) and strongly typed (because once it has a data-type it actually matters)
- Both have a compact syntax.
- Both are quite easy to read, which is important when going through other people's code (or even your own a few months later).
- Neither requires braces around bodies of code (Ruby uses “end” to indicate the end; although you can use braces if you want. Python relies on leading whitespace indentation).
- Both have an interactive command-line console.
- Both have testing infrastructures
- Both have doc generators (from comments in the source code)
- Both pride themselves on being a generation ahead of languages like C, Php, and Java
Ruby
Ruby has an especially compact syntax. No wasted words, that's for sure. It is said that Ruby's program statements read more like a natural language (or rather, a domain-specific language) than a general programming language. People pride themselves on how small they can reduce a block of code, like solving a puzzle. (And part of the challenge is to make the resulting code even more readable than the more verbose version; you certainly don't want to make it cryptic). This helps make programming in Ruby “fun”, a word people seem to associate with Ruby and Rails development. Common programming patterns (idioms) have become part of the Ruby culture.
Some cool things about Ruby: Don't need parenthesis on function calls. Dynamic blocks are like functions 'on the fly' , which can be used for example, with iterators and lists. (For example, ('a'..'e').each {|char| print char } evaluates to “abcde”).
RubyGems is the tool for downloading Ruby library packages.
Python
"Python code seems to be distinctive in having a particular character, and the Python community seems to have a lot less Cowboy Programmers than most other languages. If you hate to conform to standards, you won't like Python I guess." (http://mail.python.org/pipermail/europython/2005-April/004995.html)
Python has a strong following as a general purpose programming language. It is object-oriented, but you can use it procedurally if you want (Django doesn't). Python has a clearer (or perhaps just more formal) syntax than Ruby. Indentation (leading whitespace) is significant, to group statements.
Python programmers use the endearing term “Pythonic” to describe code that adheres to the Python philosophy. See http://en.wikipedia.org/wiki/Python_philosophy (it's also hidden within Python itself, enter: >>> import this at the Python prompt).
Python has a large selection of third-party libraries available and is used in a wide range of domains, including the engineering and scientific communities. Python has the setuptools package manager (amon others). There are over 10,000 libraries listed on the setuptools site.
Opinion
Ruby is a bit looser in how you express yourself, like its idiomatic expressions and dynamic blocks. In that, Ruby does seem more “fun”. Some people take to Ruby immediately.
Other people are really bothered by this aspect of Ruby, believing a programming language should be more concise, preferring that when there is one best way to express something, your code will be more clear, less prone to bugs, and more portable between programmers.
Ruby programmers who have used Python say they were grateful to leave behind little "annoyances", like parenthesis in function calls, having to declare the “self” argument to methods, and the special use of “__” (double-underscores) (e.g. __init__ for constructors). Python users defend these as non-issues that aid clarity. I suppose when little things like this bother you, it can grow increasingly annoying, until you finally want to kill someone, like in an Edger Allen Poe story (well, maybe that's an exaggeration).
You certainly don't want your language to be tedious. It doesn't look like either Ruby or Python are that. Far from it. But otherwise how important is this “fun” thing anyway? Seriously. (no pun intended). Ultimately the fun is in building websites that work, reliably. And I have no doubt, whichever language you use, the pain and frustration will still come when things don't work the way you expect (as I've witnessed on both framework's chat channels).
Conclusion: Both languages seem equally complete and flexible. Python is more mature and explicit. Ruby has that fun-factor. I rate them both a "4" (out of 5), for different reasons.
Comments
>>
My main beef with Ruby is that it's too much like Perl -- if you give the same task to five programmers, they come up with 5 different ways of doing it, and the other 4 can't read it at first. I started using Perl in 1996 and left it behind for Python a long time ago; Ruby is neat but it seems like a step backward somehow.
>>
I just hate Ruby code. No fun there I can tell you. If you switch from python to ruby you are not annoyed with small things but with everything! Sucks I'm telling you. Python code is so nice and fluent and when you take look at the code you see what it does...
>>
Decent overview and kudos for being fair-handed. My only nit? You underrated the fun factor. A happy coder is a productive coder.
Discovering ruby will make the most jaded perl/java/c#/etc... developer rediscover why they ever loved programming in the first place.
Re: Rails and Django - Programming Language (part 3/15)
Having moved from Perl to Python to Ruby (over several years), I can see your point. However, I view Ruby as "Perl done right".
I find I'm blazingly productive in Perl, but code I've written 6 months ago is unintelligible, even to me, even when I've tried to make it as clear as possible, unless I refer back to my own documentation. With Python, I sacrifice a chunk of Perl's productivity, but get code that's much easier to understand (the "there's only one way to do it" philosopy at work helps a lot).
With Ruby, and by extension Rails, I find I get the best of both worlds - the productivity of Perl, with the readability of Python. Don't get me wrong - on a scale of e.g. 10, I'd rate Ruby a 9 and Python an 8-8.5, but my productivity with Ruby would beat out my productivity with Python and that's a very big consideration.
>>
You are incorrect in claiming Ruby is not strongly typed.
(You write that it indeed is strongly typed on one occasion, and then later turn around and write that it isn't.)
Ruby is strongly and dynamically typed. Period.
>>
Tomas, you're right, in the sense that you can do 1+2, and 'a'+'b', but not 'a'+2. Perhaps I'm confusing dynamically typed with not-strongly-typed, e.g. Ruby does not have typed method parameters or return values.
In my defense :) according to wikipedia "these terms have been given such a wide variety of meanings over the short history of computing that it is often difficult to know what an individual writer means by using them." http://en.wikipedia.org/wiki/Strongly_typed
>>
I'm right in the sense that Ruby is strongly typed and there is no doubt about that. Strong typing is different from static typing. If you meant to say that Ruby is not statically typed (it's dynamically typed), just write that and leave strong typing out of it.
>>
You wrote that Ruby is not strongly typed because "data types share many of the same methods". This is completely and utterly unrelated to whether Ruby is strongly typed or not. Just because Ruby makes an effort to be consistent does not mean that the language is not strongly typed.
>>
Thomas, thanks for the feedback. I have corrected the article to say both Ruby and Python are both strongly-typed and dynamically-typed.
Re: Rails and Django - Programming Language (part 3/15)
If you like just one way to do some thing maybe you should consider FORTRAN. It's good way to force people writing the codes in the same way. "Too mych like Perl"!?!?!?
>>
You only compared the language's syntax, there's room for comparing their performance and portability aspects. Python washes Ruby out of the wall on those aspects.
>>
I don't know about ruby but but python is a great general purpose language. I have friends who are ruby people and they say ruby is great and I am sure it is. I use python every day, and my friends seem to treat me as the python geek for language wars.
Regarding performance; with psyco, django is wonderfully quick.
The big problem I have with python is the lack of encapsulation support. This is especially a problem with django's template system where template designers can get their dirty hands on your object's private parts. One way is to prefix with an underscore, but it seems clumsy and hacky and leaves your code none the prettier. All in all, without encapsulation, python can't call itself a real OO language, but it doesn't do badly.
The other big problem, and related to this, is isinstance().
Type sniffing with isinstance is, I would say, not a crime but a tragic and filthy habit worse than using switch statements. Few things wreck flexibility more.
So, want to know how well written django really is? grep a django checkout for "isinstance". Last time I checked there were over 600 usages! Now consider that in python 2.1.1 there were only 68 usages, mostly in test code testing that it worked... you get the picture. Here's more info:
http://www.canonical.org/~kragen/isinstance/
So we can see, django has a serious addiction to sniffing types. Sure it gives it that 'quick fix' to get it through the releases but one day it will wake up, look in the mirror and realise it badly needs a few months at the refactoring spa.
New Comment