Am 22.04.2016 um 18:24 schrieb Leon Rosenberg: > Hi guys, > > this is completely off-topic ;-) > > I was wondering if using if/else is not actually slowing down your code. > Lets say I have three possible conditions, A, B and C, which are exclusive. > My native approach would be: > if (A){...} > if (B){...} > if (C){...} > > now some people would 'optimize' it as > if (A){ ...} else if (B) {....} else if (C) { ....} > and I think in the world of single-cpu computers this optimization could > work. That would be me - as you say they're mutually exclusive.
> > But what is now, given that compilers can optimize stuff like this and tell > the processor to calculate all 3 branches simultaneously, which is not > possible for ifelse. > > Which one would you choose? > Equally important, which one do you think is more readable? I would say if > else is hard to read, but this can be just personal impression. > > regards > Leon I find that the if/else communicates the mutual exclusiveness better than individual if blocks - thus be implicitly better readable. Without proper measurements I'd be optimizing purely for readability of the maintaining developer (which might be me - yes, I'm quite selfish). *Only* in cases where a proper load test has demonstrated that a different way to write this code makes a *significant* difference, I'd shy away from this default. Which includes that this piece of code must be in a highly frequented section, executed extremely often (otherwise it wouldn't make a difference). Yes, compilers can optimize, but developers and maintainers need to understand the original intent, and they need every clue they can get. Measure both options under realistic circumstances, then check the difference. If the difference is significant, you might be up to something. If it isn't: Make it as readable as possible. Optimize for the maintainer, not for the compiler. The maintainer might buy you a beer, the compiler for sure will not. My 2ct (Euro, of course) Olaf --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org