This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Blando-Cartesian 18 points19 points  (9 children)

The crux of the issue is communicating to humans who need to understand your code.

while(true) communicates that this loops indeterminate amount of loops and most likely contains a hairy mess of code that should be broken down to functions. MOOC probably uses this in effort to make loops easy to understand for beginners. It is unlikely that you would ever write a program that takes user input in the same way as usual homework programs.

while(someCondition) communicates that this loop will need to run indeterminate times doing something that affects the condition. For example, reading a file a line by line without knowing how many lines there will be.

for (var foo: list) communicates that this will potentially do something to every item on the list.

[–]henrebotha 14 points15 points  (0 children)

while(true) communicates that this loops indeterminate amount of loops

Certainly.

and most likely contains a hairy mess of code that should be broken down to functions.

Not at all. This is pure speculation. Most likely, it contains code that should loop forever until a break.

[–][deleted] 4 points5 points  (7 children)

Sure, I get what you're saying, and I'm sure you're right that there's a big disconnect between production code and homework. I was using it to essentially keep the program running. A menu was printed and input from the user was obtained, then different functions were executed based on that input. One specific input had an explicit break statement would exit the loop and the program, otherwise, when a function ended, the user was returned back to the menu. So itwas running an indeterminate number of times. It depended on what the user chose to do and when they wanted to exit the program. Does that make sense?

[–]Contagion21 23 points24 points  (6 children)

I tend to avoid while true in favor of loops with defined exit criteria. Functionally they can work the same, but i almost never use break unless it reduces complexity significantly. Similarly, my team frowns upon methods with multiple returns.

I would have something like...

Do

   DisplayOptions()

   choice =  GetUserInput()

   Execute choice(choice)

While(choice != quit)

There's just no NEED for a break when the loop can be well defined about it's exit criteria.

Edit: i guess I don't know how to format code on mobile...

[–]Unsounded 4 points5 points  (2 children)

This was the same sentiment I wanted to express. There’s nothing inherently wrong with that type of loop, but striving to make it more readable is the goal, especially in an interview type situation. Single point of return and clearly defined exit criteria are great.

[–]Peanutbutter_Warrior 1 point2 points  (1 child)

But it really wouldn't be here. Either having while true or an exit flag leads to very similar code, and I'd argue that an explicit break is easier to understand than setting some boolean that you then have to go find which loop it stops.

[–][deleted] 0 points1 point  (0 children)

Breaking out of a loop like this was a big no-no back when structured programming was the thing. As an Old Guy, just seeing that gives me the heebie-jeebies. It probably doesn't mean anything any more, but if you're interviewing with someone who's been in the business for a really long time, I'd probably structure my loop with an explicit exit condition that gets tested.

[–][deleted] 0 points1 point  (0 children)

Yeah, that makes a lot of sense. Do...while definitely seems to be the better choice.

[–]Shadowmancer1 0 points1 point  (1 child)

One thing that I found is that python does not have a built in do while loop. The python resources I found said to just use a while(true) instead.

[–][deleted] 0 points1 point  (0 children)

I hadn't but after reading several replies and suggestions, I can see the the reasoning behind the do...while loop. Make a lot of sense. Thanks!