Managed World

Techno-babble from yet another babbler RSS 2.0
# Monday, June 02, 2008

The first time I saw some talk about this "movement", it was Martin Fowler discussing "Fluent Interfaces".

An example Martin gives is the idea of time intervals. Here's how one might declare an interval today (in Java):

TimePoint fiveOClock, sixOClock;
...
TimeInterval meetingTime = new TimeInterval(fiveOClock, sixOClock);

Here's how one might to do the same with a fluent interface (anybody who writes Ruby code should recognize this type of programming):

TimeInterval meetingTime = fiveOClock.until(sixOClock);

Like Jeff Atwood, I haven't exactly been a fan of fluent languages in the past (pre-LINQ). I honestly never saw the point in them (and I thought all the object syntax got in the way of what was being done). My mind has definitely changed though. As of late, you could say I'm on a bit of a "fluent languages kick."

LINQ, of course, had a play in this, as did the introduction of lambda expressions. But, it was really seeing two specific libraries that started to get me excited about fluent languages: Moq and NInject. When you compare fluent languages to Regular Expressions and SQL, I can imagine how it's hard to understand the benefit. But when you have a library that reads wonderfully out loud and "fits" an existing problem in order to make it easier to understand, I think it's A Great Thing.

Mocking is a topic I've been pretty passionate about in the past. Even with that passion, there was one thing I didn't like about the libraries back then: breaking changes when refactoring. A lot of mocking frameworks in the pre-.NET 2.0 days were string based. If you wanted to mock an aspect about a method, you put the name of the method in a string and pass that somewhere. Well, the second I renamed that method, BAM!, my unit test would stop passing (of course). Yes, the unit test caught it, but it was still highly annoying. Enter Moq.

Moq leverages lambda expressions to be able to determine what should be mocked. I think the solution Daniel Cazzulino has used is awesome. Let's look at some Moq code to mock some code:

var mock = new Mock<ILoveThisFramework>();

// WOW! No record/reply weirdness?! :)
mock.Expect(framework => framework.ShouldDownload(It.IsAny<Version>()))
    .Callback((Version version) => 
        Console.WriteLine("Someone wanted version {0}!!!", version))
    .Returns(true)
    .AtMostOnce();

I love how this reads. I think the trick behind groking fluent languages is reading them aloud. Go ahead, go back and read that out loud (just ignore the laughing coworkers, we'll get back at them later :P). Not only does this read very well, but if I refactor/rename the "ShouldDownload" method in Visual Studio, the use of that method in my mocking code will be updated as well.

The other fluent language that got me excited was the work Nate Kohari is doing on NInject. Assuming you are familiar with Dependency Injection, the best way to understand NInject is to drive write into code:

class Samurai {
  private IWeapon _weapon;

  [Inject]
  public Samurai(IWeapon weapon) {
    _weapon = weapon;
  }

  public void Attack(string target) {
    _weapon.Hit(target);
  }
}

So I have my injection point defined in my Samurai class. My Samurai is going to need an IWeapon. So how do we bind these two together with NInject?

Bind<IWeapon>().To<Sword>();

Once again, read this out loud. It literally reads like "Bind IWeapon to Sword". I just love that (your mileage may vary :P).

One of the things that bothered me about fluent languages in the past was that people would say "they're so great, they read just like our own language". Well, we've been down that route before, and I definitely wouldn't consider myself a fan of COBOL! So why the love for fluent languages?

One of the problems with spoken languages when it comes to programming is that spoken languages are very ambiguous and allow many meanings for a single word or phrase. In order to avoid that problem, I think COBOL became too obtuse. To be exact enough for a programming language while being solely "written language"-esque, syntax becomes annoyingly long.

The reason I like this new trend of fluent languages in .NET is that it introduces the readability of spoken languages while avoiding the obtrusiveness of languages like COBOL. To me, it's like having my cake and eating it too.

Geek Fact of the Day: Do you know the origin of the term "grok"? The term comes from Robert A. Heinlein's book Stranger In A Strange Land.

Posted in Programming
 #       Comments [6]
Tuesday, June 03, 2008 7:06:41 AM (Pacific Standard Time, UTC-08:00)
Bind<IEdible>.To<Cake>(). mmmm. Moq and NInject look like pretty neat tools. But I am especially pleased to see the developing trend of spelling "K" sounds with a "Q". I like to pretend that I was a contributing influence to this trend.
Tuesday, June 03, 2008 9:27:53 AM (Pacific Standard Time, UTC-08:00)
Marqie-Marq!!! Long time no see (I REALLY REALLY need to make it down to Portland). I'm definitely liking Moq and NInject (if that wasn't painfully obvious from the post, haha). And good point on the Q (which I will back up any claims you made on being the contributing influence for that :P). But, shouldn't your code be Bind<IEdible>.To<Caeq>() then? Or something like that? :P.
Wednesday, June 04, 2008 6:13:39 AM (Pacific Standard Time, UTC-08:00)
I really like the Ninject Bind syntax, but decorating the constructor or properties with attributes is something that I don't like to do.

It couples my domain in a deep way to a subsystem framework, one that should be easy to replace.

But his website is probably the coolest open source website there is!
Wednesday, June 04, 2008 3:14:47 PM (Pacific Standard Time, UTC-08:00)
Actually, that's a great point that I hadn't thought of Ben. It's also one of the exact reasons I avoided attributes in my facet mapping code (see part 2 after this post :P).

That is also the same feedback Krsystof and his team got regarding how they were doing dependency injection in the Managed Extensibility Framework that they showed off at the MVP summit.

Well, great minds think alike I suppose :P.

You're not at Tech Ed down in Orlando, are you? If you are, I need to buy you a beer :).
Sunday, June 08, 2008 11:54:21 AM (Pacific Standard Time, UTC-08:00)
Nice post, and thanks for the praise on the Ninject binding interface. The requirement of [Inject] is actually a common misunderstanding about Ninject... you don't technically need the attribute to mark the members that should be injected. I've heard this complaint a lot lately, so I wrote a blog post on how to dodge the attribute requirement. Check it out and let me know what you think. :) Thanks again!
Sunday, June 08, 2008 1:18:59 PM (Pacific Standard Time, UTC-08:00)
Hey Nate! Flattered you stopped by :). Yeah, I just saw your post in my RSS reader and passed it onto Ben as well via twitter.

Keep up the great work. I love the interface (as if you couldn't already tell :P) :).
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

Contact

Email Me Send mail to the author(s)

Calendar

<October 2008>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

About this site

Jason Olson's thoughts on Programming, Games, Music and Life in General

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

© Copyright 2008
Jason Olson

Sign In
All Content © 2008, Jason Olson
Theme based on 'Business' created by Christoph De Baene (delarou)