Earlier quoted context omitted.
I often write synchronous methods that include control flow that nests three deep (say try/finally, if/then/else, and a for loop). Often it's easier to read this code than it would be if everything were split out into separate named methods. Why would the same not be true of asynchronous methods, assuming that the technology was there to enable it (as it is in C#)?
> Often it's easier to read this code than it would be if everything were split out into separate named methods. This might actually be a shortcoming of our code organization/reading tools.
Callbacks as our Generation's Goto Statement
281–287 of 287 posts
Re: Callbacks as our Generation's Goto Statement
#282This is what continuations are for.
Continuations don't help with the problem that the visual structure of callback-oriented programs doesn't reflect the order of execution. As a heavy JS programmer, that's the most compelling point for me in this post.
Re: Callbacks as our Generation's Goto Statement
#283Await should not be required - it should be more like...
--
regularWork(); //im waiting till this thing is done
driveHome();// not executed till thing one is done
background orderStatus = orderPizza();
turnOnXbox();
while(orderStatus == 'not ready') {
playXbox();
}
turnOffXbox();
eat();
--
Like I said - just my humble opinion that the code written would become more expressive.
Re: Callbacks as our Generation's Goto Statement
#284Earlier quoted context omitted.
> Often it's easier to read this code than it would be if everything were split out into separate named methods. This might actually be a shortcoming of our code organization/reading tools.
Why should that be the case? Creating all the extra methods is going to create lots of new points of indirection, the new methods are likely to be tightly coupled anyway and breaking the nesting might mean you have to hoist a bunch of variables into an outer scope.
Re: Callbacks as our Generation's Goto Statement
#285Earlier quoted context omitted.
Another example: a lot of old code bases use "goto" for error handling. I'm assuming the Linux kernel still does this. Now we've formalized that behavior into exceptions.
You need C++'s RAII idiom or go's defer syntax to get the same behaviour though. I think try-with-resource in Java also would do the same thing, maybe. Just plainly throwing an exception won't release what you've acquired.
Re: Callbacks as our Generation's Goto Statement
#286Earlier quoted context omitted.
What if you have two buttons, search and 'be lucky'? I guess you will have to await for a controller object that wakes up that if either one of the buttons is triggered, and then test what button was pressed. Now what if you have add a search option dialog that can be invoked at any moment? I guess when the 'apply' button of the option dialog got pressed then this is still going to be a callback that sets global vari…
"What if you have two buttons, search and 'be lucky'? I guess you will have to await for a controller object that wakes up that if either one of the buttons is triggered, and then test what button was pressed." http://msdn.microsoft.com/en-us/library/hh194796.aspx
Re: Callbacks as our Generation's Goto Statement
#287Earlier quoted context omitted.
Yeah, C# await/async is cool. But isn't Google Go's approach nicer? Keep standard lib calls blocking as is, and use the "go" statement when you want to run something async? It avoids all the extra DoWhateverAsync() API functions. See http://stackoverflow.com/a/7480033/68707 What's the Go equivalent of "await"? I.e., like the "go" statement but asynchronously wait for the function to return and get its value?
ci := make(chan int) go func(ci)