youngoat (youngoat) wrote,
youngoat
youngoat

  • Mood:
  • Music:

Coding Poll: Which version of the loop do you like best?

[Tried to do this as an actual poll, but apparently you can't put HTML in polls.]

Whenever I have to write this type of loop, it drives me crazy. I don't like any of the options. Which version do you prefer?

char *s;
// A
do
{
    s = read_from_file();
    if (s != NULL)
    {
        // whatever
    }
}
while (s != NULL); 

// B
s = read_from_file();
while (s != NULL)
{
    // whatever
    s = read_from_file();
}

// C
while ( (s = read_from_file()) != NULL )
{
    // whatever
}

// D
for(  s = read_from_file()
    ; s != NULL
    ; s = read_from_file() )
{
    // whatever
}

// E
loop:
    s = read_from_file();
    if (s != NULL)
    {
        // whatever
        goto loop;
    }

I dislike all of them for different reasons. I think I usually use A because semantically do/while seems closest to what I want. C is really tempting. It's definitely the most compact, and the pieces are in the right order... but I really hate sticking an assignment in the loop conditional. E is actually kinda' clean, but good luck getting a "goto" past code review! ;-]

And yeah, I know I'm probably a freak for caring about this...
Tags: code, programming
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 14 comments