Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38228
| From | Martin Gregorie <martin@mydomain.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Exit from loop? |
| Date | 2018-04-05 10:46 +0000 |
| Organization | albasani.net |
| Message-ID | <pa4uqt$gn5$1@news.albasani.net> (permalink) |
| References | <6f19094e-c652-44e0-8a7d-fb3b7791c372@googlegroups.com> <pa38s0$35o$1@gioia.aioe.org> <cdaa9694-0267-45d2-80f4-5362ac858ceb@googlegroups.com> <pa3od9$roj$1@gioia.aioe.org> |
On Wed, 04 Apr 2018 19:51:04 -0400, Arne Vajhøj wrote: > The first example has two returns from the method. > > The second example has two exits from the loop. > > My suggestion has two conditions for exit from loop. > > I think that first->second->my goes to better code. > > And if one has strong religious beliefs about code then one should > really focus on getting this right. > > But if one is pragmatic/practical oriented, then I would say that all 3 > are very readable and there is no reason to spend time on such a > non-issue. > Sometimes this sort of loop coding problem becomes more readable/ understandable by converting the logic into an if..then..else 'totem pole' with a single 'stop now' flag that gets set by one or more of the conditions and is only tested in the 'for' or 'while' clause that drives the loop. I did exactly that yesterday, but in C rather than Java. I'd originally written a set of unconnected conditions into the loop, some of which either skipped the rest with 'continue' or exited with 'break' as well as the 'for' statement stopping at the end of the string being processed. When it all started to look a bit untidy, I rewrote it, combining the conditions into a totem-pole and leaving just the end-of-string stop condition. Big improvement in readability, and it ran correctly at first try. I also admit to preferring to have a single exit point from loops and a single return from functions/methods. -- Martin | martin at Gregorie | gregorie dot org
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Exit from loop? Eric Douglas <e.d.programmer@gmail.com> - 2018-04-04 12:05 -0700
Re: Exit from loop? Arne Vajhøj <arne@vajhoej.dk> - 2018-04-04 15:25 -0400
Re: Exit from loop? Eric Douglas <e.d.programmer@gmail.com> - 2018-04-04 12:44 -0700
Re: Exit from loop? Arne Vajhøj <arne@vajhoej.dk> - 2018-04-04 19:51 -0400
Re: Exit from loop? Martin Gregorie <martin@mydomain.invalid> - 2018-04-05 10:46 +0000
Re: Exit from loop? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-04-06 18:19 +0200
csiph-web