fallenrogue.com

Ruby vs C-Sharp or reason number 3259 I'm in love with Ruby

Oh Ruby! How I love thee! Let me count the ways… in lines of code. The fewer the lines… the more I love!

Ok, Ruby, I want so badly to create a series of new objects (to become database records). I’ve got 5 names which are the single property of the class “Role” which inherits Active Record. GO!

%w{god normal probation banned spam}.map{|n| Role.create(:name=>n)} 

You’re done? You’ve created 5 new records in the database as well as 5 new objects that can be used right away? That’s, seriously, how much I love thee.

Ok, enough with the Elizabethan mockery. I seriously love it. I’m going to do a little compare and contrast of why, at night, I use Ruby over what I have to use, C#, during the day.

C# example of the same code with class.. Let’s assume there is not an equal to ActiveRecord in C#. There is; someone did an open source project that does it… pretty cool too, I’ll try and find and post the link to it for my poor .net brethren. oh, I’m also on a Mac without mono… so there may be typos in here, please post if I miss something.
//class pretend I've got my using statements.
public class Role
{
    private string _Name;
    public string Name
    {
        get{return _Name;}
        set{_Name = value;}
    }
    public Role(){}
    public void create()
    {
        //try catch omitted for space and time.
         SqlConnection conn = new SqlConnection("myConnString");
         SqlCommand com = new SqlCommand("INSERT INTO 
                    tblRoles (name) VALUES (@name)", conn);
         com.Parameters.Add(new SqlParameter("@name", this.Name));

         conn.Open();
         com.ExecuteNonQuery();
         conn.Close();
         conn.Dispose();
    }
}
//implement it.
string[] names = new string[]{"god", "normal", "probation", "banned", "spam"}
foreach(string name in names)
{
    Role r = new Role();
    r.Name = name;
    r.Create();
}

I count 31 lines of code. This examples uses standard C# code and techniques. Now, you know how this is gonna go… Here is the SAME thing in Ruby (specifically Ruby on Rails)
#class
class Role < ActiveRecord::Base
end
#implementation.
%w{god normal probation banned spam}.map{|n| Role.create(:name=>n)} 

Ok, before any of you geeks out there call shenanigans on me let’s be a little honest. 31 lines of C# to 3 lines of Ruby may not be fair. I had to include the “create” method because I’m assuming no inheritence to “ActiveRecord” C#. I’m sure this would add a few lines to my Ruby class if I needed it.

The more compelling reason why I love Ruby is the implementation code: 1 line versus 5. This seems small but when you’re coding day in and day out, it can add up. Ruby keeps things simple, elegant and easy to implement. It’s a joy to use and frankly, I’m so glad that it came along when it did, cause man did I need a pick me up. For me, the move from Classic ASP to ASP.NET was a joyful one. ASP.NET 1.0/1.1 were breathtaking revelations when compared to Classic but with the bloat that is ASP.NET 2.0 and the marketing ploy that is ASP.NET 3.0, small compact and powerful languages like Ruby are beginning to prove their weight in gold.

Am I hating on C#? A little, but it could be worse. It could be Java (OOooo Snap! Oh no he didn!). Am I loving on Ruby? You bet your sweet ass I am. Loving every minute of it.

UPDATE! I’ve taken it back.

After reading the feedback, which has been very intelligent and well articulated, I’ve decided to write a follow up which can be read here. After thinking about what I said in this article, I realized one very important thing: That in the grand scheme of things… it doesn’t really matter. Code what you love and not what others say you should love. Please, check out the new article for continued insight to my feelings on this topic in light of some very compelling discussion from commentaries here as well as on dzone and reddit


articleStats

Here are some silly little facts about this Ruby vs C-Sharp or reason number 3259 I'm in love with Ruby...

It was written by over 2 years ago.
It has 4617 letters in it.
It has 682 words in it.
It has a total of 19 comments in all.
So far judy has the last word!

article Links

These are the links that appear in this article. They probably don't make sense out of context... but just in case. :)

which can be read here
dzone
reddit
 

The other stuff...

What the kids are saying...

over 2 years ago bryanl said...

man... these comparisons aren't very fair. i'm no fan of c#, but using the rails orm isn't a very fair comparison to using straight sql with c#.

don't forget to include your databases.yml in your line count as well.

over 2 years ago wtd said...

Well, to be perfectly honest, it isn't "the rails orm." ActiveRecord can be used without Rails.

over 2 years ago fallenrogue said...

@bryanl: I agree and that's why I say that in the article. I then also point to the real "value" comparison that is the implementation code for both languages.

over 2 years ago Supermike said...

Ruby On Rails makes for unreadable code.<br>

Without you explaining it, I could read the C# code just fine, but the Ruby required me to read your stuff.<br>

Besides, the time test shows that C# is faster, and PHP edges out by a second (sometimes two) on most Ruby stuff.

over 2 years ago fallenrogue said...

Yeah, I've already taken it back... sort of in a newer article which can be "read here":http://www.fallenrogue.com/article/view/126-that-damn-hammer-or-why-my-previous-article-was-off-base. I've basically changed my mind a bit, readability, much like this argument, is in the eye of the beholder. I'm sure many Ruby coders will look at my single line and go "oh yeah, that's very readable!" yet in my C# only days I would have never thought that was readable.

over 2 years ago nostrademons said...

@Supermike: Readability depends heavily on what you're already familiar with. I could read both - but I could skim the Ruby code at a glance, while I actually had to read through the C# code to understand what it was doing. I'm familiar with both Ruby and Java, but with no C# experience (it's virtually identical to Java readability-wise, though).

over 2 years ago Rich said...

I think that the comparison is relatively fair. If I'm working in ASP.NET or Ruby on Rails, the code above is an accurate representation of what I'd have to do to get the classes established and data into the database.

It's really a matter of what the framework + the language equals, not just the language itself. For natively creating objects that are reflected in a database, Ruby + ActiveRecord is less lines of code than C# + .NET.

The unreadable code comment doesn't ring true to me - but maybe that's because I've got a lot of Perl in my background and Ruby does share some similarities with it. But I can see from the line of code (singular!) to create new entries in the database, that it means for each of these strings in the collection, instantiate a new Role class and create it with the name property set to the value of the current member of the array. Same as the C# example.

Granted, it is less verbose and reads less like English - so if you're not familiar it's less readable. But if you know Ruby and what ActiveRecord does, the code does make sense. Chinese is unreadable to me, but I imagine that it makes a lot of sense to some people :).

