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


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

Using break in while loops bad habit?

Started byjonas.thornvall@gmail.com
First post2016-01-16 04:19 -0800
Last post2016-01-19 16:48 +0000
Articles 20 on this page of 56 — 16 participants

Back to article view | Back to comp.lang.javascript


Contents

  Using break in while loops bad habit? jonas.thornvall@gmail.com - 2016-01-16 04:19 -0800
    Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-16 13:59 +0100
      Re: Using break in while loops bad habit? Philip Herlihy <thiswillbounceback@you.com> - 2016-01-16 15:41 +0000
        Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-16 16:41 -0800
    Re: Using break in while loops bad habit? Philip Herlihy <thiswillbounceback@you.com> - 2016-01-16 13:02 +0000
      Re: Using break in while loops bad habit? Tim Streater <timstreater@greenbee.net> - 2016-01-16 13:43 +0000
        Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-16 14:56 +0100
          Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-18 09:39 -0800
            Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-18 19:16 +0100
              Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-23 10:32 -0800
                Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-24 00:49 +0100
                  Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-25 21:36 -0800
                    Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-26 10:12 +0100
                      Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-26 11:11 -0800
                        Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-26 22:48 +0100
                          Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-26 15:03 -0800
                            Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-27 00:51 +0100
                Re: Using break in while loops bad habit? Norman Peelman <npeelman@cfl.rr.com> - 2016-01-23 21:00 -0500
                  Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-25 21:39 -0800
                    Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-26 10:17 +0100
                      Re: Using break in while loops bad habit? Doc O'Leary  <droleary@2015usenet1.subsume.com> - 2016-01-26 17:10 +0000
                    Re: Using break in while loops bad habit? Doc O'Leary  <droleary@2015usenet1.subsume.com> - 2016-01-26 16:44 +0000
                      Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-26 11:17 -0800
                        Re: Using break in while loops bad habit? Doc O'Leary  <droleary@2015usenet1.subsume.com> - 2016-01-27 05:00 +0000
                          Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-27 09:29 -0800
              Re: Using break in while loops bad habit? Norman Peelman <npeelman@cfl.rr.com> - 2016-01-23 20:53 -0500
                Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-24 10:28 +0100
                Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-25 21:41 -0800
        Re: Using break in while loops bad habit? Robert Heller <heller@deepsoft.com> - 2016-01-16 09:06 -0600
          Re: Using break in while loops bad habit? Tim Streater <timstreater@greenbee.net> - 2016-01-16 17:31 +0000
          Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-16 16:52 -0800
    Re: Using break in while loops bad habit? Aleksandro <aleksandro@gmx.com> - 2016-01-16 12:43 -0300
    Re: Using break in while loops bad habit? Silvio <silvio@internet.com> - 2016-01-16 17:51 +0100
      Re: Using break in while loops bad habit? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-01-17 09:09 +0100
        Re: Using break in while loops bad habit? John Harris <niam@jghnorth.org.uk.invalid> - 2016-01-17 11:19 +0000
        Re: Using break in while loops bad habit? Silvio <silvio@internet.com> - 2016-01-18 00:40 +0100
          Re: Using break in while loops bad habit? Andrew Poulos <ap_prog@hotmail.com> - 2016-01-18 13:53 +1100
            Re: Using break in while loops bad habit? Silvio <silvio@internet.com> - 2016-01-18 07:59 +0100
    Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-16 16:34 -0800
      Re: Using break in while loops bad habit? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-17 01:29 +0000
        Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-17 19:22 -0800
          Re: Using break in while loops bad habit? Tim Streater <timstreater@greenbee.net> - 2016-01-18 10:00 +0000
          Re: Using break in while loops bad habit? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-18 10:50 +0000
            Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-20 16:40 -0800
              Re: Using break in while loops bad habit? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-01-21 01:50 +0000
        Re: Using break in while loops bad habit? Dr J R Stockton <reply1600@merlyn.demon.co.uk.invalid> - 2016-01-18 22:55 +0000
          Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-18 16:10 -0800
      Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-18 09:46 -0800
        Re: Using break in while loops bad habit? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-18 10:16 -0800
        Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-18 19:18 +0100
          Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-23 10:33 -0800
            Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-24 00:51 +0100
              Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-25 21:46 -0800
                Re: Using break in while loops bad habit? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2016-01-26 10:22 +0100
                  Re: Using break in while loops bad habit? Gene Wirchenko <genew@telus.net> - 2016-01-26 11:19 -0800
        Re: Using break in while loops bad habit? Doc O'Leary  <droleary@2015usenet1.subsume.com> - 2016-01-19 16:48 +0000

