File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes If Condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If Condition" Watch "If Condition" New topic

If Condition

Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
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

Joined: Mar 06, 2005
Posts: 70
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.

Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
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

Joined: Aug 19, 2003
Posts: 182
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

Joined: Feb 23, 2005
Posts: 221
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

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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 ]
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: If Condition
It's not a secret anymore!