There are a few situations when this is desired behavior. For example, the games on cartridge-based game consoles typically have no exit condition in their main loop, as there is no operating system for the program to exit to; the loop runs until the console is powered off.
Another example is when a module listen actions from another. It will need to listen all the time, so the listener have to listen for infinite time or until the program turn off. Like Sockets, Threads and UIComponents.
There isn't bad practice on the concept of infinite loop, but if it isn't wanted or prejudice your system's feature it can be considered, like when you create an unintentional infinite loop or lose program control for the loop.
To make the infinite loop a good practice:
- Make sure that it is a desired behavior. If it has stop condition, avoid infinite loop!
- Make it explicity with
for(;;)
or while(true)
. Avoid tautologies in expression, do it simple!
- Make it fault tolerant, rescuing expected exceptions and give to them the right treatment!
- And the most important! Make a simple infinite loop!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…