Page 2 of 3 — ← Prev page 1 [2] 3  Next page →


#29449

FromDoc O'Leary <droleary@2015usenet1.subsume.com>
Date2016-01-26 17:10 +0000
Message-ID<n889a5$odn$1@dont-email.me>
In reply to#29446
For your reference, records indicate that 
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote:

> The crux, IMHO, is that it breaks the subroutine/loop/function stack:
> 
> 1 in the machine code,

What?  No.  As I pointed out, it’s all just jumps to the machine.  You
can dress up the higher level concepts, but the machine loops by jumping
back to a previous instruction and stops looping by jumping to an
instruction that doesn’t.

> 2 in modular programming.

How?  If the language was properly designed, a break from a loop should
have the same side effects regardless of where it is located.  Are you
saying JS is flawed in its implementation of break?

Do you have the same objection when it comes to the “continue”
statement, too?  What about an early “return” from a function?  What
about “do-while” loops?

-- 
"Also . . . I can kill you with my brain."
River Tam, Trash, Firefly

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


#29448

FromDoc O'Leary <droleary@2015usenet1.subsume.com>
Date2016-01-26 16:44 +0000
Message-ID<n887qb$hvq$1@dont-email.me>
In reply to#29442
For your reference, records indicate that 
Gene Wirchenko <genew@telus.net> wrote:

>      A break gives a loop another exit; it is another execution path
> that has to be tested. 

That is true even if you use control variables.

> If the loop is long, the break can get lost in
> the code.  I want flow of execution to be obvious.

Then you shouldn’t use control variables, because they can also be lost
in the mix.  On top of that, they are subject to manipulation in more
ways and places than a simple break.

-- 
"Also . . . I can kill you with my brain."
River Tam, Trash, Firefly

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


#29451

FromGene Wirchenko <genew@telus.net>
Date2016-01-26 11:17 -0800
Message-ID<g8hfabpta1bh23cnnqoj4kk3kq2ovcj8hp@4ax.com>
In reply to#29448
On Tue, 26 Jan 2016 16:44:59 -0000 (UTC), Doc O'Leary
<droleary@2015usenet1.subsume.com> wrote:

>For your reference, records indicate that 
>Gene Wirchenko <genew@telus.net> wrote:
>
>>      A break gives a loop another exit; it is another execution path
>> that has to be tested. 
>
>That is true even if you use control variables.

     No.  If I use control variables, they are in the loop control
expression.  Same exit.

>> If the loop is long, the break can get lost in
>> the code.  I want flow of execution to be obvious.
>
>Then you shouldn’t use control variables, because they can also be lost
>in the mix.  On top of that, they are subject to manipulation in more
>ways and places than a simple break.

     Irrelevant.  There is no such thing as a simple break except for
trivial examples where the break will be executed in any iteration.
The reason for this is that there will be a condition to trigger the
break.

     More places?  Try backing up that statement.  Anywhere in a loop
where you could put control variable manipulation, you could put a
break sequence.

Sincerely,

Gene Wirchenko

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


#29456

FromDoc O'Leary <droleary@2015usenet1.subsume.com>
Date2016-01-27 05:00 +0000
Message-ID<n89ita$cp4$1@dont-email.me>
In reply to#29451
For your reference, records indicate that 
Gene Wirchenko <genew@telus.net> wrote:

>      No.  If I use control variables, they are in the loop control
> expression.  Same exit.

They’re *all* the “same exit”.

>      More places?  Try backing up that statement.  Anywhere in a loop
> where you could put control variable manipulation, you could put a
> break sequence.

