Results 1 to 14 of 14
  1. #1
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Default Best practice of IF loop with NOT

    Hi,

    Please suggest me the best practice of using if loop with 'NOT' condition.

    For example:
    if (rs != null){rs.close();}

    if (rs == null){} else{rs.close();}

    Which of the above is optimised?

    Thanks in advance

    Sathish

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    depends on compiler but you probably won't notice any difference in speed even if you run it a trillion times.
    My IP address is 127.0.0.1

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    In most of the cases, each line of a code effect to the execution completion time. If you can remove at least one line of code, it's much better.

  4. #4
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    This true in only some case Eranga. Think of inlining your methods although this adds more code it also makes your program faster because of the lack of method calls. Another example would be to hardcode in a inner loop for an array search.
    My IP address is 127.0.0.1

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, I mentioned on my replay pal. Please read what I have written there carefully before comments on it.

    And you know what, array search is much faster than calling a method. If you not believe me do a simple JUnit test and see.

  6. #6
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I'm confused by what you mean. Arrays were never mentioned in this thread nor where method calls
    My IP address is 127.0.0.1

  7. #7
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    I do agree that the execution time between them is negligible, but it is just good practice to use the not statement. You don't want to have an empty block. It may have to go into the block and then see that it is empty which would take time, but compilers should optimize around that.

    I would think that it would be faster to use a try/catch block on the rs.close, then it wouldnt have to do anything extra unless the file was null which i am assuming isnt going to happen very often.

  8. #8
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Most compilers will optimsize your if statment. Mostly what you want to consider is readability.
    My IP address is 127.0.0.1

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Zosden View Post
    Think of inlining your methods although this adds more code it also makes your program faster because of the lack of method calls. Another example would be to hardcode in a inner loop for an array search.
    If so I'm misunderstand on this.

  10. #10
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Please suggest me the best practice of using if loop with 'NOT' condition.

    For example:
    if (rs != null){rs.close();}

    if (rs == null){} else{rs.close();}

    Which of the above is optimised?
    You may code it in favor to what you are expecting....

    The first one may be faster than the second one, only if that portion always executes successfully and should be close,

    What if rs has 51% chance to be null? therefore the second one is faster than the first one....
    Last edited by sukatoa; 05-28-2008 at 12:52 PM.
    freedom exists in the world of ideas

  11. #11
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I do believe but I could be wrong that with a smart compiler it would take the exact amount of time either way.
    My IP address is 127.0.0.1

  12. #12
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    I agree. I would expect the java compiler to be smart enough to get those both compiled well. This would be different with an interpreted language.

  13. #13
    gogoc is offline Member
    Join Date
    May 2008
    Posts
    21
    Rep Power
    0

    Default

    it is best to avoid "NOT" other then "NOT EQUALS" as it will take an extra conversion to convert the result to its not
    and more over try to inscribe less executed code int the loop if u are going for multiple condition and using && in between vice-versa
    get new coding problems at CODERWORDS

  14. #14
    superwaxer is offline Member
    Join Date
    May 2008
    Posts
    7
    Rep Power
    0

    Default

    Its always good programming practice to use the:

    if (rs != null){rs.close();}

    :)

Similar Threads

  1. I need help on a practice code for java
    By Sageinquisitor in forum New To Java
    Replies: 12
    Last Post: 07-13-2010, 05:00 PM
  2. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  3. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:44 PM
  4. Building small web application in java for practice.
    By Saurabh321 in forum New To Java
    Replies: 1
    Last Post: 02-01-2008, 04:38 PM
  5. Web Services - IBM Expands SOA Management Practice
    By Felissa in forum Web Frameworks
    Replies: 0
    Last Post: 06-25-2007, 05:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •