toBe() || !toBe()
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Fred Kleinschmidt wrote:Do you know how to use
toBe() || !toBe()
someMethod1() and someMethod2() still need to run for every loop.
Norm Radder wrote:
someMethod1() and someMethod2() still need to run for every loop.
That implies that comments would be useful for anyone reading the code to understand what the code is supposed to do.
toBe() || !toBe()
what you're trying to say
Norm Radder wrote:
what you're trying to say
Fred assumed that the purpose of the loop was to find a dino that was purple. At that point he thought the loop could be exited with a break.
Comments describing what the loop was supposed to do would prevent that confusion.
toBe() || !toBe()
toBe() || !toBe()
Jesper de Jong wrote:you could include a check in the if-statement to see if the boolean has already been set, but that makes the code longer and doesn't really gain you anything
toBe() || !toBe()
Zahro Alfelpha wrote:
This is actually what I ended up doing for now. It just irks me to set the same variable over and over again, and part of me thinks that the boolean comparison has got to be slightly faster than the string comparison, even on some negligible level.
Junilu Lacar wrote:"Premature optimization is the root of all evil."
toBe() || !toBe()
Junilu Lacar wrote:
toBe() || !toBe()
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Zahro Alfelpha wrote:
Junilu Lacar wrote:
I'm not familiar with this syntax of using || in an assignment like that. Care to elaborate a little?
Junilu Lacar wrote:There's also some irony in the fact that the expression toBe() || !toBe() will always be true
toBe() || !toBe()
Junilu Lacar wrote:There's also some irony in the fact that the expression toBe() || !toBe() will always be true
toBe() || !toBe()
Junilu Lacar wrote:If you really want to stick with one loop, you might try using a profiler to see if this gives you any significant gain in performance:
My gut tells me this might be more efficient. I could be wrong. (See my previous point)
Ryan McGuire wrote:
Junilu Lacar wrote:If you really want to stick with one loop, you might try using a profiler to see if this gives you any significant gain in performance:
My gut tells me this might be more efficient. I could be wrong. (See my previous point)
You could convert this to...
Some would call that a "trick". I prefer "technique" - it's perfectly valid syntax and is exactly what the ||= operator was implemented for, so it shouldn't be considered esoteric at all.
toBe() || !toBe()
Paweł Baczyński wrote:There is no ||= operator in Java...
toBe() || !toBe()
And you don't really need to.Zahro Alfelpha wrote:It seems silly to just set the boolean over and over again when just setting it once would work
Paweł Baczyński wrote:There is no ||= operator in Java...
Liutauras Vilda wrote:I would try to do something with methods someMethod1 and someMethod2 if I were to know what they mean.
toBe() || !toBe()
Ryan McGuire wrote:
Well that's embarrassing. My only defense is that C# sharp has had that operator for a number of versions and I figured surely Java would've caught up on that by now.
It doesn't.Ryan McGuire wrote:That operator works as I would expect when working on booleans.
Liutauras Vilda wrote:
It doesn't.Ryan McGuire wrote:That operator works as I would expect when working on booleans.
Check this snippet:
What value 'b' you'd expect?
Don't get me started about those stupid light bulbs. |