Java Compiler: Optimization of "cascaded" ifs and best practices?

Posted by jens on Stack Overflow See other posts from Stack Overflow or by jens
Published on 2010-05-19T02:54:08Z Indexed on 2010/05/19 3:00 UTC
Read the original article Hit count: 352

Filed under:

Hello,

does the Java Compiler optimize a statement like this

if (a == true) {
 if (b == true) {
  if (c == true) {
   if(d == true) {
       //code to process stands here
   }
  }
 }
}

to

if (a == true && b==true && c==true && d == true)

So thats my first question: Do both take exactly the same "CPU Cycles" or is the first variant "slowlier".

My Second questin is, is the first variant with the cascaded if considered bad programming style as it is so verbose?

(I like the first variant as I can better logically group my expressions and better comment them (my if statements are more complex than in the example), but maybe thats bad proramming style?) and even slowlier, thats why I am asking...

Thanks Jens

© Stack Overflow or respective owner

Related posts about java