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


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

Re: Writing type conversions

Started byJohn G Harris <niam@jghnorth.org.uk.invalid>
First post2020-11-23 18:34 +0000
Last post2020-11-24 09:56 +0100
Articles 13 — 5 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: Writing type conversions John G Harris <niam@jghnorth.org.uk.invalid> - 2020-11-23 18:34 +0000
    Re: Writing type conversions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-11-23 20:11 +0000
      Re: Writing type conversions Arno Welzel <usenet@arnowelzel.de> - 2020-11-24 09:49 +0100
        Re: Writing type conversions Silvio <silvio@internet.com> - 2020-11-24 10:58 +0100
          Re: Writing type conversions Arno Welzel <usenet@arnowelzel.de> - 2020-11-24 18:56 +0100
        Re: Writing type conversions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-11-24 11:48 +0000
      Re: Writing type conversions John G Harris <niam@jghnorth.org.uk.invalid> - 2020-11-24 18:34 +0000
        Re: Writing type conversions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-11-24 21:56 +0000
          Re: Writing type conversions John G Harris <niam@jghnorth.org.uk.invalid> - 2020-11-25 15:32 +0000
            Re: Writing type conversions Julio Di Egidio <julio@diegidio.name> - 2020-11-25 10:29 -0800
            Re: Writing type conversions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-11-25 19:58 +0000
              Re: Writing type conversions Julio Di Egidio <julio@diegidio.name> - 2020-11-26 16:47 -0800
    Re: Writing type conversions Arno Welzel <usenet@arnowelzel.de> - 2020-11-24 09:56 +0100

#39178 — Re: Writing type conversions

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2020-11-23 18:34 +0000
SubjectRe: Writing type conversions
Message-ID<g50orfttg03lruckcm11jkj0anf2h01ern@4ax.com>
On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
wrote:

  <snip>
>  However, in the Java world many deemed »+ ""« to be bad
>  style because it does not express the intent of the
>  programmer to convert something to a string but would
>  mystify the reader. I do not share this point of view,
>  however, because I think the intent of »+ ""« is clear.

  "" + x + 1
makes it more obvious.

  John

[toc] | [next] | [standalone]


#39179

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-11-23 20:11 +0000
Message-ID<87eekjrfz0.fsf@bsb.me.uk>
In reply to#39178
John G Harris <niam@jghnorth.org.uk.invalid> writes:

> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
> wrote:
>
>   <snip>
>>  However, in the Java world many deemed »+ ""« to be bad
>>  style because it does not express the intent of the
>>  programmer to convert something to a string but would
>>  mystify the reader. I do not share this point of view,
>>  however, because I think the intent of »+ ""« is clear.
>
>   "" + x + 1
> makes it more obvious.

Am I missing a joke?  (It does something else altogether.)

-- 
Ben.

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


#39180

FromArno Welzel <usenet@arnowelzel.de>
Date2020-11-24 09:49 +0100
Message-ID<i23vpaFkcrpU1@mid.individual.net>
In reply to#39179
Ben Bacarisse:

> John G Harris <niam@jghnorth.org.uk.invalid> writes:
> 
>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>> wrote:
>>
>>   <snip>
>>>  However, in the Java world many deemed »+ ""« to be bad
>>>  style because it does not express the intent of the
>>>  programmer to convert something to a string but would
>>>  mystify the reader. I do not share this point of view,
>>>  however, because I think the intent of »+ ""« is clear.
>>
>>   "" + x + 1
>> makes it more obvious.
> 
> Am I missing a joke?  (It does something else altogether.)

No - this is not a joke. Since JavaScript is loosely typed, the
expression "" + x + 1 is totally valid and will always create a string
as result - so having "" in the beginning makes this more visible.
However, the result value depends on wether x is a string not not.

x = "4";
y = "" + x + 2;

y will be the string "42"

x = 4;
y = "" + x + 2;

y will be the string "6"

x = 4;
y = x + 2;

y will be the number 6


-- 
Arno Welzel
https://arnowelzel.de

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


#39182

