4 ms·
Yes, any system where you're not in control of the main loop becomes hard to debug very quickly. Flow of control should be tree-like whenever possible. Polymo
by blt 12y ago
Yes, any system where you're not in control of the main loop becomes hard to debug very quickly. Flow of control should be tree-like whenever possible.
Polymorphic virtual methods are also hard to debug because you must figure out what type the object actually is. You must either run the code with breakpoints, or run it mentally in your head.
Don't give up on knowing exactly who's in control and what the data structures look like unless there is a clear benefit.
With heavy OOP in C++, you often end up with code that knows nothing about the data except a heap pointer and a pointer to a virtual method table. Not only does this make debugging a pain, it kills almost every opportunity for compiler optimization.
Of course, this pattern can be really useful. But it should be selected as a very deliberate design process, not "Oh, we put everything behind interfaces because I heard it's good."
The only place where the pattern is really, truly necessary is when a library needs to call user-supplied methods on user-supplied data. In C this looks like:
// library.h
void set_callback(void (*myfunc)(void *, int), void *);
// me.c
struct S { ... };
void update_s(void *s, int new_value); // must cast to struct S *
struct S s;
set_callback(&update_s, &s);
This is ugly but it makes it clear how much information is being lost in such a pattern.