Pages: [1]   Go Down
Author Topic: Coding conventions  (Read 400 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 5
Posts: 207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which of these two loops is "better"?

Code:
  while (!var)
  {
    //code
    if (something)
    {
      var = 1;
    }
  }

or

Code:
 
  while (1)
  {
    //code
    if (something)
    {
      break;
    }
  }

They both do the same thing obviously, and I can use either one for part of my code so I was just wondering if one is prefered over the other. I would guess the first one is better since break seems to have the same reputation as goto. I've had a lot a similar situations while coding in the past; Is there a place where all these coding conventions are written out?
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 118
Posts: 4338
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I personally prefer the first style and would never use the second form.

The condition(s) for the loop to end are set out at the very beginning, which makes it easier to read through the loop looking for the exit condition(s) to be met rather than looking for a break.  The names of the variable(s) used in the condition(s) is also a great help in understanding the code.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

They don't do exactly the same thing necessarily.  The second form is more like a do...while() loop, whereas the first is more conventional.  The first may not execute at all depending upon "var".  The second will always execute one iteration.  They may work the same for you now, but they are not truly "the same".
Logged

Experience, it's what you get when you were expecting something else.

Offline Offline
God Member
*****
Karma: 25
Posts: 529
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I only use the first form if var is already declared and being used for something else.  I have a hard time creating a variable whose only reason for existence is as a flag to set to break out of a loop.  UKHeliBob has a point about only having one exit from a loop, but then I have to scan the code anyway to see where the flag got set.

I use the bottom form because it is usually more efficient, and I use for (;smiley-wink  instead of while (1) because I like the way it looks.

If I have to break two levels out of nested loops, sometimes I will create a function and use return.

I usually value efficiency over readability.  It doesn't mean it's "better", it's just what I prefer.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 118
Posts: 4338
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have a hard time creating a variable whose only reason for existence is as a flag to set to break out of a loop.
If you need to create the variable specifically as a means of exiting the while loop then you almost certainly don't need a while loop at all.  The whole point of a while is surely that the code inside it is going to change its control variable to another value, and that in the meantime the code should be executed repeatedly.

To clarify what I said in my earlier post.  I use the first form but would not create a variable specifically to use it.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not:
Code:
while(!something){

}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would only use the break approach if it made the code simpler. For example, if the logical design provided several reasons for terminating the loop but some of them were exceptional, I might use breaks to handle the exceptional termination so that the normal control logic was more obvious. Or I might choose to break out of the middle of the loop if it enabled me to structure the code inside the loop to avoid duplication. These are pretty rare situations though, and I wouldn't use break to terminate the loop unless there was a compelling reason.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: