Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38224 > unrolled thread
| Started by | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| First post | 2018-04-04 12:05 -0700 |
| Last post | 2018-04-06 18:19 +0200 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2018-04-04 12:05 -0700 |
| Subject | Exit from loop? |
| Message-ID | <6f19094e-c652-44e0-8a7d-fb3b7791c372@googlegroups.com> |
Does it cause any problems to exit from loop, or is it considered bad form?
ie can you do this?
public static int randomMethod() {
int i = 0;
int tries = 0;
for (int ctr = 0; ctr < 100; ctr++) {
i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
tries++;
if (i == 3) {
return tries;
}
}
return tries;
}
or should you do this?
public static int randomMethod() {
int i = 0;
int tries = 0;
for (int ctr = 0; ctr < 100; ctr++) {
i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
tries++;
if (i == 3) {
break;
}
}
return tries;
}
This is of course a simple sample, where the value getting assigned in the loop, and/or the value returned from the method are normally complex objects.
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2018-04-04 15:25 -0400 |
| Message-ID | <pa38s0$35o$1@gioia.aioe.org> |
| In reply to | #38224 |
On 4/4/2018 3:05 PM, Eric Douglas wrote:
> Does it cause any problems to exit from loop, or is it considered bad form?
>
> ie can you do this?
>
>
> public static int randomMethod() {
> int i = 0;
> int tries = 0;
> for (int ctr = 0; ctr < 100; ctr++) {
> i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
> tries++;
> if (i == 3) {
> return tries;
> }
> }
> return tries;
> }
>
> or should you do this?
>
> public static int randomMethod() {
> int i = 0;
> int tries = 0;
> for (int ctr = 0; ctr < 100; ctr++) {
> i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
> tries++;
> if (i == 3) {
> break;
> }
> }
> return tries;
> }
>
> This is of course a simple sample, where the value getting assigned in the loop, and/or the value returned from the method are normally complex objects.
Between those two I would probably do the last, but
I see no big problems with the first.
If I had the choice I would probably use a do while loop.
int ctr = 0;
X o = new X();
do {
o.something();
ctr++;
} while(ctr < 100 && ThreadLocalRandom.current().nextInt(1, 10) != 3);
Arne
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2018-04-04 12:44 -0700 |
| Message-ID | <cdaa9694-0267-45d2-80f4-5362ac858ceb@googlegroups.com> |
| In reply to | #38225 |
On Wednesday, April 4, 2018 at 3:26:15 PM UTC-4, Arne Vajhøj wrote:
> On 4/4/2018 3:05 PM, Eric Douglas wrote:
> > Does it cause any problems to exit from loop, or is it considered bad form?
> >
> > ie can you do this?
> >
> >
> > public static int randomMethod() {
> > int i = 0;
> > int tries = 0;
> > for (int ctr = 0; ctr < 100; ctr++) {
> > i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
> > tries++;
> > if (i == 3) {
> > return tries;
> > }
> > }
> > return tries;
> > }
> >
> > or should you do this?
> >
> > public static int randomMethod() {
> > int i = 0;
> > int tries = 0;
> > for (int ctr = 0; ctr < 100; ctr++) {
> > i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
> > tries++;
> > if (i == 3) {
> > break;
> > }
> > }
> > return tries;
> > }
> >
> > This is of course a simple sample, where the value getting assigned in the loop, and/or the value returned from the method are normally complex objects.
>
> Between those two I would probably do the last, but
> I see no big problems with the first.
>
> If I had the choice I would probably use a do while loop.
>
> int ctr = 0;
> X o = new X();
> do {
> o.something();
> ctr++;
> } while(ctr < 100 && ThreadLocalRandom.current().nextInt(1, 10) != 3);
>
> Arne
It's just a question of exiting from the middle of the structure, not necessarily which structure.
In my real world code it is a simple for loop with a counter. It's calling an http request method which sometimes fails, sometimes just works if you try again, so the loop counter is just how many times you want to retry. Then it calls a custom method on the return object to test for success to exit. Within the loop after the return/break it logs the error condition.
There are coding preferences to always exit from a single point, but if the language doesn't care, sometimes it's easier to exit in the middle of the loop structure if there's nothing you need to do after the loop. If it's bad for the language ie messing with the garbage collector, we should always exit properly with the break.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2018-04-04 19:51 -0400 |
| Message-ID | <pa3od9$roj$1@gioia.aioe.org> |
| In reply to | #38226 |
On 4/4/2018 3:44 PM, Eric Douglas wrote:
> On Wednesday, April 4, 2018 at 3:26:15 PM UTC-4, Arne Vajhøj wrote:
>> On 4/4/2018 3:05 PM, Eric Douglas wrote:
>>> Does it cause any problems to exit from loop, or is it considered bad form?
>>>
>>> ie can you do this?
>>>
>>>
>>> public static int randomMethod() {
>>> int i = 0;
>>> int tries = 0;
>>> for (int ctr = 0; ctr < 100; ctr++) {
>>> i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
>>> tries++;
>>> if (i == 3) {
>>> return tries;
>>> }
>>> }
>>> return tries;
>>> }
>>>
>>> or should you do this?
>>>
>>> public static int randomMethod() {
>>> int i = 0;
>>> int tries = 0;
>>> for (int ctr = 0; ctr < 100; ctr++) {
>>> i = java.util.concurrent.ThreadLocalRandom.current().nextInt(1, 10);
>>> tries++;
>>> if (i == 3) {
>>> break;
>>> }
>>> }
>>> return tries;
>>> }
>>>
>>> This is of course a simple sample, where the value getting
>>> assigned in the loop, and/or the value returned from the method are normally
>>> complex objects.
>>
>> Between those two I would probably do the last, but
>> I see no big problems with the first.
>>
>> If I had the choice I would probably use a do while loop.
>>
>> int ctr = 0;
>> X o = new X();
>> do {
>> o.something();
>> ctr++;
>> } while(ctr < 100 && ThreadLocalRandom.current().nextInt(1, 10) != 3);
>
> It's just a question of exiting from the middle of the structure, not necessarily which structure.
> There are coding preferences to always exit from a single point, but
> if the language doesn't care, sometimes it's easier to exit in the
> middle of the loop structure if there's nothing you need to do after the
> loop. If it's bad for the language ie messing with the garbage
> collector, we should always exit properly with the break.
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.
Arne
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@mydomain.invalid> |
|---|---|
| Date | 2018-04-05 10:46 +0000 |
| Message-ID | <pa4uqt$gn5$1@news.albasani.net> |
| In reply to | #38227 |
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
[toc] | [prev] | [next] | [standalone]
| From | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| Date | 2018-04-06 18:19 +0200 |
| Message-ID | <pa86no$dln$1@dont-email.me> |
| In reply to | #38226 |
On 2018-04-04 21:44, Eric Douglas wrote: > There are coding preferences to always exit from a single point, but if the language doesn't care, sometimes it's easier to exit in the middle of the loop structure if there's nothing you need to do after the loop. If it's bad for the language ie messing with the garbage collector, we should always exit properly with the break. > The language doesn't care. It's a matter of readability. Yes, arguably, single-return can make code analysis easier, and might enable more advanced JIT-like voodoo. In my experience and for most practical concerns, that's negligible, however. -- DF.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web