Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Thursday, May 10, 2007

I think the guys in the .NET Framework team has this thing about vanity. Yeah! I call it code vanity. Not that its bad.. but its just funny how code has been getting beautiful. more beautiful and even more beautiful as the evolution of .NET goes forward. And it's a great thing too!

Here's a look on how my code changed since i started working with .NET 2.0.

When i first started dealing with .NET 2.0. My iterating-thru-a-list code  looked like this:

//create a new list of numbers(int)
List<int> numberList = new List<int>();

//create a for loop that would iterate from 1 to 10
for (int number = 1; number <= 10; number++)
{
   // add the number to our list
   numberList.Add(number);
}

//iterate thru the list and print the contents of our list
foreach (int number in numberList)
{
   //print the number
   Console.WriteLine(number);
}

This is a common code structure for people moving from the old framework to the newer one. There's nothing wrong with the code but it seems that I am not using the full capability of what .NET 2.0 has to offer.

After several weeks I discovered Action delegates and List.ForEach and boy the code that i started writing looked even sexier!

//create a new list of numbers(int)
List<int> numberList = new List<int>();

//create a for loop that would iterate from 1 to 10
for (int number = 1; number <= 10; number++)
{
   // add the number to our list
   numberList.Add(number);
}

//Iterate thru the list using an Action delegate
numberList.ForEach(
                     delegate(int number)
                     {
                        //print the number
                        Console.WriteLine(number);
                     }
                  );

Ain't that sweet? Now my code looks cleaner, meaner and sexier :P

But then .NET 3.0 came and just when i thought good code was good enough the brainiacs from Redmond introduced another way of making code better. Lambda Expressions.

//create a new list of numbers(int)
List<int> numberList = new List<int>();
//add thew sequence of numbers
numberList.AddRange(Sequence.Range(1, 10));
//iterate and print
numberList.ForEach(i => Console.WriteLine(i) );

Oh Ma! When will this code vanity end?(just kidding) :P

Thursday, May 10, 2007 12:57:28 AM (GMT Standard Time, UTC+00:00)  #    Comments [2] -
.NET | Fun Stuff
Thursday, May 10, 2007 8:21:08 AM (GMT Standard Time, UTC+00:00)
It ends where maintainability ends, dude :)

In your opinion, which of the three is most intuitive and easy to understand?
Thursday, May 10, 2007 9:50:27 PM (GMT Standard Time, UTC+00:00)
Heheh! That's right Jon but then again i think it wont end like that.. i do see myself coding with pictures someday :P(jigsaw puzzle programming anyone?)

To tell you the truth i think the 1.x code structure is easier to understand because if flows and looks natural... lambda expressions are abit confusing the first time you look at it.. anonymous delegates sometimes tend to be overly used. i also believe its dependent on your coding background.. say, ruby developers would preffer the action-list.foreach combination because it looks similar to what ruby has.
Comments are closed.
On this page
Archive
<August 2008>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 246
This Year: 43
This Month: 4
This Week: 0
Comments: 111
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)