Saturday, April 29, 2006

I was wrong, wrong, wrong...

On my website I posted an article discussing the relative power of programming languages. I stated that it was an unfair test by Paul Graham to determine the power of a language by how it handles closures. I was wrong. I admit it, I was wrong. I also talked about how I was unsure if other languages offered the same functionality as reflection in C#. I was wrong about that too. I am o for 2. Let's see if the third time is the charm.

You're probably thinking (I am) "why am I reading this guy's blog if he is wrong all the time?" The answer is straightforward; I learn from my mistakes. Paul Graham was right, a closure is a good test of the power and complexity of a language. How did I learn my lesson? Ruby.

A Ruby closure:

def closure(num)
Proc.new {|x| num += x }
end

This code carries the value it is initialized with as part of its local scope, which means that you can accumulate values by adding successive numbers. [Note: I should also point out that this code works as an accumulator for strings as well, due to Ruby's duck typing. In fact, it would work as an accumulator for any class that defined the "+=" method.]

Paul's solutions page supplies an additional implementation of the same functionality for Ruby. The brevity of the code is what is paramount in this example. It is so easy and straight forward to write an accumulator in Ruby, particularly when you compare it to C# (convoluted C# example available in my original article, but trust me it is much more complex and much more wordy). With .NET v2.0 in full swing, I am pretty sure that part of this problem is mitigated by anonymous delegates. While I haven't written and tested any code to support this assertion, from what I have briefly read it appears as though it is much simpler to solve this particular riddle.

Where does this leave us? In my case, the issue is not straightforward. At home, I can work with Ruby. At work I am still using C#. I am the only programmer at my job that has time to learn new languages and compare them in a theoretical sense. If I start writing support programs at work in Ruby, I am certain that there will be complaints because nobody else will be able to edit them without learning Ruby (although, seriously 20 mins?!). So, I am stuck using C# for most things. Not to say that C# is a horrible language, but it is not nearly as powerful as Ruby, or Lisp, or Python, et cetera.

How do I get around this? That question will for the basis from which the next few blog entries are to be written. I have every intention of exploring my varying options. How can we write more succinct code in C#? What techniques can make things easier and faster? I will be exploring various forms of code generation, code reuse, object patterns and C style preprocessors.

Most importantly, I know I am not alone, so the solutions I come up with will be posted on this blog, with full code to back them up. I am not above taking ideas from others and making them my own, as long as it gets the code I need written, written faster (proper credit will be given to any individuals who knowingly or unknowingly contribute to my obsession...I mean goal.)

[Side note: seriously, if you haven't learned Ruby or given it a look, please do. Once the business guys hear about it enough (GO RAILS!) we will start to see companies using it to actually do business. Once we see that, then we can have the chance of working in a language that we would use at home, at work. I am not biased either, I think it would be much more efficient to write code in Python than in C#. I think I am just tired of declaring my variables.]

0 Comments:

Post a Comment

<< Home