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


Groups > comp.lang.javascript > #124089

Re: Statement-Continuation Rule

Path csiph.com!weretis.net!feeder8.news.weretis.net!reader5.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Mild Shock <janburse@fastmail.fm>
Newsgroups comp.lang.javascript
Subject Re: Statement-Continuation Rule
Date Thu, 22 Feb 2024 11:37:52 +0100
Message-ID <ur7860$f3dd$1@solani.org> (permalink)
References <uq1bhn$1lp15$4@dont-email.me>
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Thu, 22 Feb 2024 10:37:52 -0000 (UTC)
Injection-Info solani.org; logging-data="495021"; mail-complaints-to="abuse@news.solani.org"
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.18.1
Cancel-Lock sha1:OOh+lV9LRc4IZNJXzDgoLOoEKYY=
In-Reply-To <uq1bhn$1lp15$4@dont-email.me>
X-User-ID eJwFwQkBACAIBLBKAscXBwX6R3BTMbLnMDXo6kYODCwsu5ReOjTsZ7AI7bLuQ324EX5vuQcSy2GvKa9ofT14FRM=
Xref csiph.com comp.lang.javascript:124089

Show key headers only | View raw


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*/
> 

Back to comp.lang.javascript | Previous | NextNext in thread | Find similar


Thread

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

csiph-web