[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Using break in while loops bad habit?

JT

1/16/2016 12:20:00 PM

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?
55 Answers

Evertjan.

1/16/2016 12:59:00 PM

0

jonas.thornvall@gmail.com wrote on 16 Jan 2016 in comp.lang.javascript:

> 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.

Using such a break would not be necessary if you think and program MODULAR.

> But how does the approaches compare from performance view?

Well, you would have to measure that in the specific implementation.

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

Sorry, you would have to mindread or ask the specific implementator[s].

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

No. The if-statement can always be used, but proper modular programming
gives better logic.



======================

Edsger Wybe Dijkstra was from the 1960s the main proponent for
"Structured programming" [the word "modular" probly was not coined yet in
this sense]:

1968 on a typewriter: "Go-to statement considered harmful,
A case against the Go To statement"
<http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD2...

Wiki:

<https://en.wikipedia.org/wiki/Edsger_W._Di...
<https://en.wikiquote.org/wiki/Edsger_W._Di...



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

Philip Herlihy

1/16/2016 1:03:00 PM

0

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.

--

Phil, London

Tim Streater

1/16/2016 1:44:00 PM

0

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.

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens

Evertjan.

1/16/2016 1:57:00 PM

0

Tim Streater <timstreater@greenbee.net> wrote on 16 Jan 2016 in
comp.lang.javascript:

> 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.

It would just, but barely, be acceptable,
if loops inside loops were not allowed.

Defining the "innermost loop" as the loop that is "broken"
only complicates things.

What about the double break here to get out of both:

loop-a {
loop-b {
if (x) {break;break;}
}
}

or should we consider "named brakes"
[implicitly garbaging inner ones]?

loop-a {
loop-b {
if (x) {break loop-a;}
}
}

Better stay clear of such breaks just like it was Jquery.

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

Robert Heller

1/16/2016 3:07:00 PM

0

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.dee... -- Linux Administration Services
heller@deepsoft.com -- Webhosting Services

Philip Herlihy

1/16/2016 3:41:00 PM

0

In article <XnsA5918E421DB8Beejj99@194.109.6.166>,
exxjxw.hannivoort@inter.nl.net says...
>
> 1968 on a typewriter: "Go-to statement considered harmful,
> A case against the Go To statement"
> <http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD2...
>

A programming team of which I was a member in the early '80s readily
recognised the difficulties with the GoTo statement, and resolved to
replace it with the "ComeFrom" statement instead. In other work, we
sought to model the observed behaviour of computer systems by positing a
"sometimes" qualifier, to be appended to a statement in much the same
way that the !important qualifier is appended to lines of CSS. Take-up
of these far-seeming developments has so far been slow.
--

Phil, London

Aleksandro

1/16/2016 3:44:00 PM

0

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.

Silvio

1/16/2016 4:51:00 PM

0

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).

Tim Streater

1/16/2016 5:31:00 PM

0

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.

Michael Haufe (\"TNO\")

1/17/2016 12:34:00 AM

0

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_comp...
[2] <https://en.wikipedia.org/wiki/Halstead_complexity_me...
[3] <https://brendaneich.com/2010/07/a-brief-history-of-javas...
[4] <https://en.wikipedia.org/wiki/Design_by_com...
[5] <https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD2...