[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Is it necessary to use {} for one-line code?

Posted on 2006-11-22
7
Medium Priority
?
204 Views
Last Modified: 2010-03-31
I never pay attention on {} when it is used for one-line code in Java.  Someone said it will cause java refresh problem without {}. Can anyone explain it in detail?
- Are below two block of codes same??
- Any difference if it is inside a while or for-loop?

if (quantity > 1000)
    itemDiscount = DiscountPrice * itemPrice;
else
    itemDiscount = NormalPrice * itemPrice;

//----------------

if (quantity > 1000)
{
    itemDiscount = DiscountPrice * itemPrice;
}
else {
    itemDiscount = NormalPrice * itemPrice;
}



0
Comment
Question by:owenli27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 17996155
They're identical but the latter is much preferable
0
 
LVL 9

Author Comment

by:owenli27
ID: 17996295
Thanks, CEHJ.    The Java refresh problem may refer to Java GUI program with using Swing package.  So these two are also identical if they are in a Swing GUI program ?     I created this question after read some comments on question  http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22068478.html
0
 
LVL 23

Assisted Solution

by:Ajay-Singh
Ajay-Singh earned 600 total points
ID: 17996629
I don't think it would cause any functionality issues, except for the fact that second one (with {}) is less prone to errors
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Author Comment

by:owenli27
ID: 17996879
Thanks for reply.  Ajay-Singh.
 
>> except for the fact that second one (with {}) is less prone to errors
Any possible errors it could happen if it is without {}?  Since I coded many Java program without using {} for one-line code. And they were released in production now and I am a little bit of worry this issue.

Owen
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 800 total points
ID: 17997036
If you want to do it right, type:

     if (quantity > 1000)
     {
         itemDiscount = DiscountPrice * itemPrice;
     }
     else
     {
         itemDiscount = NormalPrice * itemPrice;
     }

The one who complains about readability of the source in the future might be ....  you!

The only real problem is when people start to add lines here and there, when there is no {}

I found out the hard way ....................

;JOOP!
0
 
LVL 9

Author Comment

by:owenli27
ID: 17997468
Thanks guys.  The syntax error is not a problem for me because I depend on IBM RAD 6.0 IDE.  The other's comments  in another question is really scared me about this issue.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17997531
consider this:

if(a)
    x = 10;

if somebody adds the following, the code would compile but will not run as expected

if(a);
    x = 10;
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question