Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent interface in Delphi
    text
    copied!<p>What are the pros and cons in using <a href="http://en.wikipedia.org/wiki/Fluent_interface" rel="nofollow noreferrer">fluent interfaces</a> in Delphi?</p> <p>Fluent interfaces are supposed to increase the readability, but I'm a bit skeptical to have <em>one</em> long LOC that contains a lot of chained methods.</p> <p>Are there any compiler issues?<br> Are there any debugging issues?<br> Are there any runtime/error handling issues?</p> <p>Fluent interfaces are used in e.g. <a href="http://docwiki.embarcadero.com/VCL/en/SysUtils.TStringBuilder" rel="nofollow noreferrer">TStringBuilder</a>, <a href="http://code.google.com/p/delphihtmlwriter/" rel="nofollow noreferrer">THTMLWriter</a> and <a href="http://17slon.com/blogs/gabr/2009/04/fluent-xml-1.html" rel="nofollow noreferrer">TGpFluentXMLBuilder</a>. </p> <hr> <p><strong>Updated:</strong><br> David Heffernan asked which issues I was concerned about. I've been given this some thought, and the overall issue is the difference between "explicitly specifying how it's done" versus "letting the compiler decide how it's done".</p> <p>AFAICS, there is no documentation on how chained methods actually is handled by the compiler, nor any specification on how the compiler should treat chained methods.</p> <p>In <a href="http://docwiki.embarcadero.com/RADStudio/en/Program_Control" rel="nofollow noreferrer">this article</a> we can read about how the compiler adds two additional var-parameters to methods declared as functions, and that the standard calling convention puts three params in the register and the next ones on the stack. A "fluent function method" with 2 params will therefor use the stack, whereas an "ordinary procedure method" with 2 params only uses the register.</p> <p>We also know that the compiler does some magic to optimize the binary (e.g. <a href="https://stackoverflow.com/questions/3250827/initialise-string-function-result/3251381#3251381">string as function result</a>, <a href="https://stackoverflow.com/questions/3054526/delphi-compiler-directive-to-evaluate-arguments-in-reverse/3054899#3054899">evaluation order</a>, <a href="https://stackoverflow.com/questions/5044981/loosen-local-procedure-function-assigned-to-procedure-variable-restriction-grac">ref to local proc</a>), but sometimes with surprising side effects for the programmer.</p> <p>So the fact that the memory/stack/register-management is more complex and the fact that compiler could do some magic with unintentional side effects, is pretty smelly to me. Hence the question.</p> <p>After I've read the answers (very good ones), my concern is strongly reduced but my preference is still the same :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload