Ruby vs. C# Smackdown
Monday, August 06 2007 - Blog
I recently attended a meeting of the Dallas C# SIG, a group which I hadn't attended in quite some time, because they had Adam Keys come in to talk about Ruby. I don't live under a rock, so I've heard the buzz about Ruby but I hadn't spent much time looking into it because it wasn't immensely relevant to my day to day. That said, this was a great chance to learn so I checked out the meeting and I must admit, I was impressed. I'm hardly a convert but I can understand Adam's point that Ruby "feels" more enjoyable to write for him.
While most things Ruby can do I think C# 3.0 has well in hand, there still some things C# can't do yet. The dynamic augmentation of an instance, not a type, is intriguing to me and certainly powerful. One feature that I thought was interesting, and immediately doable in C# 3.0 was the way Ruby can handle loops. For instance take this Ruby code (syntax errors possible, this is not checked) :
50.Times doPuts "We Built This City ..."Puts "We Built This City ..."Puts "On Rock And Roll"end
I like this much better than the standard C# syntax of a For loop because it focuses on what is important. Its terse yet clear, and I wanted in C# 3.0 right now, so here it is:
public static class LoopingExtensions
{public static void Times(this int upperBound, Action<int> action)
{for (int index = 0; index < upperBound; index++)
action.Invoke(index);
}
}
This results in a very nice piece of C# code, equivalent to our Ruby example above:
50.Times(i => {Console.WriteLine("We Built This City...");Console.WriteLine("We Built This City...");Console.WriteLine("On Rock And Roll");}
Simply enough to implement, and a great example of using Lambda expressions and extension methods to improve the readability of your code.
- #1 OJ on 8.07.2007 at 6:04 PM
-
Let's hope that down the track these kinds of common 'tricks' make it into the core rather than having to be hand-coded.
- #2 Mark Brackett on 8.07.2007 at 8:54 PM
-
If you're not mentally translating for (int i = 0; i < 50; i++) { }into "do the following 50 times", then I don't think any language can save you.Sure - 50.Times do may be easier for a non-programmer to read, but that's why non-programmers don't read source code.Besides, isn't Do.This(delegate { Console.WriteLine("We Built This City..."); Console.WriteLine("We Built This City..."); Console.WriteLine("On Rock And Roll");})(50).Times;just as readable (and doable with C# 2.0)?
- #3 Tim Rayburn on 8.07.2007 at 11:38 PM
-
Mark,Your right, most programmers do think "do this 50 times" when they read:for(int i=0;i<50;i++) { }But my point is that the code above puts in front of you so much more than that, which most times you don't need to know because you're assuming it works "like every other for loop".For instance:* Declaring the loop variable* Stating that it will increment by 1* Stating that it will start at 0Those things shouldn't need to be re-stated every single time just because of language syntax needs. See my point?As to your other example, it can of course be done in C# 2.0 with anonymous delegates, but the syntax is much much clearer in the 3.0 version because it puts the most important noun (50 in our example) right up front.Your version works, but reads like Yoda "Do this times 50 you will...".
- #4 Jonathan Holland on 8.08.2007 at 12:02 AM
-
There are far greater things to worry about in coding than discussing who has the best syntax for a loop. The conventional For loop is a holdover from the K&R C days, and is the most widely recognized loop construct ever written.Why would you want to take source code that is semanticly very similar to C, and only confuse the reader by using a Lambda to make the langauge appear to be something its not?I'm a bit of a purist, in that I love your typical curly bracket language to death. I feel at home with my for loops :)That said, despite the fact that your post was really not a comparison of C# vrs Ruby (I expected to see some benchmarks or something) and more of a comparison between two different language patterns, I'm still voting this up because it is a good example of the new lambda expressions in C# 3.0.
- #5 dotnetuncle.com on 8.21.2007 at 5:01 AM
-
The syntax is new, yet interesting.But it will be a new game altogether.Woops!Vishal Khanna



