My best code ever

Yesterday I wrote the best piece of code I’ve ever written… I was so full of myself 🙂

Incidently it’s all thanks to Functional Programming and the insights gained from reading on Lisp, Ruby and LINQ.
It would have been shorter and probably prettier in Ruby or Lisp or even C# 3.0, but it works beautifuly nevertheless.

I’m stuck with Sharepoint as the database and had to build layers and UI around it.
If you ever wrote rich business model with data access layer around web-services you know exactly how it felt.

Comes along… a New Requirement.
We need to provide access to some of application data via web-serices.
No problem, I know Contract-First, I can do stuff like that with my eyes closed.
My boss has an idea to cache the results returned from Sharepoint to avoid the extra web-service call.
No problem – cache it is, with background update and a query interface.
Here’s where the good part comes in – I implement the interface as taking a Predicate<T>.
Of course it wasn’t ground-breaking in any way if you know .NET 2.0 –
lots of collections implement .FindAll(Predicate<T>), but it was bold and new
(and I have a mental chuckle imagining an Average Developer looking at it later on).
It also uses worker pool for parrallel-processing the search request (another chukle).

Comes along… a Refined New Requirement.
My web-service has to support search functionality and handle a Complex Criteria,
somethig like “gimme all A where A.Name or A.Alias are like “xxx” and A.Status is not in (Z,Y)”.
The only time I’ve seen a web-service supporting a query like that was Sharepoint itself.
I’m not keen on introducing an expression tree into web-services,
especially that I’m implementing both ends of it, but a requirement is a requirement.

Enter Templates Collection…
The other good idea was to use templates as the way to supply search criteria.
The idea of templates comes from Hibernate, where criteria can be expressed using an instance of the resulting class.
Collection of templates allows one to express almost everything an expression tree could do.
The criteria above becomes a collection: { new A(Name=”xxx”,Status=X), new A(Name=”xxx”,Status=W)}

The keen may notice that I’m cheating on 2 accounts – 1st I’m reversing “not in” to “in”
and 2nd – I’m using Name attribute to comapre Alias as well.
I find the trade-offs acceptable considering I was done with implementation and unit testing
within a few hours of getting the requirement.

And that’s where Functional Programming shines and the decision to use Predicate<T>
as the only parameter to query pays off spectacularly.
I’m processing my templates and turn them into predicates and chain the predicates together to pass to the search function.

It may not sound like much, but boy is it short, concise and to the point! The whole conversion from templates to a predicate takes about half a page of code.
I declare it my best code ever.

I’ll see if I can turn my code into a meaningful example to post here later.

Advertisements
My best code ever

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s