Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #124089 > unrolled thread

Re: Statement-Continuation Rule

Started byMild Shock <janburse@fastmail.fm>
First post2024-02-22 11:37 +0100
Last post2024-02-23 11:55 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Statement-Continuation Rule Mild Shock <janburse@fastmail.fm> - 2024-02-22 11:37 +0100
    Re: Statement-Continuation Rule Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-02-22 19:36 +0000
      Re: Statement-Continuation Rule Mild Shock <janburse@fastmail.fm> - 2024-02-23 00:49 +0100
        Re: Statement-Continuation Rule Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-02-23 01:40 +0000
          Re: Statement-Continuation Rule John Harris <niam@jghnorth.org.uk.invalid> - 2024-02-23 11:55 +0000

#124089 — Re: Statement-Continuation Rule

FromMild Shock <janburse@fastmail.fm>
Date2024-02-22 11:37 +0100
SubjectRe: Statement-Continuation Rule
Message-ID<ur7860$f3dd$1@solani.org>
Looks like an instance of method chaining style
https://en.wikipedia.org/wiki/Method_chaining

It is heralded as something powerful:

Method chaining is a powerful programming pattern that allows you to 
call multiple methods on an object in a single line of code. It enhances 
code readability and conciseness by eliminating the need for 
intermediate variables or repeated method calls. In this blog post, 
we'll explore method chaining in Python, explain it using a simple 
example, discuss its use cases, and conclude with its benefits.
https://nikhilakki.in/understanding-method-chaining-in-python

But I have my doubts. Its also related to so called
Fluent Interfaces. When you design APIs so that they
support method chaining:
https://en.wikipedia.org/wiki/Fluent_interface

So called builders often exhibit a fluent interfrace. I
recently had a revelation, that many builders
are rather cheaters, for example I thought the appropriate
thing to do would be:

builder = builder.header(key, value);

So the fluent interface would give me a new version of the
build, with each method chaining call. Just like the replaceAll
gives a new string. But this is often not the case,

it would require that all headers are copied somehow. So we
find in the implementation of header() that it just returns
this, and the method chaining works with a side effect:

public HttpRequestBuilderImpl header(String name, String value) {
         checkNameAndValue(name, value);
         headersBuilder.addHeader(name, value);
         return this;
     }

So method chaining might not always satisfied the same expectations
about being a more "functional" approach.

Lawrence D'Oliveiro schrieb:
> Is this valid code?
> 
>      function escape_html(s)
>        {
>          return s
>              .replaceAll("&", "&amp;") /* always do first, rest can be in any order */
>              .replaceAll("\"", "&quot;")
>              .replaceAll("<", "&lt;")
>              .replaceAll(">", "&gt;")
>              .replaceAll("\t", "&#9;")
>              .replaceAll("\n", "&#10;")
>        } /*escape_html*/
> 

[toc] | [next] | [standalone]


#124275

FromLawrence D'Oliveiro <ldo@nz.invalid>
Date2024-02-22 19:36 +0000
Message-ID<ur87of$1rr1$1@dont-email.me>
In reply to#124089
On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:

> But I have my doubts.

Show us your non-method-chained-style version, then.

[toc] | [prev] | [next] | [standalone]


#124276

FromMild Shock <janburse@fastmail.fm>
Date2024-02-23 00:49 +0100
Message-ID<ur8mhl$ftfd$1@solani.org>
In reply to#124275
No syntactical doubts, only semantical doubts:

 > So method chaining might not always satisfied the
 > same expectations about being a more "functional" approach.

Guido von Rossum suggest to not always use method chaining:

https://mail.python.org/pipermail/python-dev/2003-October/038855.html

Lawrence D'Oliveiro schrieb:
> On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:
> 
>> But I have my doubts.
> 
> Show us your non-method-chained-style version, then.
> 

[toc] | [prev] | [next] | [standalone]


#124277

FromLawrence D'Oliveiro <ldo@nz.invalid>
Date2024-02-23 01:40 +0000
Message-ID<ur8t35$6ap6$1@dont-email.me>
In reply to#124276
On Fri, 23 Feb 2024 00:49:08 +0100, Mild Shock wrote:

> Lawrence D'Oliveiro schrieb:
>
>> On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:
>> 
>>> But I have my doubts.
>> 
>> Show us your non-method-chained-style version, then.
>>
> Guido von Rossum suggest to not always use method chaining:

Which is not really answering my question, is it?

[toc] | [prev] | [next] | [standalone]


#124278

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2024-02-23 11:55 +0000
Message-ID<ura132$gs2i$1@dont-email.me>
In reply to#124277
On 23/02/2024 01:40, Lawrence D'Oliveiro wrote:
> On Fri, 23 Feb 2024 00:49:08 +0100, Mild Shock wrote:
> 
>> Lawrence D'Oliveiro schrieb:
>>
>>> On Thu, 22 Feb 2024 11:37:52 +0100, Mild Shock wrote:
   <snip>
>> Guido von Rossum suggest to not always use method chaining:
> 
> Which is not really answering my question, is it?

In some scenarios it makes sense to allow chaining.
For instance when building the answer to a query where the parts to be 
included depend on circumstances. As in x.a().b().e().g();

In other scenarios it just makes things confusing for anyone reading the 
code, including the writer.

In other words it depends on design judgement, something that disturbs 
people who prefer a 300 page book of rules.

   John

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web