Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been passing callbacks or just triggering the functions from other function in my programs to make things happen once tasks complete. When something finishes, I trigger the function directly:

var ground = 'clean';

function shovelSnow(){
    console.log("Cleaning Snow");
    ground = 'clean';
}

function makeItSnow(){
    console.log("It's snowing");
    ground = 'snowy';
    shovelSnow();
}

But I've read about many different strategies in programming, and one that I understand to be powerful, but have not yet practiced, is event-based (I think a method I read about was called "sub-pub"):

var ground = 'clean';

function shovelSnow(){
    console.log("Cleaning Snow");
    ground = 'clean';
}

function makeItSnow(){
    console.log("It's snowing");
    ground = 'snowy';
    $(document).trigger('snow');
}

$(document).bind('snow', shovelSnow);

I'd like to understand the objective, strengths, and weaknesses of event-based programming vs just calling all of your functions from within other functions. In which programming situations does event-based programming make sense to use?

share|improve this question
1  
As an aside, you can just use $(document).bind('snow', shovelShow). No need to wrap it in an anonymous function. –  Karl Bielefeldt 6 hours ago

2 Answers 2

up vote 6 down vote accepted

In an Event-based application the concept of Event Listeners will give you the ability to write even more Loosely Coupled applications.

For example a third-party module or plug-in can delete a record from the database and then trigger the receordDeleted event and leave the rest to the event listeners to do their job. Everything will work fine, even though the triggering module doesn't even know who's listening for this particular event or what should happen next.

share|improve this answer

Event-based programming is used when the program does not control the sequence of events that it performs. Instead, program flow is directed by an outside process such as a user (e.g. GUI), another system (e.g. client/server), or another process (e.g. RPC).

For example, a batch processing script knows what it needs to do so it just does it. It is not event-based.

A word processor sits there and waits for the user to start typing. Keypresses are events that trigger functionality to update the internal document buffer. The program cannot know what you want to type, so it must be event-driven.

Most GUI programs are event-driven because they are built around user interaction. However, event-based programs are not limited to GUIs, that is simply the most familiar example to most people. Web servers wait for clients to connect and follow a similar idiom. Background processes on your computer may respond to events too. For example, an on-demand virus scanner may receive an event from the OS regarding a newly created or updated file, then scan that file for viruses.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.