There are several situations where this is the desired behavior. For example, games on cartridge-based game consoles usually do not have exit conditions in their main cycle, since there is no operating system for the program to exit; the loop runs until the console shuts down.
Another example is when a module listens for actions from another. He will need to listen all the time, so the listener must listen to infinite time or until the program shuts down. Like sockets, threads, and UIComponents.
There is no bad practice in the concept of an infinite loop, but if it is not needed or does not harm your system function, it can be considered, for example, when you create an unintended infinite loop or lose program control for a loop.
To make an endless loop a good practice:
- Make sure this is the desired behavior. If it has a stop condition, avoid an infinite loop!
- Do this with
for(;;) or while(true) . Avoid tautology in expression, do it easy! - Make it fault tolerant, save the expected exceptions and give them the right treatment!
- And the most important thing! Make the endless loop simple !
source share