FromSilvio <silvio@internet.com>
Date2020-11-24 10:58 +0100
Message-ID<5fbcd957$0$16808$e4fe514c@newszilla.xs4all.nl>
In reply to#39180
On 11/24/20 9:49 AM, Arno Welzel wrote:
> Ben Bacarisse:
> 
>> John G Harris <niam@jghnorth.org.uk.invalid> writes:
>>
>>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>>> wrote:
>>>
>>>    <snip>
>>>>   However, in the Java world many deemed »+ ""« to be bad
>>>>   style because it does not express the intent of the
>>>>   programmer to convert something to a string but would
>>>>   mystify the reader. I do not share this point of view,
>>>>   however, because I think the intent of »+ ""« is clear.
>>>
>>>    "" + x + 1
>>> makes it more obvious.
>>
>> Am I missing a joke?  (It does something else altogether.)
> 
> No - this is not a joke. Since JavaScript is loosely typed, the
> expression "" + x + 1 is totally valid and will always create a string
> as result - so having "" in the beginning makes this more visible.
> However, the result value depends on wether x is a string not not.
> 
> x = "4";
> y = "" + x + 2;
> 
> y will be the string "42"
> 
> x = 4;
> y = "" + x + 2;
> 
> y will be the string "6"

Think again...

> 
> x = 4;
> y = x + 2;
> 
> y will be the number 6
> 
> 

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


#39186

FromArno Welzel <usenet@arnowelzel.de>
Date2020-11-24 18:56 +0100
Message-ID<i24vqdFqidcU1@mid.individual.net>
In reply to#39182
Silvio:

> On 11/24/20 9:49 AM, Arno Welzel wrote:
>> Ben Bacarisse:
>>
>>> John G Harris <niam@jghnorth.org.uk.invalid> writes:
>>>
>>>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>>>> wrote:
>>>>
>>>>    <snip>
>>>>>   However, in the Java world many deemed »+ ""« to be bad
>>>>>   style because it does not express the intent of the
>>>>>   programmer to convert something to a string but would
>>>>>   mystify the reader. I do not share this point of view,
>>>>>   however, because I think the intent of »+ ""« is clear.
>>>>
>>>>    "" + x + 1
>>>> makes it more obvious.
>>>
>>> Am I missing a joke?  (It does something else altogether.)
>>
>> No - this is not a joke. Since JavaScript is loosely typed, the
>> expression "" + x + 1 is totally valid and will always create a string
>> as result - so having "" in the beginning makes this more visible.
>> However, the result value depends on wether x is a string not not.
>>
>> x = "4";
>> y = "" + x + 2;
>>
>> y will be the string "42"
>>
>> x = 4;
>> y = "" + x + 2;
>>
>> y will be the string "6"
> 
> Think again...

I stand corrected - the result is "42" in both cases of course.



-- 
Arno Welzel
https://arnowelzel.de

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


#39185

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-11-24 11:48 +0000
Message-ID<87ft4zasc9.fsf@bsb.me.uk>
In reply to#39180
Arno Welzel <usenet@arnowelzel.de> writes:

> Ben Bacarisse:
>
>> John G Harris <niam@jghnorth.org.uk.invalid> writes:
>> 
>>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>>> wrote:
>>>
>>>   <snip>
>>>>  However, in the Java world many deemed »+ ""« to be bad
>>>>  style because it does not express the intent of the
>>>>  programmer to convert something to a string but would
>>>>  mystify the reader. I do not share this point of view,
>>>>  however, because I think the intent of »+ ""« is clear.
>>>
>>>   "" + x + 1
>>> makes it more obvious.
>> 
>> Am I missing a joke?  (It does something else altogether.)
>
> No - this is not a joke. Since JavaScript is loosely typed, the
> expression "" + x + 1 is totally valid

I know.  I was asking if I was missing a joke in the suggestion that ""
+ x + 1 is a more obvious way to write +x + 1 + "".  They do different
things.

-- 
Ben.

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


#39187

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2020-11-24 18:34 +0000
Message-ID<1gkqrft95t0ke57qb3qfo27mmtuqdbqh8b@4ax.com>
In reply to#39179
On Mon, 23 Nov 2020 20:11:15 +0000, Ben Bacarisse
<ben.usenet@bsb.me.uk> wrote:

>John G Harris <niam@jghnorth.org.uk.invalid> writes:
>
>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>> wrote:
>>
>>   <snip>
>>>  However, in the Java world many deemed »+ ""« to be bad
>>>  style because it does not express the intent of the
>>>  programmer to convert something to a string but would
>>>  mystify the reader. I do not share this point of view,
>>>  however, because I think the intent of »+ ""« is clear.
>>
>>   "" + x + 1
>> makes it more obvious.
>
>Am I missing a joke?  (It does something else altogether.)

No, though I wasn't sure what the original intention was supposed to
be.

  var x = 2;
  "" + x + 4       // "24"
and
  var x = 2;
  "" + (x + 4)     // "6"

both make the intention clear. Other variants belong in an obscuration
contest, and nowhere else.

  John

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


#39189

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-11-24 21:56 +0000
Message-ID<87mtz6a06s.fsf@bsb.me.uk>
In reply to#39187
John G Harris <niam@jghnorth.org.uk.invalid> writes:

> On Mon, 23 Nov 2020 20:11:15 +0000, Ben Bacarisse
> <ben.usenet@bsb.me.uk> wrote:
>
>>John G Harris <niam@jghnorth.org.uk.invalid> writes:
>>
>>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>>> wrote:
>>>
>>>   <snip>
>>>>  However, in the Java world many deemed »+ ""« to be bad
>>>>  style because it does not express the intent of the
>>>>  programmer to convert something to a string but would
>>>>  mystify the reader. I do not share this point of view,
>>>>  however, because I think the intent of »+ ""« is clear.
>>>
>>>   "" + x + 1
>>> makes it more obvious.
>>
>>Am I missing a joke?  (It does something else altogether.)
>
> No, though I wasn't sure what the original intention was supposed to
> be.
>
>   var x = 2;
>   "" + x + 4       // "24"
> and
>   var x = 2;
>   "" + (x + 4)     // "6"
>
> both make the intention clear. Other variants belong in an obscuration
> contest, and nowhere else.

But since this is not what the code was doing, that seems to be a
premature judgement.  How should the original be written so as to avoid
your opprobrium?

-- 
Ben.

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


#39193

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2020-11-25 15:32 +0000
Message-ID<u3usrfhp2olemgck4460la9iqod77rinm1@4ax.com>
In reply to#39189
On Tue, 24 Nov 2020 21:56:27 +0000, Ben Bacarisse
<ben.usenet@bsb.me.uk> wrote:

>John G Harris <niam@jghnorth.org.uk.invalid> writes:
>
>> On Mon, 23 Nov 2020 20:11:15 +0000, Ben Bacarisse
>> <ben.usenet@bsb.me.uk> wrote:
>>
>>>John G Harris <niam@jghnorth.org.uk.invalid> writes:
>>>
>>>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>>>> wrote:
>>>>
>>>>   <snip>
>>>>>  However, in the Java world many deemed »+ ""« to be bad
>>>>>  style because it does not express the intent of the
>>>>>  programmer to convert something to a string but would
>>>>>  mystify the reader. I do not share this point of view,
>>>>>  however, because I think the intent of »+ ""« is clear.
>>>>
>>>>   "" + x + 1
>>>> makes it more obvious.
>>>
>>>Am I missing a joke?  (It does something else altogether.)
>>
>> No, though I wasn't sure what the original intention was supposed to
>> be.
>>
>>   var x = 2;
>>   "" + x + 4       // "24"
>> and
>>   var x = 2;
>>   "" + (x + 4)     // "6"
>>
>> both make the intention clear. Other variants belong in an obscuration
>> contest, and nowhere else.
>
>But since this is not what the code was doing, that seems to be a
>premature judgement.  How should the original be written so as to avoid
>your opprobrium?

All right, here are all the gory details :

  var x = "2";
  "" + ((+x) + 4)  // "6"

This separates the various kinds of action being performed. If +x is
not a common idiom in the development team then the rather cumbersome
Number(x) replaces it.

As Julio will tell you, the first job of a source file is to be
readable. Without that there is little chance of it being correct and
maintainable.

  John

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


#39194

FromJulio Di Egidio <julio@diegidio.name>
Date2020-11-25 10:29 -0800
Message-ID<7559f324-72ac-4795-b13a-30398c3b7ffbn@googlegroups.com>
In reply to#39193
On Wednesday, 25 November 2020 at 16:32:26 UTC+1, John G Harris wrote:

> All right, here are all the gory details : 

It's called _automatic type conversion_ (if I remember correctly), and it
happens ubiquitously in JS, JS being a loosely typed dynamic language.
(As opposed to e.g. Python, which is a strictly typed dynamic language,
where e.g. 1+"1" is an error.)  One just has to read the docs, where the
behaviour of operators is given, e.g. the fact that if an operand of addition
is string, the other operand is automatically converted to string if it is not
one already, then concatenation is performed and the result returned.

> var x = "2"; 
> "" + ((+x) + 4) // "6" 
> 
> This separates the various kinds of action being performed. If +x is 
> not a common idiom in the development team then the rather cumbersome 
> Number(x) replaces it. 
> 
> As Julio will tell you, the first job of a source file is to be 
> readable. Without that there is little chance of it being correct and 
> maintainable. 

Sure, but readable is not the same as writable, yet you keep counting
keystrokes...  And, Number rather supersedes parseInt for converting
from string.  Anyway, the piece of code you show above indeed is a
nice condensate of common JS idioms for (explicit) type conversion,
but is certainly nothing I would expect to find in production: should be
x=2 to begin with...

Julio

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


#39195

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2020-11-25 19:58 +0000
Message-ID<87pn416wf4.fsf@bsb.me.uk>
In reply to#39193
John G Harris <niam@jghnorth.org.uk.invalid> writes:

> On Tue, 24 Nov 2020 21:56:27 +0000, Ben Bacarisse
> <ben.usenet@bsb.me.uk> wrote:
>
>>John G Harris <niam@jghnorth.org.uk.invalid> writes:
>>
>>> On Mon, 23 Nov 2020 20:11:15 +0000, Ben Bacarisse
>>> <ben.usenet@bsb.me.uk> wrote:
>>>
>>>>John G Harris <niam@jghnorth.org.uk.invalid> writes:
>>>>
>>>>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
>>>>> wrote:
>>>>>
>>>>>   <snip>
>>>>>>  However, in the Java world many deemed »+ ""« to be bad
>>>>>>  style because it does not express the intent of the
>>>>>>  programmer to convert something to a string but would
>>>>>>  mystify the reader. I do not share this point of view,
>>>>>>  however, because I think the intent of »+ ""« is clear.
>>>>>
>>>>>   "" + x + 1
>>>>> makes it more obvious.
>>>>
>>>>Am I missing a joke?  (It does something else altogether.)
>>>
>>> No, though I wasn't sure what the original intention was supposed to
>>> be.
>>>
>>>   var x = 2;
>>>   "" + x + 4       // "24"
>>> and
>>>   var x = 2;
>>>   "" + (x + 4)     // "6"
>>>
>>> both make the intention clear. Other variants belong in an obscuration
>>> contest, and nowhere else.
>>
>>But since this is not what the code was doing, that seems to be a
>>premature judgement.  How should the original be written so as to avoid
>>your opprobrium?
>
> All right, here are all the gory details :
>
>   var x = "2";
>   "" + ((+x) + 4)  // "6"
>
> This separates the various kinds of action being performed. If +x is
> not a common idiom in the development team then the rather cumbersome
> Number(x) replaces it.

Thanks.  Obviously we have very different views of what looks obscure.
I like the left-to-right clarity of

  +x + 4 + ""

but I won't argue it.

-- 
Ben.

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


#39203

FromJulio Di Egidio <julio@diegidio.name>
Date2020-11-26 16:47 -0800
Message-ID<23abb9ca-60a8-47b2-89f6-52d534cc2a25n@googlegroups.com>
In reply to#39195
On Wednesday, 25 November 2020 at 20:58:33 UTC+1, Ben Bacarisse wrote:
> John G Harris <ni...@jghnorth.org.uk.invalid> writes: 

> > All right, here are all the gory details : 
> > 
> > var x = "2"; 
> > "" + ((+x) + 4) // "6" 
> > 
> > This separates the various kinds of action being performed. If +x is 
> > not a common idiom in the development team then the rather cumbersome 
> > Number(x) replaces it.
> Thanks. Obviously we have very different views of what looks obscure. 
> I like the left-to-right clarity of 
> 
> +x + 4 + "" 
> 
> but I won't argue it. 

That "left-to-right clarity" in fact relies on operator associativity,
and there is nothing trivial about that: read, error prone.  Not per
chance the usual idiom is ""+the_rest.  Which is also more similar
to !!the_rest and +the_rest.

This is the canonical way one would write the (unfortunate) code
above, conversion first and no unneeded parentheses: "" + (+t + 4)

HTH,

Julio

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


#39181

FromArno Welzel <usenet@arnowelzel.de>
Date2020-11-24 09:56 +0100
Message-ID<i2405fFkf3eU1@mid.individual.net>
In reply to#39178
John G Harris:

> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram)
> wrote:
> 
>   <snip>
>>  However, in the Java world many deemed »+ ""« to be bad
>>  style because it does not express the intent of the
>>  programmer to convert something to a string but would
>>  mystify the reader. I do not share this point of view,
>>  however, because I think the intent of »+ ""« is clear.
> 
>   "" + x + 1
> makes it more obvious.

But this may lead to the wrong result.

Example:

x = "4";
y = + x + 2;

Result: "6"

x = "4";
y = "" + x + 2;

Result: "42"

In general: if it is important to get a value as number, you should
always create a numeric value from it using Number() and never rely on
the expression itself.

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [standalone]


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


csiph-web