Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.javascript Subject: Re: Understanding && Date: Sun, 18 Apr 2021 00:14:36 +0100 Organization: A noiseless patient Spider Lines: 71 Message-ID: <87lf9ga4gz.fsf@bsb.me.uk> References: <87wnt2e06e.fsf@bsb.me.uk> <87mttydm95.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="cebc7205e7af165aaf5b3baa2f235ea5"; logging-data="22134"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/L9PYElpNStPqX4mfJs/T3PdJaL+5ZQwI=" Cancel-Lock: sha1:2axXsxvKk1rLTXwsMeqf4Fjd8wk= sha1:PKzoynWbgdJxGlNv3rSX5zg2uhU= X-BSB-Auth: 1.397c7dd8193c165f46ad.20210418001436BST.87lf9ga4gz.fsf@bsb.me.uk Xref: csiph.com comp.lang.javascript:39972 Arno Welzel writes: > Ben Bacarisse: > >> Jon Ribbens writes: >> >>> On 2021-04-16, Ben Bacarisse wrote: >>>> Jon Ribbens writes: >>>>> You most often see this sort of thing in JavaScript that was >>>>> automatically generated by a compiler (either because the original >>>>> source was in something else like TypeScript or a more modern version >>>>> of JavaScript or because the author wanted to compress/minify the >>>>> script), but human beings are also quite capable of writing hard-to-read >>>>> code like this. >>>> >>>> Is that specifically when the right-hand operand is an assignment >>>> expression? I don't find that hard to read but then I'm used to >>>> assignment being and expression so it seems like a natural extension of >>>> the more frequently seen uses of &&. >>> >>> I mean where it's an expression being abused as a control flow >>> mechanism. >> >> If readability is not the problem I'm not sure what constitutes abuse. > > Abuse is to use things for something which they are not meant to be > used. An *expression* is called so because you *express* something > like I don't think it's not that simple. In some views, an expression can express an action, and a statement can describe actions that express a value. Obviously you are in the oil and water camp about this, but the arguments for that should be more than quasi tautologies about expressions expressing something. > x = a && b; > > Nobody would expect that this *expression* will cause parts of the code > to get executed or not Hmm... Everyone should expect that since that's what && means. Do you mean && (as defined) should simply not exist is programming languages? > like in this example: > > x = a && (b=5); > > It's not just a "readability problem". It's relying on side effects like > that in a && b the "b" part will not get exectued if "a" is false. But > this is just a convention and nothing obvious. > > >> Is it just a rule of yours to keep these two things entirely separate? >> Maybe some uses are acceptable and don't constitute abuse? Is any > > I see this as "abuse" as well. > >> reliance on short-circuit evaluation in an expression an abuse? If so, >> does that extend to '??' and '?.'? (That's not easy to make clear in >> typing but I hope you know what I mean.) > > The ??= operator was *intentionally* designed to work this way. That's a new one to me. I thought the 11th edition was the latest, and it's not in there. That aside, I think && and || were intentionally designed to work as they do, so intentional design can't justification enough in your mind. -- Ben.