Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Writing type conversions Date: Wed, 25 Nov 2020 19:58:23 +0000 Organization: A noiseless patient Spider Lines: 61 Message-ID: <87pn416wf4.fsf@bsb.me.uk> References: <87eekjrfz0.fsf@bsb.me.uk> <1gkqrft95t0ke57qb3qfo27mmtuqdbqh8b@4ax.com> <87mtz6a06s.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="b3c98be523f7b25b6283cdd761443dc8"; logging-data="25215"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FDRnU3eTSGzIy7+btOBjSoENatN/IIlw=" Cancel-Lock: sha1:hIw8egGq+bgZQgySC/Eh3fSyzvs= sha1:dQHNOmNO7SlFQ9ehLpZrcEQUBwU= X-BSB-Auth: 1.fe6c8502519d50b6f5cf.20201125195823GMT.87pn416wf4.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:39195 John G Harris writes: > On Tue, 24 Nov 2020 21:56:27 +0000, Ben Bacarisse > wrote: > >>John G Harris writes: >> >>> On Mon, 23 Nov 2020 20:11:15 +0000, Ben Bacarisse >>> wrote: >>> >>>>John G Harris writes: >>>> >>>>> On 23 Nov 2020 12:29:27 GMT, ram@zedat.fu-berlin.de (Stefan Ram) >>>>> wrote: >>>>> >>>>> >>>>>> 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.