I think that unreadable code generally comes from something being bad code - although I've looked as some Lisp code and maybe that's not always true... I had trouble reading it through all of the parenthesis. But again, there are probably some people who really love it and find it very readable and useful. I guess that I'm just not one of them.

over 2 years ago JMS said...

C# = Any new developer that's familiar with C-style syntax can read it (for the most part...they might have to dig into the assemblies for method signatures or object semantics.)

Ruby = Ruby coders can read it. Everyone else can take a hike.

Personally, I'm a fan of standardized language structure. Just because you can write DES3 encryption in PERL with one line of code does NOT mean you should. It's the old argument: L33t hax0r code vs. easily readable code.

Personally, from a design perspective, I'll take the latter.

over 2 years ago Rich said...

@JMS
I would agree that C#'s probably more immediately familiar to most people because of its C origins

Having done a lot of Perl in my time, it is a balance between trying to write everything in one line and writing readable code. I just like some of the shorthand functions like 'map' that Ruby and Perl offer rather than having to be more verbose in the C-style languages.

over 2 years ago fauigerzigerk said...

Rich, if this comparison is fair then the language with the largest number of libraries always wins and that's certainly not Ruby. A fair point that could be made is that Java and C# have a rather tedious way of defining properties/accessors.

over 2 years ago Julio César said...

You can also write it as:

Array.ForEach(new string[] { "god", "normal", "probation", "banned", "spam" }, delegate(string name) { (new Role(name)).Save(); });

Just one line too and pretty close to the Ruby version. The Array.ForEach method was added in .NET 2.0 an the anonymous delegate syntax (a more verbose Roby block) was aded in C# 2.0.

Also note that this isn't the usual way its written in C#, but it is available if you want to.

over 2 years ago fallenrogue said...

@ fauigerzigerk: hit it right on the head. Java/C# really need a better way to define accessors. In fact, in Objective-C I've noticed (in controllers) Devs just giving the class a hashtable called "properties" rather than going through the hassle. I'm not saying it's a good idea, but certainly inventive!<br>

@Julio: I'm glad that you posted that snippet, because I debated as to whether or not I should. Mainly, because (as you state in your comment) it's not the standard practice and I would assume more C# devs would consider it not very readable... which seems to be a secondary debate in this comment thread as well! :)

over 2 years ago Roger said...

.NET ca pu

over 2 years ago Rich said...

fauigerzigerk, I just meant that in terms of creating objects w/representations in a database, I think that Ruby on Rails is a lot easier and less code than C#. Not so much as a language A is better than B sort of thing.

over 2 years ago Ryan said...

Wow, that's ridiculous. I use (unfortunately) C# at work, and I develop in Ruby at home (for others and myself). It's amazing how much of a difference there is, but that example really, really show's how Ruby makes things easier, and more fun.

over 2 years ago fallenrogue said...

Me too, Ryan. There's not a day that I don't say out loud -"Damn. If only I were at home I'd be done by now!" :)

9 months ago Paul Kline said...

While I agree ruby has a lot of advantages, C# can write things like that now and even in a static type language. The previous comment about using delegates, I use delegates ALOT and know how to use them quickly. Little verbose in 2.0 but in C# 3.0 (NOT .NET 3.x) the syntax is prettier.

9 months ago Leon said...

Hi Paul, this article was written almost a year ago and now that the C# 3.0 spec (included in .net release 3.5) is out there is a wonderfully terse syntax for this through lambdas. So, if I were to write this article again today, it's possible I would come to a differnt conclusion. Also, if you read through the article, I take it back in a future article: http://www.fallenrogue.com/article/view/126-that-damn-hammer-or-why-my-previous-article-was-off-base

That said, if I were to think about this topic again, more objectively and with less "fanboy" tone for Ruby, then I think the following is true: C#, in it's forumlative days, did too much to mimic C++ and Java in order to drive adoption at the expense of syntatical sugar, duck typing, generics, etc. There are concepts implmented in languages like F# (and it's ML brethen) that could have been applied to C# at a much earlier time in the specification to reduce some of the overhead described above. Instead we had to wait for now and I'm happy to say that I still develop in C# and I still enjoy it. The 3.0 spec beings some welcome additions to the language that I hope to start taking advantage of soon. In fact, I've given several talks on the C# 3.0 spec as well as how it applies to LINQ.

thanks for the comment, Paul!

2 months ago judy said...

Great site. I will bookmark for my sons to view as well!!!o

Leave a comment
*name:
*email: (never sold or published.)
url :

©2000-2008 fallenrogue.com | Some Rights reserved.