Results 1 to 10 of 10
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default [SOLVED] Is this good programming practice?

    May seem like a strange question, but my experience with java is still pretty limited. I just wanted to know if this is good programming practice:

    Java Code:
    boolean runLoop = true;
    while (runLoop = true)
    {
         WHILE BODY
    }
    The general idea is that I create some sort of arbitrary boolean value, the sole purpose of which is to start a loop.

    Is this acceptable or is it discouraged?
    Last edited by porchrat; 04-03-2009 at 05:24 PM.

  2. #2
    SaSho's Avatar
    SaSho is offline Member
    Join Date
    Dec 2008
    Location
    Ukraine, Dnepropetrovsk
    Posts
    5
    Rep Power
    0

    Default

    If it must be the infinite loop you can write only
    Java Code:
    while ( true )
    {
       //  WHILE BODY
    }

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    maybe the poster meant while(runLoop == true) and not runLoop = true? that would be acceptable code, assuming that either there is a break in the loop or a condition where runLoop = false.

  4. #4
    SaSho's Avatar
    SaSho is offline Member
    Join Date
    Dec 2008
    Location
    Ukraine, Dnepropetrovsk
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    that would be acceptable code, assuming that either there is a break in the loop or a condition where runLoop = false.
    Maybe, but if runLoop is local method variable (I've supposed it because of serial code in the first post) code inside loop have to modify runLoop to false value and later check it for break... I think it's not good.
    And poster wrote "the SOLE purpose of which is to start a loop". It means runLoop doesn't use in any another place, I think...

  5. #5
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default This is acceptable.

    I can think of two ways out of this loop.

    You can use conditional tests within this
    while loop, which if true, execute a "break".

    or

    You can place the while loop within a
    try/catch statement. I've used code like
    this to read a file:

    Java Code:
    try{
      while(true){
         //pseudo code
         read a line from a file;
      }
    catch(Exception e){}
    Translated this means,
    "Try to do this forever thingy, unless it throws
    an Exception."
    Since I have no infinitely long file, an
    exception will occur, causing try{}catch{}
    to abandon the while loop.

    The other purpose produces cleaner looking
    code for traversing linked-lists (at least I think
    it's cleaner looking, and easier to follow), but
    this is a style isssue. I'm tempted to say it
    improves code performance, but I haven't
    tested that aspect of it yet.

    I would not freak at seeing this kind of loop.

  6. #6
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    maybe the poster meant while(runLoop == true) and not runLoop = true? that would be acceptable code, assuming that either there is a break in the loop or a condition where runLoop = false.
    Sorry guys yes that is correct I meant

    Java Code:
    while (runLoop == true)
    Sorry for the confusion, it was a typo. :(

  7. #7
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    maybe the poster meant while(runLoop == true) and not runLoop = true? that would be acceptable code, assuming that either there is a break in the loop or a condition where runLoop = false.
    That is pretty much what I was asking, thanks again guys, I am learning a lot from this forum. I was planning on using a runLoop = false; statement inside an 'if' statement, but I hadn't thought of using a break;

    It seems easier to follow (for me anyway) with a runLoop = false statement instead of a break.

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    boolean runLoop = true;
    while (runLoop)
    {
        // WHILE BODY
        // runLoop may set to false
    }
    i think it is good

  9. #9
    shamsa is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    the corrected one is good runLoop==true

    goof luck ^^

  10. #10
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Thank you all again, you are a huge help. I will mark the thread solved.

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. Hello Good Morning, Good afternoon, and Good Evening
    By MrFreeweed in forum Introductions
    Replies: 3
    Last Post: 12-11-2009, 04:32 PM
  3. Best practice of IF loop with NOT
    By sathishranganathan in forum Advanced Java
    Replies: 13
    Last Post: 05-29-2008, 01:11 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:19 AM
  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
  •