Callbacks don't have to be horrible, they are just horrible if you don't plan ahead and chain them together so deep that you can't follow the trail anymore.
I just finished a project that was all callbacks. Callbacks all over the damn place. But I designed what I think is a nice system whereby a manager object did almost all the work, and the rest of the program made requests to the manager while registering a callback. In most cases it worked like a one-time event. In a few cases, it resulted in a chaining of callbacks but only when absolutely necessary. So I didn't eliminate the problem, but I definitely minimized it.
But thinking back to that project, the benefits we got from using them far outweighed the drawbacks. There are many examples, but for one we were able to completely avoid using coroutines and could include a crucial stop/start mechanism to the whole thing simply by pausing the time loop in the manager.
Callbacks don't have to be horrible, they are just horrible if you don't plan ahead and chain them together so deep that you can't follow the trail anymore.
I agree with you here, but you can apply the same argument for a lot of practices, including (and I'm sincerely not trying to provoke anyone), gotos.
The lesson here is that even in the case of something that's widely considered a universally bad idea, there are still times when it can be useful if used carefully and correctly.
But they aren't a bad idea just because they are widely considered to be. In fact, they aren't even a bad idea in the first place. People just copy paste some else's misunderstanding of a famous programmer and it gets out of hand.
Nothing about goto is any worse than any other form of jumping. For example, all conditionals use goto and yet hardly anyone complains about them (some still do because there are always silly people around). It's just syntactic sugar to type if instead of goto, but all of a sudden people are fine with renaming goto?
The perceived problems possible with goto are the same problems with callbacks, yet people will defend callbacks because they're spelled differently. This really is the only difference, semantically they have the same core issue: jumping to random places and dealing with the fallout.
Both have the same solution also: just don't do that.
but this actually proves the point of the article. good software development uses good abstractions.
the return statement is a good abstraction.
instead of
function {
...
if (foo)
goto functionexit;
...
functionexit:
}
we have a language feature called the return statement.
the while statement is also an implicit goto. basically everything is a goto, because lots of statements get compiled to jmp ...
the problem with unstructured callbacks is that they are not a good abstraction. you should attempt to solve the problem at a higher level, to make code easier to inspect.
I never thought of it this way, but you're right: a break is like a semantic goto. You're not going to a specific line of code but rather to a place where something else is being done.
Block structured programming was designed to eliminate this kind of thing, with very good reason. I find it quite distasteful that it has become so prevalent in much of the code I see today.
Many reasons, all well documented by such luminaries as Hoare, Dijkstra, Wirth and others. My own favorites include
Harder to argue about correctness of a program
Harder to reuse stuff (that 'something' may not always be a valid test, depending on what the code is being used for)
Harder to debug because there are too many exit points of which to keep track
Harder to read and understand. You can't develop a high level abstract description because you always have to drill down in case there's something that causes the code to exit (jump) unexpectedly. It's like trying to understand biology using quantum mechanics description instead of cells!
I don't find either one more difficult to read or understand. And I'm not sure why it makes it harder for "foo" to be re-used. I'm a professional software engineer working in computer vision, and I see and write things like
if(img.width == 0) { return error; }
pretty frequently, and I don't think that I've had an issue building an abstraction when debugging code because of an early exit. The linux kernel has many such instances. And there may be many different exit points, but once you've moved past one in the code execution, if it's not in a loop, you don't need to worry about it anymore. Maybe I am misunderstanding the idiom that you have a problem with. Could you expand on it?
An argument could be given that this "if(arg > 10) return;" is an assertion, and part of the function's definition. Then the argument shifts to how you should define your functions.
I agree, with both parts (and the GP). I'll admit to writing tons of gotos (or equivalent) in my life; most of them were undoubtedly 'Dijkstra gotos', but I'm sure there were some that I would still defend now as 'the right way to solve that problem'.
I couldn't agree more. I've seen 'goto' used intelligently and I've seen Callbacks used intelligently. I've also seen the less than intelligent solutions. CallbackHell, JarHell, DllHell. It doesn't matter what it is: it is a symptom of bad/lazy design and project management, not the tool.
It seems to me that if you're using an anonymous function (or pointer) to call another function to call another you're probably doing it wrong. It doesn't matter if it is classes, goto's or callbacks. It is just bad design at the root.
TL;DR; Blame the developers, not the tools. (And yes, I like using Callbacks in my frameworks, they are just as simple an abstraction as an "ActionListener")
Yes, I mean, tools have the reputation they do for a reason. Tools with bad reputations can perhaps have some good uses, the reputation just means that using them should be taken with a grain of salt. When people are informed that careless use of goto leads to difficult to understand spaghetti code, they should take that into account. When you use goto, you need to take on an extra level of responsibility to thoroughly plan and make sure you understand what you're doing. But that's one more thing I have to plan for and worry about, and goto just adds another layer on top that largely needless, so I mostly ignore it.
If you ignored the warning and it turned into spaghetti code - then, yes, that's your fault. But if you'd never been told, I can't hold you to the same level of responsibility. This is what education is for. A mindless slogan like "blame the developers, not the tools" ignores subtleties. I mean, sure, it's my responsibility, not the guns, when I walk around with the safety off and blow someones head off. Does this somehow mean that a gun with the safety off has an undeserved reputation? God no!
This is pretty much what this author claims to be inventing. Monads and Closures are well known solutions to the "callback problem", and have their own well known advantages and disadvantages.
I get it, people love functional programming. It's trendy. But really, these things are ancient, we've known about them for decades, and we're still doing callback driven programming. There is no magical always-right solution.
This is pretty much what this author claims to be inventing.
No, not exactly. What I present here is merely syntactic sugar to make callback programming more readable; the programmer should still perceive this as just plain callback programming.
The author is instead supplying a completely different mental model: signals and FRP. Both my approach (not that I came up with i; it is indeed quite old and well-known) and the author's approach (also not really his) attempt to solve the "callback problem" with a solution that is both readable and responsive. From what soviyet said about his approach, it sounds like he made an FRP-esque framework for himself (whether he knew it or not).
Would you have been forced to go though the trouble of structuring all your code around this inner manager were you not forced to write in callback style? It would be much more readable, composable and sane to write code in traditional style with implicit callbacks and have a compiler handle the boring part of plumbing the explicit callbacks for you.
We needed some way of doing what callbacks do best, which is allowing you to wait on some state change and getting alerted when that state is ready. What the manager basically did is hide most of the spaghetti inside itself, and since I wrote and was responsible for the manager, it was just one guy having to keep track of the mess, which allowed me to shield the rest of the team from it. So like I said, didn't really solve the problem, but minimized the effects of it pretty well.
If the language could handle that for me, that would of course be preferable but I have been writing code long enough to know that any new solution is going to come with its own set of problems. For example, coroutines more or less work around our particular issue except for the fact that pausing coroutines (in our case) was an extra pain in the ass that created its own set of problems, and the workaround to those problems created new ones, and so on.
So I generally take the approach of: if I can make this work while keeping it fairly readable and sane, then I'll stick with it. Its all cost/benefit analysis I suppose.
Have you looked at the disruptor pattern yet? I find it works great at replacing the single-manager/multiple-worker-threads pattern. You could pause in the same way but at the ring buffer instead of a time loop.
35
u/soviyet Nov 02 '12
Callbacks don't have to be horrible, they are just horrible if you don't plan ahead and chain them together so deep that you can't follow the trail anymore.
I just finished a project that was all callbacks. Callbacks all over the damn place. But I designed what I think is a nice system whereby a manager object did almost all the work, and the rest of the program made requests to the manager while registering a callback. In most cases it worked like a one-time event. In a few cases, it resulted in a chaining of callbacks but only when absolutely necessary. So I didn't eliminate the problem, but I definitely minimized it.
But thinking back to that project, the benefits we got from using them far outweighed the drawbacks. There are many examples, but for one we were able to completely avoid using coroutines and could include a crucial stop/start mechanism to the whole thing simply by pausing the time loop in the manager.