Fluidic APIs are stupid... NOT!
People rave about Ruby with its fancy schmancy ability to construct internal DSL's (Domain Specific Languages). The oft-touted example is:
20.minutes.ago
In .net we now have 'extension methods', which let us do all sorts of lingustic gymnastics, including 20.minutes.ago
(albeit with a few extra brackets in C#) (phil haack posted an example 6.months.ago)
But the examples are all a little contrived: 20.minutes.ago
happens to form a logical chain where each word passes a value to the following word. But very few phrases are like that.
The real question is: can we turn all of our .net code into fluidic and beautiful prose?
Can we implement, for example, the Shakespeare Programming Language as an internal DSL, in C#?
Here goes...
No, actually, I'm not that brave. I'm going to try something far simpler -- converting a common line of real C# into a more grammatically correct form.
Here's the common line of C#:
if (!this.IsPostBack)
It would translate into plain english as:
"if this is not a post back"
So, subtituting spaces for dots, brackets and so on, a fluid interface might say:
if (this.is.not.a.postback))
...Can it be done?
If I went easy on myself I could change it to:
I could do it the cheaty way, and create a 'is_not_a_postback' method... but that's against the spirit of the thing.
The tricky bit is the word 'not' -- i figured it would create a clone of the object, then use reflection to visit each boolean property, and set the value to it's logical opposite. That's bad though, cause firstly "IsPostBack" is a readonly property -- so we can't reverse it (without accessing internal state... heh heh) and secondly, setting those properties might have unwanted side effects... so forget it.
At this point I could either email some of the world's greatest .net minds and get them to work on the problem around the clock while i head off to sleep. Or I could apply a model-driven architecture approach:
If the facts don't fit the model -- it's time to change the facts.
Yes, I think a more appropriate modern grammatical form of our code would be:
"if this iz a postback... NOT!!" Now, I think I can implement that as:if (this.iz.a.postback.__NOT__)
(again -- with a few extra empty brackets in C#)
iz
,
and
a
can be implemented as what i'd term "empty non-modifier repeater extension methods" or "pass through" methods, both implemented like this:
public static Object iz(this Object obj) { return obj; }
postback is a "synonymous extension method
", a simple wrapper on IsPostBack
:
public static bool postback(this System.Web.UI.Page pg) { return pg.IsPostBack ; }
__NOT__ is "the logical inversion extension method
", embellished with Textile-style __underlining__ and some nice SHOUTING.
public static bool __NOT__(this bool that) { return !that; }Now adjust your syntax highlighting so that braces and dots are the same colour as the background and we have:
if this iz a postback __NOT__
With a few more empty non-modifiers
we can expand our code into the more common English form:
if this iz um like a well um like a youknow postback um thing i_mean __NOT__ mkay yeh
And maybe now i see why Microsoft really added extension methods. It's bound to um, y'know like, encourage more kids to become programmers. NOT.
Next → ← PreviousMy book "Choose Your First Product" is available now.
It gives you 4 easy steps to find and validate a humble product idea.