Is it good programming practice to write a while with true argument so that it runs endlessly?

Asked

Viewed 216 times

1

In a hypothetical situation: Is it good programming practice to write a while that way? So that it continues to run endlessly?

    while(true){
    System.out.println("Por favor, Escolha: ");
    System.out.println("1: Adicionar Cliente");
    System.out.println("2: Depositar Dinheiro");
    System.out.println("3: Retirar Dinheiro");
    System.out.println("4: Checar Conta");
    System.out.println("5: Calcular Valor");
    System.out.println("6: Sair");
    }
  • 1

    Ta missing a read (user input) ai :P

  • 1

    The question is whether a while (true) has a valid case or if this case is exactly valid?

  • Yeah, I’d generally like to know if at any point, I need to do a while that way, if it’s wrong?

  • Just looking at this code becomes difficult, with the user input and if any of the methods called by the numbers kill the application or call the menu again is up to reasonable

  • I edited the question for a hypothetical situation, I had not specified before. Thank you.

  • I get it, I’ll post the whole code. Thank you.

  • 2

    For this situation there does not make sense, the loop will be infinite with nothing to change this condition.

  • 2

    For the situation of your question, the while would make sense if it were while (escolha != 6), for example. The use of while/do-while does not hurt any best Practice. You should use it whenever you want something to run at least once, and not know the number of times it should repeat.

  • 5

    This is one of the most typical cases where good practice is of no use. In this example shown it is clear that you should not do this. In another example, it’s the right thing to do.

  • I understand, guys, thank you so much. I’m still adapting, trying to understand as a whole for later when I need it, I really make sure I can use it or not. ;)

  • I can do while that way as long as I have some way that it stops at some point... It’s because I’ve never seen a while being done with just (true) and got in doubt.

  • 3

    As others have cited above, while(true) is not a bad practice if there is a condition to break this loop at some point, games for example use while(true) in a thread to work the movement of objects and other information...

  • 2

    If you can avoid it, it’s better because the code gets harder to read. Not in this example, because it is very simple, now imagine more complex codes, with some conditions of stops using break inside the repetition, if not very well structured it is difficult to give maintenance.

  • 4

    I recommend caution with the term "good practice", as this is usually used to propagate ideas "canned" from those who do not master what they are doing, or who do not want to take the trouble to explain (usually the first case, see many teachers who have never used programming for anything serious that was not only theoretical, and sell these ready-made recipes), so I recommend that you do not buy this idea when they try to apply this terminology to you, and charge a more serious explanation. Everything in programming has to have a technical reason to be done, and "good practice" is not a true reason

  • 2

    Related subject http://answall.com/questions/104376/70

  • So it’s not good to use the term good practice because in programming a good practice would be a code well done? Be it, the way you do it... There’s no recipe... That would be it?

Show 11 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.