I've noticed that a big part of my code is built around callbacks. Is this considered a "design flaw"? Are there better design patterns that I should follow?
Not at all. In fact, it's a buzzword-level hot design practice: "dependency injection".
I guess you could see the observer pattern as something that could be used akin to callbacks. Have you checked them out?
In the book Pragmatic Programmer, they mention the example of a person waiting to be boarded on a flight. Instead of that person asking the check-in desk constantly whether it's okay for her to go on board (polling), the check-in desk announces publicly to all those interested when the flight is ready.
A pseudocode for this example might look something like this:
class Clerk implements CheckInNotifyer {
BunchOfObservers observers = new Bunch();
public void addObserver(CheckInObserver observer) {
observers.add(observer);
}
private void notifyListeners() {
observers.all.notifyCheckIn(new CheckInEvent());
}
}
class Passenger implements CheckInObserver {
public void notifyCheckIn(CheckInEvent event) {
event.getPlane().board();
}
}
class WaitingArea {
public init() {
Passenger passenger = new Passenger();
Clerk clerk = new Clerk();
clerk.addObserver(passenger);
}
}
Another option that works especially well when you have a number of procedures with callbacks going at once is to use WaitHandles.
it's not bad, it's just the only way to do functional programming in non-functional languages.
It depends. For Javascript, I prefer the observer pattern for anything that can actually be aware of a fired event - mainly because you can register and manage multiple observers. But for something like an AJAX call, wrapping it in a closure and including the callback method seems like the cleanest approach.
In an object oriented language I find polymorphism a much 'cleaner' (no casting and no messy function type declarations) and a more object oriented method (deriving from an interface) to achieve the functionallity provided by callbacks.