Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
conor o callaghan wrote:This is just a question of taste. I don't expect everyone to agree with me, but is it just me or does anyone else here find this bit of code a little annoying.
I mean its perfect...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Peter Kirk wrote:I'd probably just write it
for (cond) {
if (object.isProcessable()) {
other lines;
}
}
or a while loop as others have said, depending on what the iteration is. But I don't think
while (cond && object.isProcessable())
is neccessarily the same.
Ernest Friedman-Hill wrote:I'll occasionally use the
format if indenting "something" one more level would be bad, or if "something" is complex and/or expected behavior. Most of the time I'd agree with your rewrite, but I wouldn't say that the original form is always out of place.
Peter Kirk wrote:Recently I did actually do something similar, I must admit. I was bug fixing some code, and came across something like this
A problem could occur sometimes in the loop, where "object" could become invalid for further processing. So I just did something like:
so shoot me.
Peter Kirk wrote:But I don't think
while (cond && object.isProcessable())
is neccessarily the same...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Peter Kirk wrote:But I don't think
while (cond && object.isProcessable())
is neccessarily the same...
Actually, based on what we were supplied, it is;
It's not what your program can do, it's what your users do with the program.
Joe Areeda wrote:I think
Is different because it stops at the first object that is not Processable and the original code searches the whole list.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
It is kind of unclear what is going on and there seem to be multiple interpretations so far.Winston Gutkowski wrote:
Joe Areeda wrote:I think
Is different because it stops at the first object that is not Processable and the original code searches the whole list.
At the risk of flogging this to death, the code originally posted wasn't clear and I took 'cond' to mean some kind of condition, rather than a for-each. And since the isProcessable() test was the first line in the loop, it seemed logical that it could be put outside.
Of course 'cond' could have included something to retrieve object, so I was wrong anyway; but at least it prompted further explanation .
Winston
It's not what your program can do, it's what your users do with the program.
Actually OP has now clarified it as being basically a for-each loop.Joe Areeda wrote:It is kind of unclear what is going on and there seem to be multiple interpretations so far.
Wow, that never even occurred to me; but you're quite right. Yet another reason to read that "Tell, don't ask" article .The original code looks to me like a polling situation in a background task, where cond is some kind of switch to terminate the process and object.isProcesable() is somehow changing state external to the loop.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |