Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If Condition

 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
I have a single query here... As we all know if an if condition has a single statement to execute we can write as below:



thus we dont need any enclosing braces. However i have read somewhere that its a good practice to have enclosing brackets . thus from abve



My question is why is it advised the latter way?...
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is easier to see what is included inside the if statement and what is not.

It makes it easier for other programmers to understand.

Look up "scope".

That's what the brackets are called, scope.

Eric
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, the 'I' in 'If' should be lower case.

The reason for always having braces is to make absolutely clear what is included in the conditional statement. Put simply, if you always use braces, then you always have one style. If you don't, then you are using two styles: one for conditionals containing one statement, and one for multi-line ones. In my opinion, code clarity always overrules brevity.
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question can be best understood this way

does during the compilation of the java file into .class files , the compiler has to explicitly insert the braces for a non braced if?...i know its not an over head if so, however we would like to keep the compiler happy
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't get things the wrong way round

The compiler understands both formats, so it doesn't need to put the braces in so it can then understand them!

Don't worry about the compiler - worry about the clarity of your code.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When modifying code without the braces, this is a very common error:

Actually you're fairly lucky here because the else will likely be unbalanced. But can you see how this appears to do one thing but compiles to do something else?

Now to be honest, I leave the braces out fairly often. I use the code formatter in Eclipse after every couple lines of typing so I'm not usually fooled by false indentation like that. Still, it's a pain to go back and insert braces later, and I do encourage you to get in the "always use them" habit, even if I didn't.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Eclipse (or any other modern editor), turn on templates. That way, you type "if" and hit the spacebar, and the editor puts inwhere ^ is the new position of the caret for typing text and ... is just there to show you it's indented. I haven't typed a brace in Java in years. Well, okay, I have when fixing other people's code.

There is one place where I don't use braces: debugging. I have methods defined in my classes to help do Log4j logging.When you're learning something new, it's best to stick to one method of doing it. Once you gain experience, you'll become comfortable with altering your style as you see fit. If you try to do that early in your training, you'll hamper your progress.
[ March 07, 2005: Message edited by: David Harkness ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic