Fluent Interfaces

I like to embrace Languages Inside of Languages. Jeff says “they are actually an ugly hack, and a terrible substitute for true language integration.”

Jeff totally misses the point. His first example of regular expression does indeed replace one line regular expression with ten lines of objects, methods and named enumerations. Jeff asks if this is progress.

It depends on your goal. What if you make a typo in the one line regular expression? You will never know it until you realize that things don’t work. My goal is to leverage the languages at my disposal to  a maximum benefit.

A fluent interface gets me a certain degree of type safety. This means I can

  • catch errors at compile time instead of debug time.
  • write fewer unit tests to get the same amount of coverage.
  • utilize tools such as intellisense when writing to an API instead of strings.


Sure, LINQ is awesome. VB.NET’s XML Literals are awesome too. Jeff is ignoring what a regular expression fluent interface brings to the table: catching errors in your code.

Next Jeff attacks Subsonic’s fluent interface. It looks a lot to me like what I have used, and loved, in API output form NHibernate Query Generator.

Jeff’s example is great. Lets look at it again

SELECT * from Customers WHERE Country = "USA"
ORDER BY CompanyName

Is approximately the SQL generated by Subsonic’s fluent interface, given this code:

CustomerCollection c = new CustomerCollection();
c.Where(Customer.Columns.Country, "USA");

Jeff says this is harder to write. Jeff is wrong.

By using native C# code instead of embedding a SQL query in some string, we gain

  • type safety
  • use of code completion features like intellisense or intellassist
  • ability to pass around queries and partial queries without modifying them with StringBuilder

That last point should be obvious to anyone who has used DetachedCriteria in Hibernate/NHibernate. If you aren’t sure what I mean, go look at DetachedCriteria or better yet go look at the code that NHQG creates.

So, what do you do as a developer? I encourage you to listen to neither Jeff or me. Make your own decision given the circumstances. I definitely prefer to leverage the compiler and type safety wherever possible. However, I also write Perl when a task at hand can best be accomplished with that tool. Jeff seems to be overlooking some serious benefits to Fluent Interfaces. Please don’t overlook them yourself.