Solved

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

Posted on 2006-11-22
7
195 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 58
by zero exception 10 79
Fast way to search item into Java Array (Rhino compatible) 2 46
passing enum to a method 4 47
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

738 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