6

Someone just explain me why. I really can't figure other reason that beeing too lazy to think a condition while writing a "while loop".

At university, a colleague asked me what i think of his code. I see something like that:

while ( 1 ){
...
if (condition)
break;
...
}

Am I a fucking genius thinking that just moving condition into while will improve readability, or am i a fucking stupid ? Really not the first time seeing this, maybe i am missing something.

Comments
  • 4
    Thats really dumb.

    What happens if the condition never triggers? You've just locked your application.
  • 1
    No its not genius. the code is fine as is. if you were to move the condition up in the while the entire block would only execute when the condition happens. let's say your condition is a button press. would still (kinda) work but as soon as you introduce more conditions let's say another key press you listen for. youre not only breaking the concept of code being modular as well as keeping code maintainable. i always like to think of while (1)s as very top level structures. they should be kept "clean" and neutral. for example you have 4 different complicated conditions. putting that into your while would make it slow, unreadable and unmaintanable.

    Do some research on SOLID.
  • 2
    @beggarboy I really don't get your point. Obviously i meant to put the negate (!) into the while(). If you need to add conditions just build an expression and put it into the while.
    If there is an enormous number of conditions, put into while loop a function which return a boolean based on the conditions.
    I think is better than << Ok now i have to read all the while loop body to understand when it stops >>
    And i also think that nesting while(1) into while(1) with breaks at more levels of depth, is just hell.
    But again, i think i'm missing something because i see this style more often than i expected to.
  • 1
    @toro2k
    What? Why would you have a while loop, or an if statement if the condition is always true?

    Why would you intentionally write MORE code that does nothing?

    The fuck?
Add Comment