I know people are really split on them but even in...
# announcements
k
I know people are really split on them but even in Java, I personally prefer
while (true)
with a
break
over crazy assignments in the
while
condition:
Copy code
var len: Int = 0
while (true) {
  len = def.deflate(buf, 0, buf.size)
  if (len <= 0) break
  ...
}
a
assignment in the condition was something I got used to in C 25 years ago, old habits die hard (that's my excuse, anyway)
c
For this kind of loop, I think performance trumps everything so I have no shame writing whatever code will slurp these bytes in as fast as possible.
k
@cedric which would be more performant, actually? Assignment in the condition?
I assume so just because it's what I generally see in SDK methods that use while loops
e
I bet there would be no perf difference.
šŸ‘šŸ» 1
e
I view while(true) { … break; } as a code smell, personally. don’t make me parse the entire block to find out what my exit criteria are.
c
It is a code smell but my point here is that avoiding that code smell might lead to a performance smell
In this particular case, I’d pick whichever proves to be faster even if it leads to slightly less readable code
while
probably beats all the other alternatives and I’m guessing
while(true)
+
break
might beat
while(keepGoing)
, but that’s just a gut feeling
k
@evanchooly I personally feel like it's way more readable if there's a clear guard case at the top of the while loop than trying to read a nested gross
while ((len = obj.someLongMethodThatObscuresTheIntentOfThisCondition()) > 0) { ... }
conditional statement
Languages like Rust have
loop { ... }
for this case, which is basically an alias for
while (true) { ... }
e
but this is kotlin so you could always write an extension function
forEach(int) { }
to hide that all away
k
repeat(int) { ... }
exists in the stdlib aleady.
k
and this is Kotlin so you can't use an assignment as an expression like that anyway šŸ˜†
I'm talking about Java really
e
ewww. šŸ˜‰
c
repeat(Long.MAX_VALUE) {  // should be big enough
k
I just died inside.
k
I mean you could just write:
Copy code
inline fun loop(block: () -> Unit) {
  while(true) { block() }
}
šŸ™‚
could even make that function return
Nothing
k
Well you can still
return@loop
, right?
k
the actual
while (true)
would still continue though, wouldn't it? šŸ¤”
šŸ‘ 1
e
labeled breaks make my butt itch
d
Copy code
repeat(Long.MAX_VALUE) {
    repeat(Long.MAX_VALUE) {  // if 1 repeat isn't enough
    }
}
a
reminds me of the infamous ā€œwaitAndDoubleRetryā€ scheme we found lurking in an old codebase once…