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


Groups > comp.lang.javascript > #39980

Re: Understanding &&

Path csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Understanding &&
Date Sun, 18 Apr 2021 23:59:28 +0100
Organization A noiseless patient Spider
Lines 87
Message-ID <87mttv42sv.fsf@bsb.me.uk> (permalink)
References <U_CdnQWSXulHj-T9nZ2dnUU7-evNnZ2d@westnet.com.au> <slrns7ishn.4j8.jon+usenet@raven.unequivocal.eu> <87wnt2e06e.fsf@bsb.me.uk> <slrns7jb5v.4j8.jon+usenet@raven.unequivocal.eu> <87mttydm95.fsf@bsb.me.uk> <slrns7jv2m.4j8.jon+usenet@raven.unequivocal.eu> <87o8edd8qk.fsf@bsb.me.uk> <slrns7lv7n.6lk.jon+usenet@raven.unequivocal.eu> <87zgxwa5sg.fsf@bsb.me.uk> <slrns7msra.6lk.jon+usenet@raven.unequivocal.eu>
Mime-Version 1.0
Content-Type text/plain
Injection-Info reader02.eternal-september.org; posting-host="d80a767a035369f2ba4fbb5c271696c3"; logging-data="25532"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/WaiLGPqn1DpeulIS02GWJoG2iiINBa2A="
Cancel-Lock sha1:Ms8Y8X4LxUP1IxwAcSIW29tHI+4= sha1:Ffg1+JtXobsBXIlr1ibAhYmeJFo=
X-BSB-Auth 1.37f4afac538ed577217f.20210418235928BST.87mttv42sv.fsf@bsb.me.uk
Xref csiph.com comp.lang.javascript:39980

Show key headers only | View raw


Jon Ribbens <jon+usenet@unequivocal.eu> writes:

> On 2021-04-17, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>> Jon Ribbens <jon+usenet@unequivocal.eu> writes:
>>> I don't mean as a rule that's written down, I mean as in 99% of
>>> programmers do not write code that looks like this. I don't consciously
>>> have it as a "rule" either except inasmuch as it is a consequence of
>>> "readability matters".
>>
>> I read too much into some very strong words.
>
> I did say "abuse", which can be a very strong word in some contexts but
> isn't really in when talking about software programming. You introduced
> the word "rule".

Oh, please!  I /asked/ if it was a rule as far you were concerned, so,
yes, I typed the word first, but your reply was to claim it was an
almost universal rule.  That's the strong language!

>> Your view is much more moderate than I thought it was from some of the
>> words used, but I did get that you were making balanced judgement.  When
>> I said I don't have a problem with the readability I was suggesting that
>> nothing is last so there is no need for any balancing gain, but I think
>> you see readability through the eyes of the least skilled reader who
>> might have to look at the code.  I find that hard.  How low should one
>> go...
>
> It depends on the context I suppose. Some companies hire expensive DBAs,
> so that they can hire cheap programmers. Some companies don't.
>
> I would expect a moderately competent programmer to know immediately
> what's going on with something like "a && (b = c)" but at the same time
> it would require more intellectual effort expended (I think, for most
> people) to verify its correctness than "if (a) b = c",

Interesting.  I would not have thought so, but I have no data.  Code
verification seems to be a lost art these days so my main reaction is
delight that it's still considered important!

> and the
> complexity basically goes up exponentially the more branches you put
> into the expression.

Ack.   I'm sure that this is true.

> I use "JavaScript Standard Style", and so I must admit I am sometimes
> tempted when I have something like:
>
>   if (complex_expression) complex_conditional
>
> and it doesn't fit on one line, to use:
>
>   complex_expression &&
>     complex_conditional
>
> because the linter won't object to that, but it will object to:
>
>   if (complex_expression)
>     complex_conditional
>
> but almost always I will end up using instead:
>
>   if (complex_expression) {
>     complex_conditional
>   }
>
> because it is probably easier for others to read ("others" perhaps
> including me a year later...) And by "easier" I don't mean incompetent
> programmers can't understand it, I just mean it will take most people
> slightly longer to understand / have a slightly higher percentage chance
> of being misunderstood.

That's another interesting perspective.  When I look back on old code,
it's never things like this that take any time at all.  I often wish
there were a large block comment explaining the algorithm or method
being used, and sometimes I just wish for a comment explaining /why/
count is being set to i + j, but I never lose even a moment over /what/
the code is doing (in the sense of what the syntax means).

Another attraction of '&&' is that it chains in a natural way whereas
'if' nests.  Sometimes (as in the example of mine that you'd reject
immediately) you can negate the tests and use early returns to avoid the
nesting, but I don't like doiing either very much.  There are studies
that have shown that nesting is harder to follow that chaining.

-- 
Ben.

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


Thread

Understanding && Andrew Poulos <ap_prog@hotmail.com> - 2021-04-16 14:30 +1000
  Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-16 11:00 +0100
  Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-16 11:17 +0000
    Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-16 16:09 +0100
      Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-16 15:26 +0000
        Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-16 21:09 +0100
          Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-16 21:06 +0000
            Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 02:01 +0100
              Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-17 08:33 +0100
              Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-17 12:06 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-17 14:13 +0100
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 15:50 +0100
              Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-17 15:21 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 23:46 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-17 23:46 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 23:59 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-19 13:56 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-19 23:08 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-19 22:52 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-20 01:23 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-20 01:08 +0000
              Re: Understanding && Arno Welzel <usenet@arnowelzel.de> - 2021-04-18 00:49 +0200
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 17:26 +0100
          Re: Understanding && Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-17 17:05 +0200
            Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 23:04 +0100
              Re: Understanding && Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-18 00:48 +0200
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 17:13 +0100
                Re: Understanding && Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-19 21:58 +0200
            Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-18 18:20 +0100
          Re: Understanding && Arno Welzel <usenet@arnowelzel.de> - 2021-04-18 00:40 +0200
            Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 00:14 +0100
              Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-18 00:01 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 17:44 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-18 17:51 +0000
              Re: Understanding && Arno Welzel <usenet@arnowelzel.de> - 2021-04-19 13:41 +0200
                Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-19 15:02 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-20 17:40 +0100
                Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-20 18:19 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-21 10:11 +0100
                Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-21 19:46 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-22 13:48 +0100
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-19 21:28 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-19 21:56 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-19 23:13 +0100

csiph-web