Any break you see is immediate!  A variable changing is just a variable
changing.  You won’t know if that is equivalent to a break unless you
parse the entire loop body.

if (x > 5) break;

Easy to understand the flow of control.

if (x > 5) exit_level += 16;  // Or “hand_full = true” or whatever!

Have fun checking for anywhere else that gets modified, and then
referencing back to the beginning of the loop to determine what the
*actual* value needs to be for the loop to exit.  And have fun trying
to use your technique with a for-in loop.

-- 
"Also . . . I can kill you with my brain."
River Tam, Trash, Firefly

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


#29457

FromGene Wirchenko <genew@telus.net>
Date2016-01-27 09:29 -0800
Message-ID<q8vhablfr19fe28r3ll28hmqtrl7r5qvpa@4ax.com>
In reply to#29456
On Wed, 27 Jan 2016 05:00:26 -0000 (UTC), Doc O'Leary
<droleary@2015usenet1.subsume.com> wrote:

>For your reference, records indicate that 
>Gene Wirchenko <genew@telus.net> wrote:
>
>>      No.  If I use control variables, they are in the loop control
>> expression.  Same exit.
>
>They’re *all* the “same exit”.

     That is what I stated.

>>      More places?  Try backing up that statement.  Anywhere in a loop
>> where you could put control variable manipulation, you could put a
>> break sequence.
>
>Any break you see is immediate!  A variable changing is just a variable
>changing.  You won’t know if that is equivalent to a break unless you
>parse the entire loop body.

     Sure I will.  If I have a loop condition of
          !eof() and looping
and I see that looping has been assigned the value false, I understand
that the loop is going to be terminated early.

>if (x > 5) break;
>
>Easy to understand the flow of control.

     Yes for that simple case.  Sometimes, there is a need to make
other adjustments before the exit is done.  And overuse of breaks
makes it difficult to tell what the real condition of the loop is.

>if (x > 5) exit_level += 16;  // Or “hand_full = true” or whatever!
>
>Have fun checking for anywhere else that gets modified, and then
>referencing back to the beginning of the loop to determine what the
>*actual* value needs to be for the loop to exit.  And have fun trying

     It is easy with a boolean.

>to use your technique with a for-in loop.

     I have not used those.  If it requires a different technique,
then I will work one out.

Sincerely,

Gene Wirchenko

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


#29427

FromNorman Peelman <npeelman@cfl.rr.com>
Date2016-01-23 20:53 -0500
Message-ID<n81akm$rb8$1@dont-email.me>
In reply to#29317
On 01/18/2016 01:16 PM, Evertjan. wrote:
> Gene Wirchenko <genew@telus.net> wrote on 18 Jan 2016 in
> comp.lang.javascript:
>
>>> What about the double break here to get out of both:
>>>
>>> loop-a {
>>>   loop-b {
>>>     if (x) {break;break;}
>>>   }
>>> }
>>
>>       The second break would not be executed.
>
> Seems you don't follow my explanation.
>
> I am showing code as I think should be implemented
> to be slightly acceptable.
>

Loop-a {
   Loop-b {
     If (x) { break 2; }
   }
}

> As it is now, loop breaks are just as unacceptable as gotos.
>

   Let's not confuse GOTOs with controlled exits. The 'Break' command is 
designed to *stop* the current looping block and continue executing the 
following line. I don't know if I would associate that with the 
vagueness of 'GOTO'. 'Break/Continue' give you quite simple control over 
the loop without adding more and more If/Then/Else's. Also, in the case of:

While (x) {
   ...
}

...there is an implied 'Break' anyway, is there not?



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


#29430

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2016-01-24 10:28 +0100
Message-ID<XnsA5996AA03D663eejj99@194.109.6.166>
In reply to#29427
Norman Peelman <npeelman@cfl.rr.com> wrote on 24 Jan 2016 in 
comp.lang.javascript:

>> I am showing code as I think should be implemented
>> to be slightly acceptable.
>>
> 
> Loop-a {
>    Loop-b {
>      If (x) { break 2; }
>    }
>}

I'll try that. 
Oh, I forget I hate breaks anyway, except for the summer one.
 
>> As it is now, loop breaks are just as unacceptable as gotos.
> 
> Let's not confuse GOTOs with controlled exits.

Well, let's, as there is no confusion!

> The 'Break' command is 
> designed to *stop* the current looping block and continue executing the 
> following line. 

Did you interview the OD?

> I don't know if I would associate that with the 
> vagueness of 'GOTO'.

I don't know either, 
as I don't know your associations,
but you should.

GoTo is not vague at all, 
perhaps less vague than breaks,
as breaks presume the programmer to know 
the innermostness of the loop concerned.

GoTo can also exit loops, subroutines, functions,
and would bfeak the internal loop/subroutine/function-stack
if not designed to clean up the structure-mess just as a break does.

Both break the integrity of the programming confinement they are in,
and so also destruct the sanity of the structural programmer's mind,
which I think is even more important then internal stack-garbage-disposal.

=====

btw:

Historically [ancient BASIC] GoTo was the only way to do an IF-statement,
before the inline "Then"-keyword was introduced:

If z2 = 5 Then a1 = a1 + 1

had to be written:

110 If z2 <> 5 GoTo 130
120 Let a1 = a1 + 1
130 Remark ComeFrom 110 or 120

In VisualBasic there still is some remnant to be found in:

On Error Goto Next/Resume

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

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


#29443

FromGene Wirchenko <genew@telus.net>
Date2016-01-25 21:41 -0800
Message-ID<vk1eabt4knojj0o18dhpsfsrg82bqhmsf7@4ax.com>
In reply to#29427
On Sat, 23 Jan 2016 20:53:01 -0500, Norman Peelman
<npeelman@cfl.rr.com> wrote:

>On 01/18/2016 01:16 PM, Evertjan. wrote:

[snip]

>> As it is now, loop breaks are just as unacceptable as gotos.

>   Let's not confuse GOTOs with controlled exits. The 'Break' command is 
>designed to *stop* the current looping block and continue executing the 
>following line. I don't know if I would associate that with the 
>vagueness of 'GOTO'. 'Break/Continue' give you quite simple control over 
>the loop without adding more and more If/Then/Else's. Also, in the case of:

     Agreed.

>While (x) {
>   ...
>}
>
>...there is an implied 'Break' anyway, is there not?

     No.  A break is a break of the usual execution flow for a loop.

     The usual execution flow for a loop includes termination due to
the control condition evaluating to false.

Sincerely,

Gene Wirchenko

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


#29249

FromRobert Heller <heller@deepsoft.com>
Date2016-01-16 09:06 -0600
Message-ID<p7CdnQD0X6TnwQfLnZ2dnUU7-dGdnZ2d@giganews.com>
In reply to#29247
At Sat, 16 Jan 2016 13:43:51 +0000 Tim Streater <timstreater@greenbee.net> wrote:

> 
> In article <MPG.31044fd5afc9fcf9896d8@news.eternal-september.org>,
> Philip Herlihy <thiswillbounceback@you.com> wrote:
> 
> >In article <94ec98fa-9956-48c9-ad34-de8d07cd6419@googlegroups.com>, 
> >jonas.thornvall@gmail.com says...
> >> 
> >> I have a question regarding the use of breaks in while loops, it seem most
> >> breaks can be avoided by adding extra conditions for the loop. However it
> >> somehow make it a bit harder to get the logic of the loop right.
> >> 
> >> But how does the approaches compare from performance view?
> >> What was the main reason to include break in the language?
> >> Is there logic that can not be resolved without a break in loop?
> >
> >Given that the hardware understands only binary, it follows that any 
> >high-level language is a tool to allow human readers to follow the logic 
> >of a program.  If you buy into that the only thing that really matters 
> >is whether the code is readily and accurately comprehensible.  It may be 
> >that the use of 'break' lends itself semantically to exception 
> >conditions.
> 
> It does and that is just how I proceed. I don't use GOTO (in fact I've
> not used one since 1978 when I stopped writing in FORTRAN) but I'll
> certainly use break where it's appropriate.
> 
> There may be two or three reasons to exit a while loop. So I'll test
> for each one and then break. Putting all the conditions in the while
> itself just gets clumsy.

There are times when the 'exceptional' conditions cannot be [easily] tested in
the while clause, usually because they don't 'exist' in that context or scope.
Or if you must do that, you end up using 'continue' in much the same places as
you would have used 'break' -- one *could* argue that 'continue' is just as
'bad' as 'break' for many of the same reasons. And yes, extreme use of
if-then-else can be used instead of 'continue'. Both break and continue can be
used to make the code *clearer* [to a *human* reader]. OTOH, one 'trick' is to 
write the loop like this:

done = false;
while (!done) {
   if (somecondition) {
       done = true;
   } else {
       some code;
       if (someothercondition) {
           done = true;
       } else {
           some other code;
       }
   }
}

The above totally avoids both break and continue.  Whether it is clear or not 
is a different issue.

-- 
Robert Heller             -- 978-544-6933
Deepwoods Software        -- Custom Software Services
http://www.deepsoft.com/  -- Linux Administration Services
heller@deepsoft.com       -- Webhosting Services
                        

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


#29254

FromTim Streater <timstreater@greenbee.net>
Date2016-01-16 17:31 +0000
Message-ID<160120161731097931%timstreater@greenbee.net>
In reply to#29249
In article <p7CdnQD0X6TnwQfLnZ2dnUU7-dGdnZ2d@giganews.com>, Robert
Heller <heller@deepsoft.com> wrote:

>At Sat, 16 Jan 2016 13:43:51 +0000 Tim Streater <timstreater@greenbee.net>
>wrote:
>
>> 
>> In article <MPG.31044fd5afc9fcf9896d8@news.eternal-september.org>,
>> Philip Herlihy <thiswillbounceback@you.com> wrote:
>> 
>> >In article <94ec98fa-9956-48c9-ad34-de8d07cd6419@googlegroups.com>, 
>> >jonas.thornvall@gmail.com says...
>> >> 
>> >> I have a question regarding the use of breaks in while loops, it seem most
>> >> breaks can be avoided by adding extra conditions for the loop. However it
>> >> somehow make it a bit harder to get the logic of the loop right.
>> >> 
>> >> But how does the approaches compare from performance view?
>> >> What was the main reason to include break in the language?
>> >> Is there logic that can not be resolved without a break in loop?
>> >
>> >Given that the hardware understands only binary, it follows that any 
>> >high-level language is a tool to allow human readers to follow the logic 
>> >of a program.  If you buy into that the only thing that really matters 
>> >is whether the code is readily and accurately comprehensible.  It may be 
>> >that the use of 'break' lends itself semantically to exception 
>> >conditions.
>> 
>> It does and that is just how I proceed. I don't use GOTO (in fact I've
>> not used one since 1978 when I stopped writing in FORTRAN) but I'll
>> certainly use break where it's appropriate.
>> 
>> There may be two or three reasons to exit a while loop. So I'll test
>> for each one and then break. Putting all the conditions in the while
>> itself just gets clumsy.
>
>There are times when the 'exceptional' conditions cannot be [easily] tested in
>the while clause, usually because they don't 'exist' in that context or scope.
>Or if you must do that, you end up using 'continue' in much the same places as
>you would have used 'break' -- one *could* argue that 'continue' is just as
>'bad' as 'break' for many of the same reasons. And yes, extreme use of
>if-then-else can be used instead of 'continue'. Both break and continue can be
>used to make the code *clearer* [to a *human* reader]. OTOH, one 'trick' is to 
>write the loop like this:
>
>done = false;
>while (!done) {
>   if (somecondition) {
>       done = true;
>   } else {
>       some code;
>       if (someothercondition) {
>           done = true;
>       } else {
>           some other code;
>       }
>   }
>}
>
>The above totally avoids both break and continue.  Whether it is clear or not 
>is a different issue.

Typically it won't be clear at all, and yes, I use continue liberally
as well. If you limit these constructs, then you are insisting that the
decision making in the while loop be done either at the beginning or at
the end, rather than at some logical point in th middle. Neither of
these may be appropriate in many circumstances.

This issue is similar to one that popped its ugly head up around the
start or so of the 80s (perhaps before and I hadn't noticed it). This
was the craze fro SESE (single entry, single exit) in functions. You
enter at the top, do stuff, and exit at the end. Early exit was not
allowed. Net result was a rats nest of if-then-else to simulate the
same thing. One time when I was forced to use Pascal as there was
nothing else except macro-11, I used GOTO's instead: label 999 for the
function's return and 998 and lower for loop exists. It was not ideal.

What counts is readability for future maintainers. This is the single
most important factor, IMO.

-- 
New Socialism consists essentially in being seen to have your heart in 
the right place whilst your head is in the clouds and your hand is in 
someone else's pocket.

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


#29274

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-01-16 16:52 -0800
Message-ID<362be156-5fc4-4e84-9337-2898e1997e97@googlegroups.com>
In reply to#29249
On Saturday, January 16, 2016 at 9:06:45 AM UTC-6, Robert Heller wrote:

> There are times when the 'exceptional' conditions cannot be [easily] tested in
> the while clause, usually because they don't 'exist' in that context or scope.
> Or if you must do that, you end up using 'continue' in much the same places as
> you would have used 'break' -- one *could* argue that 'continue' is just as
> 'bad' as 'break' for many of the same reasons. And yes, extreme use of
> if-then-else can be used instead of 'continue'. Both break and continue can be
> used to make the code *clearer* [to a *human* reader]. OTOH, one 'trick' is to 
> write the loop like this:
> 
> done = false;
> while (!done) {
>    if (somecondition) {
>        done = true;
>    } else {
>        some code;
>        if (someothercondition) {
>            done = true;
>        } else {
>            some other code;
>        }
>    }
> }
> 
> The above totally avoids both break and continue.  Whether it is clear or not 
> is a different issue.


Once the exiting criteria of a loop gets too complex to place in its head one should probably refactor the loop.

The same applies when you start to see a Great Maze of If-Elsedom appearing. 

The loop is doing too much.

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


#29251

FromAleksandro <aleksandro@gmx.com>
Date2016-01-16 12:43 -0300
Message-ID<n7doa4$63p$1@dont-email.me>
In reply to#29244
On 16/01/16 09:19, jonas.thornvall@gmail.com wrote:
> I have a question regarding the use of breaks in while loops, it seem most breaks can be avoided by adding extra conditions for the loop. However it somehow make it a bit harder to get the logic of the loop right.
> 
> But how does the approaches compare from performance view?
> What was the main reason to include break in the language?
> Is there logic that can not be resolved without a break in loop?

Just as you don't reed to return from a function in the last line, it's
just a matter of opinion I'd say. Messy programmers will always find a
way to screw up with semantics, that doesn't mean X is no-good.

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


#29252

FromSilvio <silvio@internet.com>
Date2016-01-16 17:51 +0100
Message-ID<569a7510$0$23823$e4fe514c@news.xs4all.nl>
In reply to#29244
On 01/16/2016 01:19 PM, jonas.thornvall@gmail.com wrote:
> I have a question regarding the use of breaks in while loops, it seem most breaks can be avoided by adding extra conditions for the loop. However it somehow make it a bit harder to get the logic of the loop right.
>
> But how does the approaches compare from performance view?
> What was the main reason to include break in the language?
> Is there logic that can not be resolved without a break in loop?
>

Code using break is by a many considered broken.

I have no problem with break if the non-break equivalent is hard to read 
or needs additional variables The latter usually implies the former).

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


#29279

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-01-17 09:09 +0100
Message-ID<40378077.uoPSlP3H4q@PointedEars.de>
In reply to#29252
Silvio wrote:

