Jodi
Jodi

Reputation: 900

When to use events?

At work, we have a huge framework and use events to send data from one part of it to another. I recently started a personal project and I often think to use events to control the interactions of my objects.

For example, I have a Mixer class that play sound effects and I initially thought I should receive events to play a sound effect. Then I decided to only make my class static and call

    Mixer.playSfx(SoundEffect)

in my classes. I have a ton of examples like this one where I initially think of an implementation with events and then change my mind, saying to myself it is too complex for nothing.

So when should I use events in a project? In which occasions events have a serious advantage over others techniques?

Upvotes: 9

Views: 2810

Answers (4)

I have been working with a huge code base at my previous work place and have seen, that using events can increase the complexity quite a lot and often unnecessarily.

I had often to reverse engineer existing code in order to fix it or to extend it. In both cases, it is a lot easier to understand what is going on, when you can simply read a list of function calls instead of just seeing the raise of an event.

The event forces you to look for usages in order to fully understand what is happening. Not a problem with modern IDEs, but if you then encounter many functions, which also raise events, it quickly becomes complex. I had encountered cases, where it mattered in what order functions did subscribe to an event, even though most languages don't even gurantee a calling order...

There are cases when it is a really good idea to use events. But before you start eventing, consider the alternative. It is probably easier to read and mantain.

A Classic example for the use of events is a UI framework, which provides elements like buttons etc. You want the function "ButtonPressed()" of the framework to call some of your functions, so that you can react to the user action. The alternative to an event that you can subscribe to, would for example be a public bool "buttonPressed", which the UI framework exposes and which you can regurlary check for beeing true or false. This is of course very ineffecient, when there are hundreds of UI elements.

Upvotes: 0

Ajit Singh
Ajit Singh

Reputation: 1016

Its a tradeoff between simplicity and re-usability. Lets take an metaphor of "Sending the email" process:

If you know the recipients and they are finite in number that you can always determine, its as simple as putting them in "To" list and hitting the send button. Its simple as thats what we use most of the time. This is calling the function directly.

However, in case of mailing list, you don't know in advance that how many users are going to subscribe to your email. In that case, you create a mailing list program where the users can subscribe to and the email goes automatically to all the subscribed users. This is event modeling.

Now, even though, in both above option, emails are sent to users, you are a better judge of when to send email directly and when to use the mailing list program. Apply the same judgement, hope that you would get your answer :)

Cheers,

Ajit.

Upvotes: 4

RBarryYoung
RBarryYoung

Reputation: 56735

The difference between Calling a subroutine and raising events has to do with: Specification, Election, Cardinality and ultimately, which side, the initiator or the receiver has Control.

With Calls, the initiator elects to call the receiving routine, and the initiator specifies the receiver. And this leads to many-to-one cardinality, as many callers may elect to call the same subroutine.

With Events on the other hand, the initiator raises an event that will be received by those routines that have elected to receive that event. The receiver specifies what events it will receive from what initiators. This then leads to one-to-many cardinality as one event source can have many receivers.

So the decision as to Calls or Events, mostly has to do with whether the initiator determines the receiver is or the receiver determines the initiator.

Upvotes: 5

Senthil Kumar
Senthil Kumar

Reputation: 479

You generally use events to notify subscribers about some action or state change that occurred on the object. By using an event, you let different subscribers react differently, and by decoupling the subscriber (and its logic) from the event generator, the object becomes reusable.

In your Mixer example, I'd have events signal the start and end of playing of the sound effect. If I were to use this in a desktop application, I could use those events to enable/disable controls in the UI.

Upvotes: 11

Related Questions