|
Works better than any break point...
|
|
|
|
|
const DucksFloat = true;
:
:
while(DucksFloat) {
}
Nothing succeeds like a budgie without teeth.
|
|
|
|
|
|
The problem with any remark, comment, oneliner, if it gets overused, a number of things happen
- they get a life of their own. Everybody starts to parrot this "wisdom" because of how good it sounds
- the use of the term gets disconnected from the original meaning, because it is an easy, and thus lazy comment to make
- instead of being helpful, or any kind of contribution to the quality and ethics of software engineering, it becomes the catchphrase of choice for individuals who want their opinion to be taken for "superior" at all cost.
What everybody *should* have done instead, is work out why, or why not, they are using a particular construct, and show real professionalism that way, rather than faking it through gratuitous remarks that sit well with the boss.
|
|
|
|
|
for (;;)
{
Console.WriteLine("this, and while(true) loops, are an abomination ... as evil as using goto");
goto get_me_out_of_here;
}
get_me_out_of_here:
Now you have it all
|
|
|
|
|
#define ever (;;) would allow you to write
for ever {...}
|
|
|
|
|
In the development phase while(true) is fine and clear. When you are clear as to what conditions must be met to break out of the loop, we can set up a meaningful boolean eg while(NoReliablStatus) or whatever.
Replacing while(true) with while(notdone) is a waste of time.
|
|
|
|
|
What about embedded systems? A lot of embedded systems have initialisation code and then the remainder of the system is handled in a single while loop, that's more or less standard practise
|
|
|
|
|
When I need an infinite loop, I do it accidentally.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
It's probably been said, but try to do embedded software with out infinite loops. The only time I have seen goto used in the wild was in a avionics control software!
|
|
|
|
|
while (true)
{
if (isPolitican) break;
}
|
|
|
|
|
Once I found in the company's common "utility" .H file, this line:
#define ever (;;)
so you could write:
for ever { ....}
But, all of this ignores one basic truth of code: ALL LOOPS END! -- one way or another. Somewhere buried in your code is something along the lines of:
if (realExitCondition)
break;
so you might as well put it into the while() .
Truth,
James
|
|
|
|
|
James Curran wrote: But, all of this ignores one basic truth of code: ALL LOOPS END! Because no device with embedded code will last forever. So you could include some sort of "while (this device is not being decomposed into its constituents for recycling purposes) {...}". The question is how the device can perform this test, and take the proper actions to terminate the loop.
Lots of embedded infinite loops won't even survive a change of battery. Yet the problem is the same: A test like "while (battery power is available) {...}" has a fairly low probablity of being able to perform a loop exit.
Larger systems, e.g. running databases, may have UPS systems that allow them to do a controlled shutdown, such as to write in-memory logs to stable storage. Lots of servers, both web servers and other kinds of servers, are stateless and have no data to save between requests. They sit waiting for a request, process it, and sit down waiting for the next request. There is nothing to do if the machine is turned off, the process is forcefully terminated, or a power outage occurs. So why should they have a loop exit handling? It has no meaning. Their purpose is to run indefinitely. If it stops, it stops within its loop.
|
|
|
|
|
Quote: Because no device with embedded code will last forever. So you could include some sort of "while (this device is not being decomposed into its constituents for recycling purposes) {...}". The question is how the device can perform this test, and take the proper actions to terminate the loop.
Even the most standard loop (for(int i=0; i < str.Length; ++i) ) will not survive a power cut, so that's not the case we are interested in.
Most embedded systems still have a "power" switch, which is not hard-wired to the power, but instead, starts the "power-down" procedure. Hence:
while (!shutting_down()) {}
Truth,
James
|
|
|
|
|
James Curran wrote: Most embedded systems still have a "power" switch, which is not hard-wired to the power, but instead, starts the "power-down" procedure What do you plan to to in this omnipresent "power-down" procedure when there is nothing to save or cleanup? If your code runs multiple threads, each executiong an infinite loop (which is quite common in embedded code), will you broadcast a 'power-down' message to each of these loops and let them all take various terminating actions? Conceptually, almost all interrupt handlers are infinite loops, making another iteration when an interrupt is received. Will you signal the 'power-down' to each handler as well, for them to do their termination handling?
If there are cleanup actions to be done for one, or possibly a couple, of the threads, you should of course send these theads a termination request for orderly shutdown. But lots of embedded threads, or even complete embedded systems, have no cleanup requirements. They have no need for any 'shutting down' signal, but can be cut off without formalities, just like when power disappears.
The CHILL language was specifically designed for embedded systems (specifically: phone switches), and it provided an explicit loop mechanism: 'for ever do ...'. Phone switches are also illustrating that the system as a whole is designed for never terminating. If a phone switch stops running, it is due to an error or other exceptional condition; the code design assumes perpetual running.
|
|
|
|
|
I once wrote the following code:
var HellFreezesOver = false
do until(HellFreezesOver)
// code
loop
The language I was working in didn't have the concept of an infinite loop and I needed one for this application. Of course, the application terminated when the Red Sox won the World Series in 2004.
|
|
|
|
|
My variant of the same was "WW3".
|
|
|
|
|
READY.
10 PRINT "HELLO"
20 GOTO 10
RUN
|
|
|
|
|
A question from a CP user last night involved how he should go about making a portion of an app multithreaded.
So I helped him through it at least somewhat.
After that I find out he doesn't know how to spin a loop with no ending condition in C#. while(true) {}
And now I feel like a gave a child a loaded pistol
Real programmers use butterflies
|
|
|
|
|
Yeah ... I get the feeling that one has lied his way into a job he has no idea how to do and is desperately trying to cover up his ignorance. His comments aren't confidence inspiring.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Now he should be able to shoot himself in the foot multiple times, from multiple threads.
Tomorrow his questions will be about Thread Starvation and Deadlock, but all he will know is that the UI is frozen.
"Time flies like an arrow. Fruit flies like a banana."
|
|
|
|
|
while (true)
{
new Thread(() =>
{
}).Start();
} Am I doing this right?
|
|
|
|
|
I'd come after you for that. You're not the only one with a pitchfork - I collect them from villagers.
Real programmers use butterflies
|
|
|
|
|
Alright, how about I optimize that using the faster and lightweight TPL?
Lightweight threads from the threadpool, or, if possible, just time slice and save the overhead from starting up a new thread altogether!
while (true)
{
Task.Factory.StartNew(() => {
});
} All fixed
|
|
|
|
|
Unless you need to pass in a custom scheduler or something like that you really should use Task.Run()
While TaskFactory.StartNew() isn't obsolete or anything, the overload you're using has been supplanted by Task.Run()
I think I read that from Stephen Toub, but it could have been someone else at the Microsoft blogs.
Just sayin'
*hides*
Real programmers use butterflies
|
|
|
|