Hacker News new | ask | show | jobs
by TorKlingberg 5096 days ago
The second solution looks like a good old callback to me. The other two are just complicating things with additional state.

In general, I try to avoid function pointers i C unless I am sure it makes the code clearer. It is often difficult to debug code that tries to be clever with them. Like preprocessor macros they are powerful, but tempting to misuse.

1 comments

Good old callback, exactly what it is! In a very simple case like this perhaps the additional state is complicating things, it does have it's uses is many cases though, as described in the post. I personally think function pointers doesn't make code more difficult to debug than any dependency injection used in many of the higher level programming languages as Java. It is usually the main argument against dependency injection as well. Usually it goes; Since it makes it harder to debug I'd rather make it more coupled. When it's more coupled it's harder to unit test. Since it's harder to unit test, I need to debug more, etc etc. Personally I'd rather break that chain early. In the end, debugging code with function pointers (or dependency injection) gets easier with time.