• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

If-else vs Not-If

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


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

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will not this solve the purpose

 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing is good in your logic is resuibility



Correct
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic