aspose file tools*
The moose likes Java in General and the fly likes If-else vs Not-If Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "If-else vs Not-If" Watch "If-else vs Not-If" New topic
Author

If-else vs Not-If

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Please find following code:



Versus



In First, i have placed condition in If and if , if is not satisfied then Else fired
In second also, if "if statement" not true then code below will executed

is there any technical difference or some sort of standard difference which one must follow?
Which of above two approach is fine?


Regards
Azrael Noor
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2627
    
    9

Hello

The question you should ask yourself is: Why do I need to use an "if statement" to check whether there is anything in the result set?

In fact if your purpose is to get that boolean as a separate function then you can just do:


However if you want to do something with your result set then


The whole idea is that if the result set has no records, it will exit the while loop.

Your current approach is somewhat making the logic more complicated and difficult to understand.

Hope this helps.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Azrael Noor wrote:
is there any technical difference or some sort of standard difference which one must follow?
Which of above two approach is fine?

In general, besides the fact that your code gets more complicated, there isn't a big difference. If you have an if that returns from method when it's evaluated to true, there is no need to put else after it, because execution will not continue. On the other hand, if it's not evaluated to true, the execution will proceed to the next statement after if block, hence you don't need to explicitly add else statement. E.g.

The difference is only when you need to check for more conditions, which is the situation where you use else if.

However, for your particular problem, the solution K. Tsang provided is the best.


The quieter you are, the more you are able to hear.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
K. Tsang wrote:Hello
The whole idea is that if the result set has no records, it will exit the while loop.
Your current approach is somewhat making the logic more complicated and difficult to understand.
Hope this helps.


My current approach is


-------------

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Azrael Noor wrote:My current approach is

Which doesn't help us much, because your checkRequestExist() method doesn't actually call myfunc().

My suggestion would be to back up and explain, in English, what you're trying to do.

ResultSet is an awkward beast, mainly because you have no direct access to a row.
It also doesn't have a hasNext() method, which in my view is criminal after all this time; and if that's what you're trying to emulate, the answer is: you probably can't (or if you do it will be very tortuous), just as you can't determine the size of a ResultSet without reading it.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
Winston Gutkowski wrote:
Azrael Noor wrote:My current approach is

Which doesn't help us much, because your checkRequestExist() method doesn't actually call myfunc().

My suggestion would be to back up and explain, in English, what you're trying to do.

ResultSet is an awkward beast, mainly because you have no direct access to a row.
It also doesn't have a hasNext() method, which in my view is criminal after all this time; and if that's what you're trying to emulate, the answer is: you probably can't (or if you do it will be very tortuous), just as you can't determine the size of a ResultSet without reading it.

Winston


> There is main Service Class.....
> It checks how many requests in Database.....
> it fetches out 10 requests from database
> it send data coming from database into a function which do further processing

To check how many request there in database there is checkRequestExist() function in a CLASS custom, it returns the resultset

main Service class calls Custom.checkreqyestexist()

after getting number of request it just call function present in do while loop........

that is criteria and example is same as in above post ...

hope it clarifies a bit

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

If your goal is to check if your ResultSet instance contains data after executing query over database, you can do something like this:

Actually, you are using next() method of ResultSet class for that purpose. As you can see from documentation:
Returns:
true if the new current row is valid; false if there are no more rows


So, that will do the trick, without that empty do-while that you use.
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
will not this solve the purpose

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Method hasnext() does not exist in ResultSet class.
If your goal is to check what I wrote in the previous post, I think the solution I gave you is more elegant. On the other hand:

what's the purpose of the statement above in your loop?
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
One thing is good in your logic is resuibility



Correct
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Azrael Noor wrote:> There is main Service Class.....
> It checks how many requests in Database.....
> it fetches out 10 requests from database
> it send data coming from database into a function which do further processing

To check how many request there in database there is checkRequestExist() function in a CLASS custom, it returns the resultset
hope it clarifies a bit

Not really. You cannot know how many rows will be in your ResultSet unless you:
(a) Read through it counting rows, and then run beforeFirst() (not guaranteed to work; and may create extra network traffic).
(b) Actually control the specific rows returned in your SQL statement (tricky).
(c) Return the total number of rows that will be satisfied by your SQL statement with an initial SELECT COUNT(*) (again, not guaranteed to work unless you can lock the table).

so the simplest way to deal with rows 10 at a time from a ResultSet is:Now if you want to put that in a method, fine; but I don't think it's buying you much. ResultSet is a PITA when it comes to modularization.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If-else vs Not-If