logo
  • Jobs
  • About Me
  • Contact
  • Home
« Google Calendar Sync
RailsEnvy.com »

Extenstion methods for expressive code

Posted March 12th, 2008 by Matt Berther

One of my main likes of the Ruby language is that it very clearly allows you to express programmer intent. I don’t want to get into the argument of whether or not Ruby is more beautiful than another language. Suffice it to say that it is just as easy to create ugly code in Ruby as it is in any other language. However, the tendency is towards very clearly expressing intent. This is what constitutes beautiful code to me.

For example: I would much prefer to read this code

def main
	20.times { puts "Hello, world!" }
end
 

In my opinion, it is very clear what this code does. Contrast that with the following c# code:

public static void Main(string[] args)
{
	for (int i = 0; i < 20; i++) {
		Console.WriteLine("Hello, world!");
	}
}
 

Not only is the latter more verbose, it also puts the responsibility of the iteration into the hands of the client.

C# 3.0 introduced the concept of extension methods which promise to allow you to create more expressive code and abstract away some client complexities. To test how this might work, I set out to create a C# version of the Ruby code above. This is what I came up with:

class Program
{
	static void Main(string[] args)
	{
		20.times(delegate { Console.WriteLine("Hello, world!"); });
	}
 
	static class Extensions
	{
		public static void times(this int number, Action a)
		{
			for (int i = 0; i < number; i++)
			{
				a();
			}
		}
	}
}
 

Much to my surprise, this not only compiled, but actually worked. Also, I got intellisense after the . on 20. These techniques really give C# developers the power to express the intent of their programs. Isnt this what beautiful code is really all about?

This entry was posted on Wednesday, March 12th, 2008 at 11:55 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Joey Beninghove
March 12th, 2008

Yep, cool stuff. I recently saw an opportunity to start a OSS project for this. If you’re interested you can check it out here: http://joeydotnet.com/blog/category/56.aspx

Trumpi's blog
March 13th, 2008

Our daily link (2008-03-13)…

Sorry about the lack of categories yesterday. I was really lazy. Web ValidatorCalloutExtender does not…

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
Jobs
mattberther.com © 2003 - 2009