Solved

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

Posted on 2006-11-22
7
191 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
7 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 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 150 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 200 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

776 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