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


Groups > comp.lang.javascript > #39193

Re: Writing type conversions

From John G Harris <niam@jghnorth.org.uk.invalid>
Newsgroups comp.lang.javascript
Subject Re: Writing type conversions
Date 2020-11-25 15:32 +0000
Organization A noiseless patient Spider
Message-ID <u3usrfhp2olemgck4460la9iqod77rinm1@4ax.com> (permalink)
References <type-style-20201123132025@ram.dialup.fu-berlin.de> <g50orfttg03lruckcm11jkj0anf2h01ern@4ax.com> <87eekjrfz0.fsf@bsb.me.uk> <1gkqrft95t0ke57qb3qfo27mmtuqdbqh8b@4ax.com> <87mtz6a06s.fsf@bsb.me.uk>

Show all headers | View raw


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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web