Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod chaining - why is it a good practice, or not?
    primarykey
    data
    text
    <p><a href="http://en.wikipedia.org/wiki/Method_chaining" rel="noreferrer">Method chaining</a> is the practice of object methods returning the object itself in order for the result to be called for another method. Like this:</p> <pre><code>participant.addSchedule(events[1]).addSchedule(events[2]).setStatus('attending').save() </code></pre> <p>This seems to be considered a good practice, since it produces readable code, or a "fluent interface". However, to me it instead seems to break the object calling notation implied by the object orientation itself - the resulting code does not represent performing actions to the <em>result</em> of a previous method, which is how object oriented code is generally expected to work:</p> <pre><code>participant.getSchedule('monday').saveTo('monnday.file') </code></pre> <p>This difference manages to create two different meanings for the dot-notation of "calling the resulting object": In the context of chaining, the above example would read as saving the <em>participant</em> object, even though the example is in fact intended to save the schedule object received by getSchedule.</p> <p>I understand that the difference here is whether the called method should be expected to return something or not (in which case it would return the called object itself for chaining). But these two cases are not distinguishable from the notation itself, only from the semantics of the methods being called. When method chaining is not used, I can always know that a method call operates on something related to the <em>result</em> of the previous call - with chaining, this assumption breaks, and I have to semantically process the whole chain to understand what the actual object being called really is. For example:</p> <pre><code>participant.attend(event).setNotifications('silent').getSocialStream('twitter').postStatus('Joining '+event.name).follow(event.getSocialId('twitter')) </code></pre> <p>There the last two method calls refer to the result of getSocialStream, whereas the ones before refer to the participant. Maybe it's bad practice to actually write chains where the context changes (is it?), but even then you'll have to constantly check whether dot-chains that look similar are in fact keep within the same context, or only work on the result.</p> <p>To me it appears that while method chaining superficially does produce readable code, overloading the meaning of the dot-notation only results in more confusion. As I don't consider myself a programming guru, I assume the fault is mine. <strong>So: What am I missing? Do I understand method chaining somehow wrong? Are there some cases where method chaining is especially good, or some where it's especially bad?</strong></p> <p>Sidenote: I understand this question could be read as a statement of opinion masked as a question. It, however, isn't - I genuinely want to understand why chaining is considered good practice, and where do I go wrong in thinking it breaks the inherent object-oriented notation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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