• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

If Condition

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?...
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic