"Doubt is uncomfortable, certainty is ridiculous." - Voiltaire

Monatsarchiv für December 2008

 
 

Ahh, the forest from the trees.

My biggest weakness as a C# 3.0 developer is I’ve never programmed in 2.0.  Things like LINQ, Expression Trees, and Lambdas are things I grew up with.  I don’t know a world without them.

When I’m using these new language featues I’m constantly asking myself “How did they do that before?”, “Is there a better way I missed?”.

Today I think I made a good realization regarding expression trees.

Its the body that matters most.  The right side of the stake.  If you just compile an execute the body then why are you using them?

My soft and slow rule at this point is that besides using the IQueryable interface they are perfect for situations where the body matters and you can use that tree to perform AOP ( Aspect Oriented Programming ) techniques on the code without cluttering up the actual implementation.

I’m just glad I have a screwdriver in my toolbox now and not just a hammer.

php is now PHP, all grown up

When I stopped programming with PHP, 5.0 was just out the door.  Back in 2004 most of the object oriented support was brand new and the developer ecosystem behind it was still in its infancy.

In direct contract when I started programming with .NET there was a really mature community behind the 99 level tutorials I was reading.  You couldn’t step outside beginner land without stumbling upon modern and fresh ( to me anyway ) ideas like TDD, MVC frameworks, and dependency injection/mocking/stubbing.

Mix in some LINQ and the newest language features in c# 3.0 and an old procedural style PHP guy was pretty blown away by the power of the .NET stack.

It sure was a suprise to me today when I stumbled upon some new PHP 5.3 features coming down the pipeline.  Closures, Namespaces, nice!

Further investigation revealed there are unit testing, mvc, and dependency injection frameworks already out there ready to.  Since I already installed a pretty decent php add-in for visual studio its really apparent that PHP is not the same infant language I left four years ago.

So I have almost all the same language features available to me in C#, can program it using my favorite IDE, have the freedom to run my code on any platform, and could eventually take advantage thousands of more job and freelance opportunities that PHP offers over C#…

Looks like I have something new to learn…

Expression Trees are the new Magic Strings – Part 1

Like pink is the new black right?

When I started programming I was luck enough to jump right into the C# world riding the LINQ horse.  The first time I foreach’d over an IEnumerable using a .Where() statement was pure heaven coming from a PHP and classic ASP world.  No more {} nesting craziness for me.  The code was just easier to write in that style.

The impact that LINQ and its Extension Methods had was not truly realized until I came back to that first code projects months later after focusing on an entirely different domain.  Because I was ( and still am ) a relative newbie to the object oriented and professional programming world I didn’t comment my code well.  The results where typical, I had no idea why I did what I did and lots of implementation details where lost.

Yet when I came upon my LINQ statements the codes meaning was immediately clear to me.  I knew what I wanted to do with my collections and was able to infer why I did them because the LINQ preserved some of the more declarative meaning about what I was doing.    LINQ created a DSL right inside of my application that didn’t need much further commenting.  On top of it all the code using LINQ rarely had to change when I had to shuffle some bits around.  Replacing one property with another inside of a lambda is so much neater than tracing through the nested if blocks of the C# 2.0 world.

It was at this moment I realized the importance of readable code.  LINQ offers that benefit and I decided I should use it wherever possible.

Almost a year later I now came upon a problem for a new project I’m hacking away at in my free time; Georum, a geographically aware message board application.  This led to my Thanksgiving weekend being wasted researching IQueryable because my only solution to my problem was to implement my own IQueryable provider.  And like white on rice, Expression Trees also entered the picture.

During the next week at work I had another revelation while working on a piece of messaging functionality; I was trying to solve an architectural problem when the true problem was more of a composability one.  Once I realized my true goal was to create code that others and myself could more easily use a solution using Expression Trees just fell into my lap and because of them, no functionality has to be sacrificed.

Expression Trees offer a kind of dynamic hook into execution that used to be the sole domain of Reflection, or if you were truly brave, custom IL output.  The research this blogger did indicates there is a 600% improvement over reflection based code using Expression Trees.

Expression Trees have already been used in a couple of interesting ways in DI and Mocking frameworks but haven’t really made their way into a lot of mundane code examples at this point and I don’t understand why.

Yes, parsing over expressions is a little harder than just calling normal code but I think they offer a tremendous opportunity for us to create composable, self-documenting, and intellisensed dynamic code.  It’s my prediction that Expression Trees will eventually completely change how we work with C# in the long term future just like LINQ changed how we work with collections.

In this series of posts titled “Expression Trees are the new Magic Strings” I’m going to write out my discoveries surrounding Expression Trees, explore some interesting implementations using them, and also eventually offer my own.

My goal is to reproduce that “aha!” moment when we all first used a IEnumerable<T> extension method or when we hooked into a database using the same syntax.  Using Expression Trees is going to increase the readability of our code and offer a lot of functionality that was previously restricted to badly performing but easy to write reflection and well performing and hard to write IL generation.

*Why doesn’t Composability show up in the spellchecker?