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
Solved

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

Posted on 2006-11-22
7
193 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 54
Is there a simpler dropbox system? 10 34
Notify sent to other threads in Java 9 33
American Express @Work site and Java 4 16
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

809 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