> On 01/16/2016 01:19 PM, jonas.thornvall@gmail.com wrote:
>> I have a question regarding the use of breaks in while loops, it seem
>> most breaks can be avoided by adding extra conditions for the loop.
>> However it somehow make it a bit harder to get the logic of the loop
>> right.
>>
>> But how does the approaches compare from performance view?
>> What was the main reason to include break in the language?
>> Is there logic that can not be resolved without a break in loop?
>>
                               [who?]
> Code using break is by a many considered broken.

That would be just “many” instead of “a many” in English, but:

<https://en.wikipedia.org/wiki/Weasel_word>

> I have no problem with break if the non-break equivalent is hard to read

Code that is harder to read than it needs to be is bad code.  Because such 
code is also harder to understand and maintain than it needs to be.

> or needs additional variables The latter usually implies the former).

The number of variables on code is to be minimized.  Code that introduces 
variables just for the sake of them is bad code.  Because such code is 
harder to understand and maintain, and less efficient, than it needs to be.

Yours is neither a sound argument nor a professional attitude – to be 
expected by someone who posts only with their first name, if that, and with 
a fake e-mail address that abuses a foreign namespace (of internet.com).

<https://www.xs4all.nl/over-xs4all/voorwaarden.htm>
<http://www.interhack.net/pubs/munging-harmful/>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#29283

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2016-01-17 11:19 +0000
Message-ID<46um9blipnlesi0t3h45v5ato7tr530u4q@4ax.com>
In reply to#29279
On Sun, 17 Jan 2016 09:09:58 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:


  <snip>
>Yours is neither a sound argument nor a professional attitude

Here's Thomas condemning someone who says the same thing as Thomas.


> – to be 
>expected by someone who posts only with their first name, if that, and with 
>a fake e-mail address that abuses a foreign namespace (of internet.com).
  <snip>

