• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Coding Conventions

Status
Not open for further replies.

Rui

Rui

Level 41
Joined
Jan 7, 2005
Messages
7,550
This is a bit off-topic, but do you guys think it is okay to use “ while(true) (...) break; ”. This in Java, which is the language we've been using at uni for first year. My teacher says we should use a boolean variable instead and never while(true)break;, but the fact is that I went to Oracle's website and even saw some sample code doing this.
 
This is a bit off-topic, but do you guys think it is okay to use “ while(true) (...) break; ”. This in Java, which is the language we've been using at uni for first year. My teacher says we should use a boolean variable instead and never while(true)break;, but the fact is that I went to Oracle's website and even saw some sample code doing this.

Depends on the conventions. Normaly using break and continue is considered to be unstructured code. However, it's faster, saves memory and avoids making a lasagne of code if you need to exit a loop with a lot of code inside. So it's good, they were created to avoid using gotos.
 

Ralle

Owner
Level 79
Joined
Oct 6, 2004
Messages
10,182
Sorry Rui, I decided to move your post to a new thread. Hope that's alright.

As long as you know what you're doing, you can do what you want. People prefer that you use while(something) { ... } because then they know what you thought when you wrote it. Consider this:
JavaScript:
while (iAmStillHungry) {
  // many lines of code
}
As opposed to:
JavaScript:
while(true) {
  // many lines
  if (iAmStillHungry) {
    break;
  }
  // more lines
}
The first is eaiser to understand. As soon as you see the while you know for how long it runs. The other you have to go digging to see what this while is for.

They both work, but I suppose the first is more chronological when you read somebody else's code.
Also, if you always put the boolean in the while() you won't forget to do it. That will save you some infinite loops :D
 
Level 2
Joined
Aug 18, 2013
Messages
1
If your uni is specifically telling you to follow certain conventions, and you're getting marked on these style / programming conventions, please follow your uni's outlined conventions. They are telling you these conventions so they can mark people on their programming style / conventions equally. Also, learning how to strictly follow conventions is actually quite a useful skill to learn.

Apart from inside your schooling environment, many programmers hate while(true) loops, because the termination condition is not immediately apparent. If your code can easily be written as while(condition) , most programmers will tell you to write it that way for that exact reason. while(true) itself can certainly be useful when you need to terminate the loop in the middle of a loop, but the majority of loops simply won't ever need it.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
You can also use...
Code:
for(;;){
...
break;
...
}
as another way to do infinite loops until breaking. It is technically faster as well on worse compilers as it ignores checking the Boolean although I am sure Java probably optimizes that out as is.

Sometimes you want an infinite loop that never breaks. An example would be the main method of a micro controller where if it reached the end it forces a restart. Thus they do have a purpose and use in code.

You can also use them if you need a middle state termination. This makes a forth basic kind of while loop.

Code:
while( x ){
    //stuff
}

do{
    //stuff
} while( x );

for(;;){
    //stuff
    if( x ) break;
    //stuff
}
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
I personally prefer not to use a while(true){...}, even when there are quite some conditions to break on. Yeah, a variable named "isStillHungryOrQuitSchoolOrHasNoJobOrBurnsBooksOrLikesHardRockOrPeesOnToiletSeats" isnt nice, so I would then just make variable called "isDone" or "quit" or something like that. When you have alot of breaking conditions, just make a check for each and set that boolean according to those checks.

EDIT: Also, when there are alot of breaking conditions it should be well-documented otherwise you probably wont know what you did when reviewing your code later on, and so certainly someone else won't as well then..
 
Status
Not open for further replies.
Top