Solved

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

Posted on 2006-11-22
7
189 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now