More of what Thomas would call ad hominem. :-(

  John

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


#29297

FromSilvio <silvio@internet.com>
Date2016-01-18 00:40 +0100
Message-ID<569c2667$0$23862$e4fe514c@news.xs4all.nl>
In reply to#29279
Oh get over yourself.

I and with me many other will continue to post what and how we see fit 
in this newsgroup. If you have a problem with that you should start your 
own moderated one and don't bother to give it a name that does not 
include your own.

By your perpetual complaints about other peoples posts you have become 
the laughingstock of the group.

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


#29299

FromAndrew Poulos <ap_prog@hotmail.com>
Date2016-01-18 13:53 +1100
Message-ID<rqudnfWiHb42zgHLnZ2dnUU7-LednZ2d@westnet.com.au>
In reply to#29297
On 18/01/2016 10:40 AM, Silvio wrote:
> Oh get over yourself.
>
> I and with me many other will continue to post what and how we see fit
> in this newsgroup. If you have a problem with that you should start your
> own moderated one and don't bother to give it a name that does not
> include your own.
>
> By your perpetual complaints about other peoples posts you have become
> the laughingstock of the group.

On the contrary, I applaud the fact that PE (I assume that's who you're 
talking about) continues to share his knowledge. If you want a place 
where everyone gets a certificate just for attending then perhaps you 
are the one that should consider a different newsgroup?

Andrew Poulos

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


#29301

FromSilvio <silvio@internet.com>
Date2016-01-18 07:59 +0100
Message-ID<569c8d41$0$23859$e4fe514c@news.xs4all.nl>
In reply to#29299
On 01/18/2016 03:53 AM, Andrew Poulos wrote:
> On 18/01/2016 10:40 AM, Silvio wrote:
>> Oh get over yourself.
>>
>> I and with me many other will continue to post what and how we see fit
>> in this newsgroup. If you have a problem with that you should start your
>> own moderated one and don't bother to give it a name that does not
>> include your own.
>>
>> By your perpetual complaints about other peoples posts you have become
>> the laughingstock of the group.
>
> On the contrary, I applaud the fact that PE (I assume that's who you're
> talking about) continues to share his knowledge. If you want a place
> where everyone gets a certificate just for attending then perhaps you
> are the one that should consider a different newsgroup?
>
> Andrew Poulos

Nobody is saying that his knowledge is not valuable. He should just 
limit himself to sharing that. I am not interested in what he thinks of 
the way someone lays out his posts, what name or email address he uses 
or if he chooses to use the term "JavaScript".

And if he is commenting on topic he could be a bit more graceful about 
how he corrects people with less knowledge who are trying to help 
someone or be helped.

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


#29272

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-01-16 16:34 -0800
Message-ID<d2afc3d1-f6d2-4502-867a-ba28a1d506e9@googlegroups.com>
In reply to#29244
On Saturday, January 16, 2016 at 6:20:15 AM UTC-6, jonas.t...@gmail.com wrote:
> I have a question regarding the use of breaks in while loops, it seem most breaks can be avoided by adding extra conditions for the loop.

Correct.

> However it somehow make it a bit harder to get the logic of the loop right.

That would depend on how complex[1][2] the body of the loop is.

> But how does the approaches compare from performance view?

Hard to say. It would depend on the algorithm.

> What was the main reason to include break in the language?

Simply put: because Java had it. [3]

Why did Java have it? 
Because C++ had it. 
why did C++ have it?
Because C had it.
Why did C have it?
Because BCPL had it.
Why did BCPL have it?
Because CPL had it.
Why did CPL have it?
Because ALGOL had it.
Why did ALGOL have it?

Who knows. it had everything and is held up as an example of why Design By Committee[4] is not a good idea.

It is a good guess that it had a BREAK statement because Djikstra wrote a paper criticizing the GOTO statement[5], and like a good committee they made sure they included both features in the language <.<

> Is there logic that can not be resolved without a break in loop?

Not in JavaScript, unless you are trying to do some form of black magic with the JIT.

[1] <https://en.wikipedia.org/wiki/Cyclomatic_complexity>
[2] <https://en.wikipedia.org/wiki/Halstead_complexity_measures>
[3] <https://brendaneich.com/2010/07/a-brief-history-of-javascript/>
[4] <https://en.wikipedia.org/wiki/Design_by_committee>
[5] <https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF>

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


#29275

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-01-17 01:29 +0000
Message-ID<87io2tf0xq.fsf@bsb.me.uk>
In reply to#29272
"Michael Haufe (TNO)" <tno@thenewobjective.com> writes:

> On Saturday, January 16, 2016 at 6:20:15 AM UTC-6, jonas.t...@gmail.com wrote:
>> I have a question regarding the use of breaks in while loops, it
>> seem most breaks can be avoided by adding extra conditions for the
>> loop.
>
> Correct.
>
>> However it somehow make it a bit harder to get the logic of the loop right.
>
> That would depend on how complex[1][2] the body of the loop is.
>
>> But how does the approaches compare from performance view?
>
> Hard to say. It would depend on the algorithm.
>
>> What was the main reason to include break in the language?
>
> Simply put: because Java had it. [3]
>
> Why did Java have it? 
> Because C++ had it. 
> why did C++ have it?
> Because C had it.
> Why did C have it?
> Because BCPL had it.
> Why did BCPL have it?
> Because CPL had it.

Did CPL have break?  I can't find it in the most widely published paper
on CPL[1] but CPL evolved a lot and was never finalised so it may well
have acquired break by the end.  Do you have a better reference?

> Why did CPL have it?
> Because ALGOL had it.

Did Algol have it?  I don't think so.

> Why did ALGOL have it?
>
> Who knows. it had everything and is held up as an example of why
> Design By Committee[4] is not a good idea.

I'm not just nit-picking.  Your idea is that it's lost in time in some
committee's vanished collective memory, but break seems to have been
added by BCPL -- a design be a single, still living, individual.  So if
that's the case one could ask!

<snip>
[1] "The main features of CPL", D. W. Barron, J. N. Buxton,
    D. F. Hartley, E. Nixon and C. Strachey, The Computer Journal, 1963.

-- 
Ben.

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


Page 2 of 3 — ← Prev page 1 [2] 3  Next page →

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


csiph-web