Rails vs. ASP.NET (Really, Ruby vs C#)

Where have I been...why the lack of posts: a) holidays, b) knee injury playing basketball (that required surgery to repair) has slowed me down, c) recently, I've started to work on a project where ASP.NET using C# was the requirement.

So, I haven't been working in Rails for a few weeks and it's been a few years since I've used ASP.NET or C#, so I've had to jump back into it a little to refresh myself.

Now, look, I'm not trying to start a religious war here: ASP.NET and Rails each have their own benefits and drawbacks. However, I would like to say this: for simple web sites Rails is so much easier.

Here's a simple example: I've got a URL stored in the database, such as http://www.mysite.com/foo/fee/foh.htm and I need to get the foh.htm (which isn't stored).

ASP.NET with C#

    public string FileName
{
get
{
char[] delim = { '/' };
string[] a = this.Url.Split(delim);
return a[a.Length - 1];
}
}

Rails with Ruby
  def filename
self.url.split('/').last
end

I'm sure I'm not the first person to make this comparison. I just felt compelled to speak out about the many moments during the day where it takes me several minutes longer to do something with ASP.NET and C# than Ruby and Rails.

6 comments:

Jörg W Mittag said...

Hi,

while I agree with you that Ruby is more expressive than C#, this particular example is a little unfair, because your C# example is written in a totally different style than the Ruby example:

* the Ruby example uses the Array#last convenience method, the C# example uses Array indexing,
* the Ruby example uses message chaining, the C# example uses temporary local variables and
* the Ruby example passes arguments as literals, the C# example uses temporary local variables.

Also, the C# example uses unnecessarily complex types (a char[] Array instead of plain char) and unnecessary type annotations (all types can be inferred).

To make those two examples more comparable, both would have to be written in the same style. Either the Ruby code should look a little more like this:

def filename
delim = '/'
a = self.uri.split(delim)
a[-1]
end


or the C# code should look more like this:

public string FileName
{
get { return this.Uri.Segments.Last(); }
}


Either way, the Ruby code still wins, but the difference is not nearly as extreme as in your original example.

I've posted more detailed explanations at RefactorMyCode, with proper code formatting.

Greetings,
Jörg.

XIU said...

First of all, the array isn't needed and with .NET 3.5 we also have the Last() method on enumerables so:

return Url.Split('/').Last();

Looks pretty much the same as the ruby code to me :)

Unknown said...

I agree with JoergWMittag on this one... while usually Ruby uses less code and usually (!) is faster to write, this specific example isn't fair towards C# and .Net.

Kevin said...

Yes, but Ruby was first. Seems to me like Microsoft is making C# look more and more like Ruby everyday, take MVC3 for example, Rails copied to a tee, not everything though, they still have alot to implement but I'm sure they'll get there. It's sad, but once they do it, it seems as if they did it. I work with MVC developers everyday that don't even know Ruby exists. LOL.

Anonymous said...

C#

string filename = Path.GetExtension("http://www.domain.com/directory/file.html");

Simplz!

Unknown said...

Nice post very helpful

dbakings