Saturday, June 21, 2008

Lambda => Epiphany

I am back! There was a lot of speculation that this adventurer had fallen into a large pit of quick sand or appeared on Survivor. I can neither confirm nor deny that either of those events happened.

Something nearly as drastic did happen however. I converted to C#! It actually was much easier than escaping from quick sand. With my classic ASP, HTML and JavaScript background the jump from using JavaScript to C# was quite natural. Dare I say "easy?" In fact if anyone is on the fence about which .NET language to develop in and you will be writing some JavaScript I cannot encourage you enough to choose C#. You will become a much better JavaScript programmer and you will only have to remember one set of syntax.

With so much scripting involved in modern web pages and that frequency increasing daily it just makes sense to be as strong as you can be in JavaScript. Developing in C# everyday will make that happen. Of course, the fact that Visual Studio 2008 now has JavaScript intellisense and breakpoints makes JavaScript programming MUCH easier. Bless the good folks in the VS IDE Team for making that possible!

Enough about where I've been hiding the past 11 months, let's get to something interesting.

Today, I attended the Sarasota .NET Developers Group's Visual Studio 2008 Launch Event. There were 3 very good presentations. Each got me thinking. I will be writing about several of these thoughts over the coming days (well, nights).

In David Hayden's (a local Sarasota C# MVP) presentation about the new 3.0 language features he mentioned lambda expressions and gave a nice example of how to route event handlers directly to methods and I thought, "That's a great way to save a bunch of useless typing." Because I get paid by the year, not the number of lines of code I am always in favor of typing less code.

So, here's how it works...

As you can see I'm adding the eventhandler to each button's Click event in Page_Load. Notice that I don't even have to write the traditional event handlers (commented out at the bottom). I can just skip that and get right to the fact that I want the event to invoke my own methods, "SaveChanges()" and "CancelChanges()". Since I have gotten in the habit of trying to never write more than 1 line of code in an event handler this works great for my coding style.

Another added benefit to this lambda syntax is that I do not have to write any code in the ASPX page related to stating which event should be associated with each control's Click event.

All of my event handler associations are in the .cs file. This is just one more way to break the presentation layer from the business logic. I believe that in 1 year I won't be doing web pages in ASPX but rather XAML I want to keep as much as I can out of the ASPX pages.

No comments: