Just A Summary : Fluent Interfaces http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces.rss en-us 40 Piers Cawley Practices Punditry Trackback from The Infotainment Telesector: Fluent Interfaces, aka Method Chaining on Fluent Interfaces <p>A few folks have been talking about &#8220;fluent interfaces&#8221;. I first saw this pattern (and it&#8217;s Smalltalk roots) described as &#8220;method chaining&#8221; in Hibernate In Action: <strong>Method chaining</strong> is a programming style supported by ma&#8230;..</p> Thu, 29 Dec 2005 14:46:22 -0600 urn:uuid:adcd639e-e7f9-4169-b7d0-bc6fa933f8e3 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#trackback-3 Comment on Fluent Interfaces by Joe Reddy swcses.eponym.com <p>I like your 7 points. I too have been practicing the idea of &#8220;fluent&#8221; interfaces for awhile without giving it a name. Although I don&#8217;t use Visual Basic much anymore, the English-like syntax was very attractive.</p> <p>I espcially ike the &#8220;Just Principle&#8221; I think I will use that when explaining using an english-like appraoch to interfaces.</p> <p>Take care,</p> <p>Joe <a href="http://swcses.eponym.com" rel="nofollow">http://swcses.eponym.com</a></p> Wed, 10 Jan 2007 10:43:31 -0600 urn:uuid:b57a6df4-ad1c-463c-beea-58a2360e2857 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-68 Comment on Fluent Interfaces by Bob <p>The fluent interface has been around for more than 20 years most likely from SmallTalk. This was considered cool in the early 1990s and fell out of favor for the same reasons that a complicated single line for loop with only a semi-colon body is now avoided.</p> <pre> From: <a href="mailto:j...@hplsla.HP.COM" rel="nofollow">j...@hplsla.HP.COM</a> (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: ******** Message-ID: &lt;<a href="mailto:6590264@hplsla.HP.COM" rel="nofollow">6590264@hplsla.HP.COM</a>&gt; Date: 26 Sep 89 18:54:01 GMT References: &lt;<a href="mailto:3863@helios.ee.lbl.gov" rel="nofollow">3863@helios.ee.lbl.gov</a>&gt; Organization: HP Lake Stevens, WA In any case, assignment to this is considered obsolete with 2.0, though still allowed for backward compatibility. Expect C++ compilers to not support assignment to this in the future. Not allowing assignment to this can allow more efficient compilers. For example, consider the following oop style of chaining commands: --------------------------------------------------------------------------- class something { public: something&#38; dothis(); something&#38; dothat(); something&#38; doanotherthing(); }; something&#38; something::dothis(){ /* do something, then */ return *this;} something&#38; something::dothat(){ /* do something, then */ return *this;} something&#38; something::doanotherthing(){ /* do something, then */ return *this;} main() { something I; I.dothis().dothat().doanotherthing(); } </pre> Thu, 15 Mar 2007 12:04:03 -0500 urn:uuid:ef85accf-983a-41c2-aae7-3e1920b4cfcf http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-139 Comment on Fluent Interfaces by Piers Cawley <p>I can&#8217;t honestly say I&#8217;m advocating doing that. Even in Smalltalk where a method defaults to returning self unless you tell it to return something else, you&#8217;d use a cascade:</p> <div class="typocode"><pre><code class="typocode_default ">SomeClass new doThis; doThat; doTheOther</code></pre></div> <p>is rather neater than:</p> <div class="typocode"><pre><code class="typocode_default ">|anObject| anObject := SomeClass new. anObject doThis. anObject doThat. anObject doTheOther</code></pre></div> <p>Interestingly, the Rails framework now defines a <code>returning</code> method which can be abused to give something like Smalltalk cascades:</p> <div class="typocode"><pre><code class="typocode_default ">returning(SomeClass.new) do |o| o.do_this o.do_that o.do_the_other end</code></pre></div> <p>but it does seem rather clumsy next to Smalltalk&#8217;s syntax.</p> Thu, 15 Mar 2007 12:36:26 -0500 urn:uuid:81a3f6aa-4109-430d-8e92-2aa1d69b5df1 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-140 Comment on Fluent Interfaces by Bob <p>This C++ practice of chaining together member function calls fell out of favor because writing the code using traditional means was about the same number of lines of code without hiding the implementation details. It also fell out of favor because it promotes writing code that does not do error handling for individual member functions called (via a large try/catch block around all of the calls).</p> <p>Code like the following should be avoided by having an AddLineItem(string, quantity) method for BillingRecord</p> <div class="typocode"><pre><code class="typocode_default ">BillingRecord r = new BillingRecord() LineItem i = new LineItem(&quot;Apples&quot;, 10) r.AddLineItem(i) LineItem j = new LineItem(&quot;Pears&quot;, 10) r.AddLineItem(j) LineItem k = new LineItem(&quot;Oranges&quot;, 10) r.AddLineItem(k)</code></pre></div> Thu, 15 Mar 2007 14:24:15 -0500 urn:uuid:13351370-19a6-456e-8805-3d7ab24728cd http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-143 Comment on Fluent Interfaces by Piers Cawley <p>While I take your point, your comments seem to be directed more at Martin Fowler&#8217;s original post than this one.</p> <p>You seem to think that exposing implementation details is a good thing, which strikes me as odd; I&#8217;ve always taken the view that you leave the implementation to the object and concern yourself with coming up with message protocols that are relevant to the task you want the object to perform.</p> Thu, 15 Mar 2007 15:32:36 -0500 urn:uuid:80a117fd-041d-4f44-ad74-9a1f2b1252b2 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-145 Comment on Fluent Interfaces by dan ros <p>good once</p> Sat, 18 Aug 2007 03:46:27 -0500 urn:uuid:17eda456-7e79-44dc-bac7-c0780f91e6d5 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-548 Comment on Fluent Interfaces by Stehan Schmidt <p>The blog and the comments mix different concepts. They miss the point. This reminds me of David Weinbergs &#8220;There is no problem.&#8221; The blog post mainly talks about DSLs and the comments about cascading calls. upTo and belongsTo are not examples for fluent interfaces. Fluent interfaces are more than this. They are semantically on a higher level. Fluent interfaces use clever cascading calls with clever return types to create human readable constraint or object descriptions.</p> <p>Quaere is an example of an fluent interface.</p> <p><a href="http://andersnoras.com" rel="nofollow">http://andersnoras.com</a></p> <p>Peace -stephan</p> <p>&#8212; Stephan Schmidt :: <a href="mailto:stephan@reposita.org" rel="nofollow">stephan@reposita.org</a> Reposita Open Source &#8211; Monitor your software development <a href="http://www.reposita.org" rel="nofollow">http://www.reposita.org</a> Blog at <a href="http://stephan.reposita.org" rel="nofollow">http://stephan.reposita.org</a> &#8211; No signal. No noise.</p> Sun, 30 Sep 2007 03:09:46 -0500 urn:uuid:cf48c040-d805-4df3-b3c8-82335efae3a5 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-729 Comment on Fluent Interfaces by Piers Cawley <p>Looking at Quaere, I find it hard to think of any <span class="caps">API</span> that requires the user to pass &#8216;executable&#8217; string arguments as being particularly fluent.</p> <p>You seem to be under the impression that interface fluency is something that only applies to a particular domain, and in that, I think you&#8217;re flat out wrong.</p> <p>Fluent interfaces can crop up anywhere. The <code>(1 upTo: 10) do: [ ... ]</code> example I gave from Smalltalk exhibits <em>exactly</em> what you claim is the distinguishing feature of a fluent interface. It&#8217;s an example of a cascading call that makes use of a clever return type (a message sent to a number builds a range object) to build a human readable loop.</p> <p>Admittedly, it&#8217;s not as tiresomely clever as some fluent interfaces can be, but denying that it&#8217;s fluent simply because it doesn&#8217;t fit your model of where fluent interfaces should be used isn&#8217;t really on either.</p> <p>I contend that fluency is a condition that <em>all</em> api designers should aspire to, whatever their domain. Smalltalk uses fluency to solve problems that other languages address by introducing syntax. Fluently designed classes like Range and the rest of the collection hierarchy, coupled with well thought out constructors on other classes mean that a Smalltalk image is well supplied with expressive, fluent ways of specifying loops and other types of control flow, that the language itself has special syntax for handling control flow.</p> <p>Every time I come back from exploring smalltalk, I find myself wondering about adding ifTrue(&#38;block), ifFalse(&#38;block), ifNil(&#38;block) etc to ruby&#8230;</p> <p>So far, I&#8217;ve resisted.</p> Sun, 30 Sep 2007 14:03:59 -0500 urn:uuid:5a700413-0604-448b-9b66-994978393d8a http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-730 Comment on Fluent Interfaces by Neil Martin <p>I have referenced this post in my post : <a href="http://neildmartin.spaces.live.com/blog/cns" rel="nofollow">http://neildmartin.spaces.live.com/blog/cns<img src="7E48D388265D63E1" alt="" />468.entry</a><img src="7E48D388265D63E1" alt="" />468.entry</p> <p>I love method Chaining and fluency in its place it is very powerful and makes <span class="caps">API</span>&#8217;s easy to consume however it isnt the end of the story and can be used very badly i liked your 7 points.</p> Wed, 05 Dec 2007 04:01:49 -0600 urn:uuid:ac3f99a1-9611-46d1-ac10-f8f07f2965d7 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-997 Comment on Fluent Interfaces by Mert Nuhoglu <p>I think, Stephan Schmidt&#8217;s blog post on this topic is the way to go: <a href="http://stephan.reposita.org/archives/2007/10/10/fluent-interface-and-reflection-for-object-building-in-java/" rel="nofollow">Fluent Interface and Reflection for Object Building in Java</a></p> Sat, 17 May 2008 05:49:32 -0500 urn:uuid:8a817e8b-7296-43c5-97d3-c448fd778ba8 http://www.bofh.org.uk/articles/2005/12/21/fluent-interfaces#comment-1408